xref: /csrg-svn/bin/rm/rm.c (revision 6413)
1*6413Smckusic static char *sccsid = "@(#)rm.c	4.8 (Berkeley) 03/31/82";
21083Sbill int	errcode;
31083Sbill 
41083Sbill #include <stdio.h>
5*6413Smckusic #include <sys/param.h>
61083Sbill #include <sys/stat.h>
75898Smckusic #include <ndir.h>
81083Sbill 
91083Sbill char	*sprintf();
101083Sbill 
111083Sbill main(argc, argv)
121083Sbill char *argv[];
131083Sbill {
141083Sbill 	register char *arg;
151083Sbill 	int fflg, iflg, rflg;
161083Sbill 
171083Sbill 	fflg = 0;
181083Sbill 	if (isatty(0) == 0)
191083Sbill 		fflg++;
201083Sbill 	iflg = 0;
211083Sbill 	rflg = 0;
221083Sbill 	while(argc>1 && argv[1][0]=='-') {
231083Sbill 		arg = *++argv;
241083Sbill 		argc--;
251932Sroot 
261932Sroot 		/*
271932Sroot 		 *  all files following a null option are considered file names
281932Sroot 		 */
291932Sroot 		if (*(arg+1) == '\0') break;
301932Sroot 
311083Sbill 		while(*++arg != '\0')
321083Sbill 			switch(*arg) {
331083Sbill 			case 'f':
341083Sbill 				fflg++;
351083Sbill 				break;
361083Sbill 			case 'i':
371083Sbill 				iflg++;
381083Sbill 				break;
391083Sbill 			case 'r':
401083Sbill 				rflg++;
411083Sbill 				break;
421083Sbill 			default:
431083Sbill 				printf("rm: unknown option %s\n", *argv);
441083Sbill 				exit(1);
451083Sbill 			}
461083Sbill 	}
471083Sbill 	while(--argc > 0) {
481083Sbill 		if(!strcmp(*++argv, "..")) {
491083Sbill 			fprintf(stderr, "rm: cannot remove `..'\n");
501083Sbill 			continue;
511083Sbill 		}
521083Sbill 		rm(*argv, fflg, rflg, iflg, 0);
531083Sbill 	}
541083Sbill 
551083Sbill 	exit(errcode);
561083Sbill }
571083Sbill 
581083Sbill rm(arg, fflg, rflg, iflg, level)
591083Sbill char arg[];
601083Sbill {
611083Sbill 	struct stat buf;
625898Smckusic 	struct direct *dp;
635898Smckusic 	DIR *dirp;
645898Smckusic 	char name[BUFSIZ];
651083Sbill 	int d;
661083Sbill 
676402Sroot 	if(lstat(arg, &buf)) {
681083Sbill 		if (fflg==0) {
691083Sbill 			printf("rm: %s nonexistent\n", arg);
701083Sbill 			++errcode;
711083Sbill 		}
721083Sbill 		return;
731083Sbill 	}
741083Sbill 	if ((buf.st_mode&S_IFMT) == S_IFDIR) {
751083Sbill 		if(rflg) {
761083Sbill 			if (access(arg, 02) < 0) {
771083Sbill 				if (fflg==0)
781083Sbill 					printf("%s not changed\n", arg);
791083Sbill 				errcode++;
801083Sbill 				return;
811083Sbill 			}
821083Sbill 			if(iflg && level!=0) {
832057Sroot 				printf("remove directory %s? ", arg);
841083Sbill 				if(!yes())
851083Sbill 					return;
861083Sbill 			}
875898Smckusic 			if((dirp = opendir(arg)) == NULL) {
882057Sroot 				printf("rm: cannot read %s?\n", arg);
891083Sbill 				exit(1);
901083Sbill 			}
915898Smckusic 			while((dp = readdir(dirp)) != NULL) {
925898Smckusic 				if(dp->d_ino != 0 && !dotname(dp->d_name)) {
936003Sroot 					sprintf(name, "%s/%s", arg, dp->d_name);
941083Sbill 					rm(name, fflg, rflg, iflg, level+1);
951083Sbill 				}
961083Sbill 			}
975898Smckusic 			closedir(dirp);
981083Sbill 			errcode += rmdir(arg, iflg);
991083Sbill 			return;
1001083Sbill 		}
1011083Sbill 		printf("rm: %s directory\n", arg);
1021083Sbill 		++errcode;
1031083Sbill 		return;
1041083Sbill 	}
1051083Sbill 
1061083Sbill 	if(iflg) {
1072057Sroot 		printf("rm: remove %s? ", arg);
1081083Sbill 		if(!yes())
1091083Sbill 			return;
1101083Sbill 	}
1111083Sbill 	else if(!fflg) {
1121083Sbill 		if (access(arg, 02)<0) {
1136003Sroot 			printf("rm: override protection %o for %s? ", buf.st_mode&0777, arg);
1146003Sroot 			if(!yes())
1151083Sbill 				return;
1161083Sbill 		}
1171083Sbill 	}
1181083Sbill 	if(unlink(arg) && (fflg==0 || iflg)) {
1191083Sbill 		printf("rm: %s not removed\n", arg);
1201083Sbill 		++errcode;
1211083Sbill 	}
1221083Sbill }
1231083Sbill 
1241083Sbill dotname(s)
1251083Sbill char *s;
1261083Sbill {
1271083Sbill 	if(s[0] == '.')
1281083Sbill 		if(s[1] == '.')
1291083Sbill 			if(s[2] == '\0')
1301083Sbill 				return(1);
1311083Sbill 			else
1321083Sbill 				return(0);
1331083Sbill 		else if(s[1] == '\0')
1341083Sbill 			return(1);
1351083Sbill 	return(0);
1361083Sbill }
1371083Sbill 
1381083Sbill rmdir(f, iflg)
1391083Sbill char *f;
1401083Sbill {
1411083Sbill 	int status, i;
1421083Sbill 
1431083Sbill 	if(dotname(f))
1441083Sbill 		return(0);
1451083Sbill 	if(iflg) {
1462057Sroot 		printf("rm: remove %s? ", f);
1471083Sbill 		if(!yes())
1481083Sbill 			return(0);
1491083Sbill 	}
1501083Sbill 	while((i=fork()) == -1)
1511083Sbill 		sleep(3);
1521083Sbill 	if(i) {
1531083Sbill 		wait(&status);
1541083Sbill 		return(status);
1551083Sbill 	}
1561083Sbill 	execl("/bin/rmdir", "rmdir", f, 0);
1571083Sbill 	execl("/usr/bin/rmdir", "rmdir", f, 0);
1581083Sbill 	printf("rm: can't find rmdir\n");
1591083Sbill 	exit(1);
1601083Sbill }
1611083Sbill 
1621083Sbill yes()
1631083Sbill {
1641083Sbill 	int i, b;
1651083Sbill 
1661083Sbill 	i = b = getchar();
1671083Sbill 	while(b != '\n' && b != EOF)
1681083Sbill 		b = getchar();
1691083Sbill 	return(i == 'y');
1701083Sbill }
171