/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 *
 */

/*	Copyright (c) 1984 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident	"$Header: rmdir.c 1.4 89/11/22 $@(#)rmdir:rmdir.c	1.10"
/*
** Rmdir(1) removes directory.
** If -p option is used, rmdir(1) tries to remove the directory
** and it's parent directories.  It exits with code 0 if the WHOLE
** given path is removed and 2 if part of path remains.  
** Results are printed except when -s is used.
*/

#include <stdio.h>
#include <errno.h>
#include <string.h>

extern int opterr, optind, errno, rmdir(), rmdirp(), setuid();
extern char *optarg, *sys_errlist[], *malloc();
extern void exit(), free();
extern unsigned short getuid();

main(argc,argv)
int argc;
char **argv;
{

	int c, pflag, sflag, errflg, rc;
        char *ptr, *remain, *msg, *path;
	unsigned int pathlen;
	char	msgid[16];

	char *gettxt();

	pflag = sflag = 0;
	errflg = 0;
				/* set effective uid, euid, to be same as real	*/
				/* uid, ruid.  Rmdir(2) checks search & write	*/
				/* permissions for euid, but for compatibility	*/
				/* the check must be done using ruid.		*/
	if(setuid((int) getuid()) == -1) {
		sprintf(msgid,"syserr:%d",errno+1);
		fprintf(stderr, gettxt("rmdir:1", "rmdir: setuid(2) failed, %s\n"), gettxt(msgid,sys_errlist[errno]));
		exit(1);
	}

	while ((c = getopt(argc, argv, "ps")) != EOF)
		switch (c) {
			case 'p':
				pflag++;
				break;
			case 's':
				sflag++;
				break;
			case '?':
				errflg++;
				break;
		}
        if(argc < 2 || errflg) {
                fprintf(stderr, gettxt("rmdir:2", "rmdir: usage: rmdir [-ps] dirname ...\n"));
                exit(2);
        }
	argc -= optind;
	argv = &argv[optind];
        while (argc--) {
		ptr = *argv++;
  					/* -p option. Remove directory and parents.
					** Prints results of removing */
		if (pflag) {
			pathlen = (unsigned)strlen(ptr);
			if ((path = malloc(pathlen + 4)) == NULL ||
			    (remain=malloc(pathlen + 4)) == NULL) {
				fprintf(stderr, gettxt("rmdir:3", "rmdir: Out of memory\n"));
				exit(2);
			}
			strcpy(path,ptr);

				/* rmdirp() removes directory and parents */
				/* rc != 0 implies only part of path removed */

			if ((rc = rmdirp(path, remain)) == 0) {
				if (!sflag) 
					fprintf(stdout,
					gettxt("rmdir:4", "rmdir: %s: Whole path removed.\n"),ptr);
			}
			else {
				if (!sflag) {
					switch (rc) {
					case -1:
						if (errno == EEXIST)
							msg=gettxt("rmdir:5", "Directory not empty");
						else
						{
							sprintf(msgid,"syserr:%d",errno+1);
							msg = gettxt(msgid,sys_errlist[errno]);
						}
						break;
					case -2:
						msg=gettxt("rmdir:6", "Can not remove . or ..");
						break;
					case -3:
						msg=gettxt("rmdir:7", "Can not remove current directory");
						break;
					}	
					fprintf(stdout,
					gettxt("rmdir:8", "rmdir: %s: %s not removed; %s\n"),ptr, remain, msg);
				}
			}
			free(path);
			free(remain);
			continue;
		}

			/* No -p option. Remove only one directory */

		if (rmdir(ptr) == -1) {
			switch(errno) {
			case EEXIST:	msg = gettxt("rmdir:5", "Directory not empty");
					break;
			case ENOTDIR:	msg = gettxt("rmdir:9", "Path component not a directory");
					break;
			case ENOENT:	msg = gettxt("rmdir:10", "Directory does not exist");
					break;
			case EACCES:	msg = gettxt("rmdir:11", "Search or write permission needed");
					break;
			case EBUSY:	msg = gettxt("rmdir:12", "Directory is a mount point or in use");
					break;
			case EROFS:	msg = gettxt("rmdir:13", "Read-only file system");
					break;
			case EIO:	msg = gettxt("rmdir:14", "I/O error accessing file system");
					break;
			case EINVAL:	msg = gettxt("rmdir:15", "Can't remove current directory or ..");
					break;
			case EFAULT:
			default:
				        sprintf(msgid,"syserr:%d",errno+1);
					msg = gettxt(msgid,sys_errlist[errno]);
					break;
			}
			fprintf(stderr, gettxt("colon:65", "rmdir: %s: %s\n"), ptr, msg);
		continue;
		}
        }
        exit(errno?2:0);
}
