xref: /csrg-svn/bin/rm/rm.c (revision 13491)
1*13491Ssam static char *sccsid = "@(#)rm.c	4.12 (Berkeley) 06/30/83";
21083Sbill int	errcode;
31083Sbill 
41083Sbill #include <stdio.h>
56413Smckusic #include <sys/param.h>
61083Sbill #include <sys/stat.h>
7*13491Ssam #include <sys/dir.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);
989852Ssam 			if (dotname(arg))
999852Ssam 				return;
1009852Ssam 			if (iflg) {
1019852Ssam 				printf("rm: remove %s? ", arg);
1029852Ssam 				if (!yes())
1039852Ssam 					return;
1049852Ssam 			}
1059852Ssam 			if (rmdir(arg) < 0) {
106*13491Ssam 				fprintf(stderr, "rm: ");
1079852Ssam 				perror(arg);
1089852Ssam 				errcode++;
1099852Ssam 			}
1101083Sbill 			return;
1111083Sbill 		}
1121083Sbill 		printf("rm: %s directory\n", arg);
1131083Sbill 		++errcode;
1141083Sbill 		return;
1151083Sbill 	}
1161083Sbill 
1171083Sbill 	if(iflg) {
1182057Sroot 		printf("rm: remove %s? ", arg);
1191083Sbill 		if(!yes())
1201083Sbill 			return;
1211083Sbill 	}
1221083Sbill 	else if(!fflg) {
1237274Ssam 		if ((buf.st_mode&S_IFMT) != S_IFLNK && access(arg, 02) < 0) {
1246003Sroot 			printf("rm: override protection %o for %s? ", buf.st_mode&0777, arg);
1256003Sroot 			if(!yes())
1261083Sbill 				return;
1271083Sbill 		}
1281083Sbill 	}
1291083Sbill 	if(unlink(arg) && (fflg==0 || iflg)) {
1301083Sbill 		printf("rm: %s not removed\n", arg);
1311083Sbill 		++errcode;
1321083Sbill 	}
1331083Sbill }
1341083Sbill 
1351083Sbill dotname(s)
1361083Sbill char *s;
1371083Sbill {
1381083Sbill 	if(s[0] == '.')
1391083Sbill 		if(s[1] == '.')
1401083Sbill 			if(s[2] == '\0')
1411083Sbill 				return(1);
1421083Sbill 			else
1431083Sbill 				return(0);
1441083Sbill 		else if(s[1] == '\0')
1451083Sbill 			return(1);
1461083Sbill 	return(0);
1471083Sbill }
1481083Sbill 
1491083Sbill yes()
1501083Sbill {
1511083Sbill 	int i, b;
1521083Sbill 
1531083Sbill 	i = b = getchar();
1541083Sbill 	while(b != '\n' && b != EOF)
1551083Sbill 		b = getchar();
1561083Sbill 	return(i == 'y');
1571083Sbill }
158