[4286] in Athena Bugs

home help back first fref pref prev next nref lref last post

/bin/rm

daemon@ATHENA.MIT.EDU (John F Carr)
Thu Feb 22 01:56:44 1990

Date: Thu, 22 Feb 90 01:56:08 -0500 (EST)
From: John F Carr <jfc@ATHENA.MIT.EDU>
To: bugs@ATHENA.MIT.EDU
This message contains patches to rm to print error messages more useful
than "foo not removed" and to remove the dependency on printf() (this
reduces size on the RT by 20 K).

*** /source/bsd-4.3/common/bin/rm.c	Thu Aug  6 14:20:21 1987
--- rm.c	Thu Feb 22 01:51:56 1990
***************
*** 9,14 ****
--- 9,16 ----
  #include <sys/stat.h>
  #include <sys/dir.h>
  #include <sys/file.h>
+ #include <errno.h>
+ #include <strings.h>
  
  int	fflg;		/* -f force - supress error messages */
  int	iflg;		/* -i interrogate user on each file */
***************
*** 16,23 ****
  
  int	errcode;	/* true if errors occured */
  
! char	*strcpy(), *malloc(), *realloc();
  
  main(argc, argv)
  	char *argv[];
  {
--- 18,32 ----
  
  int	errcode;	/* true if errors occured */
  
! extern char	*malloc(), *realloc();
  
+ void usage()
+ 
+ {
+   fputs("usage: rm [-rif] file ...\n",stderr);
+   exit(1);
+ }
+ 
  main(argc, argv)
  	char *argv[];
  {
***************
*** 52,66 ****
  				break;
  
  			default:
! 				fprintf(stderr, "usage: rm [-rif] file ...\n");
! 				exit(1);
  			}
  	}
  
! 	if (argc < 2 && !fflg) {
! 		fprintf(stderr, "usage: rm [-rif] file ...\n");
! 		exit(1);
! 	}
  
  	while (--argc > 0)
  		(void) rm(*++argv, 0);
--- 61,72 ----
  				break;
  
  			default:
! 				usage();
  			}
  	}
  
! 	if (argc < 2 && !fflg)
! 		usage();
  
  	while (--argc > 0)
  		(void) rm(*++argv, 0);
***************
*** 87,98 ****
  
  	if (dotname(arg)) {
  		if (!fflg) 
! 			fprintf(stderr, "rm: cannot remove `.' or `..'\n");
  		return (0);
  	}
  	if (lstat(arg, &buf)) {
  		if (!fflg) {
! 			fprintf(stderr, "rm: %s nonexistent\n", arg);
  			errcode++;
  		}
  		return (0);		/* error */
--- 93,107 ----
  
  	if (dotname(arg)) {
  		if (!fflg) 
! 			fputs("rm: cannot remove `.' or `..'\n",stderr);
  		return (0);
  	}
  	if (lstat(arg, &buf)) {
  		if (!fflg) {
! 			if(errno == ENOENT)
! 				rep_error("%1 nonexistent\n", arg,0,0);
! 			else
! 				rep_error("%1: %2",arg,sys_errlist[errno]);
  			errcode++;
  		}
  		return (0);		/* error */
***************
*** 100,112 ****
  	if ((buf.st_mode&S_IFMT) == S_IFDIR) {
  		if (!rflg) {
  			if (!fflg) {
! 				fprintf(stderr, "rm: %s directory\n", arg);
  				errcode++;
  			}
  			return (0);
  		}
  		if (iflg && level != 0) {
! 			printf("rm: remove directory %s? ", arg);
  			if (!yes())
  				return (0);	/* didn't remove everything */
  		}
--- 109,121 ----
  	if ((buf.st_mode&S_IFMT) == S_IFDIR) {
  		if (!rflg) {
  			if (!fflg) {
! 				rep_error("%1 directory\n", arg);
  				errcode++;
  			}
  			return (0);
  		}
  		if (iflg && level != 0) {
! 			msg("remove directory %1? ", arg);
  			if (!yes())
  				return (0);	/* didn't remove everything */
  		}
***************
*** 114,120 ****
  			if (rmdir(arg) == 0)
  				return (1);	/* salvaged: removed empty dir */
  			if (!fflg) {
! 				fprintf(stderr, "rm: %s not changed\n", arg);
  				errcode++;
  			}
  			return (0);		/* error */
--- 123,129 ----
  			if (rmdir(arg) == 0)
  				return (1);	/* salvaged: removed empty dir */
  			if (!fflg) {
! 			  rep_error("%1 not changed (permission denied)\n", arg);
  				errcode++;
  			}
  			return (0);		/* error */
***************
*** 121,127 ****
  		}
  		if ((dirp = opendir(arg)) == NULL) {
  			if (!fflg) {
! 				fprintf(stderr, "rm: cannot read %s?\n", arg);
  				errcode++;
  			}
  			return (0);
--- 130,136 ----
  		}
  		if ((dirp = opendir(arg)) == NULL) {
  			if (!fflg) {
! 				rep_error("cannot read %1?\n", arg);
  				errcode++;
  			}
  			return (0);
***************
*** 143,149 ****
  			*cp++ = '\0';
  			if ((dirp = opendir(arg)) == NULL) {
  				if (!fflg) {
! 					fprintf(stderr, "rm: cannot read %s?\n", arg);
  					errcode++;
  				}
  				break;
--- 152,158 ----
  			*cp++ = '\0';
  			if ((dirp = opendir(arg)) == NULL) {
  				if (!fflg) {
! 					rep_error("cannot read %1?\n", arg);
  					errcode++;
  				}
  				break;
***************
*** 158,166 ****
  			if (!ok) {
  				dp = readdir(dirp);
  				if (dp != NULL && strcmp(cp, dp->d_name)) {
! 					fprintf(stderr,
! 			"rm: internal synchronization error: %s, %s, %s\n",
! 						arg, cp, dp->d_name);
  				}
  				strcpy(prevname, dp->d_name);
  			}
--- 167,174 ----
  			if (!ok) {
  				dp = readdir(dirp);
  				if (dp != NULL && strcmp(cp, dp->d_name)) {
! 					rep_error("internal synchronization error: %1, %2, %3\n",
! 						  arg, cp, dp->d_name);
  				}
  				strcpy(prevname, dp->d_name);
  			}
***************
*** 171,183 ****
  			*pathp = '\0';
  		}
  		if (iflg) {
! 			printf("rm: remove %s? ", arg);
  			if (!yes())
  				return (0);
  		}
  		if (rmdir(arg) < 0) {
  			if (!fflg || iflg) {
! 				fprintf(stderr, "rm: %s not removed\n", arg);
  				errcode++;
  			}
  			return (0);
--- 179,192 ----
  			*pathp = '\0';
  		}
  		if (iflg) {
! 			msg("remove %1? ", arg);
  			if (!yes())
  				return (0);
  		}
  		if (rmdir(arg) < 0) {
  			if (!fflg || iflg) {
! 				rep_error("%1 not removed: %2\n",
! 					  arg,sys_errlist[errno]);
  				errcode++;
  			}
  			return (0);
***************
*** 186,198 ****
  	}
  
  	if (iflg) {
! 		printf("rm: remove %s? ", arg);
  		if (!yes())
  			return (0);
  	} else if (!fflg) {
  		if ((buf.st_mode&S_IFMT) != S_IFLNK && access(arg, W_OK) < 0) {
! 			printf("rm: override protection %o for %s? ",
! 				buf.st_mode&0777, arg);
  			if (!yes())
  				return (0);
  		}
--- 195,208 ----
  	}
  
  	if (iflg) {
! 		msg("remove %1? ", arg);
  		if (!yes())
  			return (0);
  	} else if (!fflg) {
  		if ((buf.st_mode&S_IFMT) != S_IFLNK && access(arg, W_OK) < 0) {
! 		  /* printf("rm: override protection %o for %s? ",
! 		     buf.st_mode&0777, arg); */
! 		msg("override protection for %1? ",arg,0,0);
  			if (!yes())
  				return (0);
  		}
***************
*** 199,205 ****
  	}
  	if (unlink(arg) < 0) {
  		if (!fflg || iflg) {
! 			fprintf(stderr, "rm: %s not removed\n", arg);
  			errcode++;
  		}
  		return (0);
--- 209,215 ----
  	}
  	if (unlink(arg) < 0) {
  		if (!fflg || iflg) {
! 			rep_error("%1 not removed: %2\n",arg,sys_errlist[errno]);
  			errcode++;
  		}
  		return (0);
***************
*** 249,263 ****
  	if (path == NULL) {
  		pathsz = MAXNAMLEN + MAXPATHLEN + 2;
  		if ((path = malloc(pathsz)) == NULL) {
! 			fprintf(stderr, "rm: ran out of memory\n");
  			exit(1);
  		}
  		pathp = path;
  	} else if (pathp + n + 2 > path + pathsz) {
! 		fprintf(stderr, "rm: path name too long: %s\n", path);
  		exit(1);
  	} else if (pathp != path && pathp[-1] != '/')
  		*pathp++ = '/';
  	strcpy(pathp, name);
  	pathp += n;
  }
--- 259,316 ----
  	if (path == NULL) {
  		pathsz = MAXNAMLEN + MAXPATHLEN + 2;
  		if ((path = malloc(pathsz)) == NULL) {
! 			fputs("rm: ran out of memory\n",stderr);
  			exit(1);
  		}
  		pathp = path;
  	} else if (pathp + n + 2 > path + pathsz) {
! 		rep_error("path name too long : %1",path);
  		exit(1);
  	} else if (pathp != path && pathp[-1] != '/')
  		*pathp++ = '/';
  	strcpy(pathp, name);
  	pathp += n;
+ }
+ 
+ rep_error(s0,s1,s2,s3)
+ 	char *s0,*s1,*s2,*s3;
+ {
+   char c;
+   fputs("rm: ",stderr);
+   while(c = *s0++) {
+     if(c == '%')
+       {
+ 	if((c = *s0++) == '1')
+ 	  fputs(s1,stderr);
+ 	else if(c == '2') 
+ 	  fputs(s2,stderr);
+ 	else if(c == '3') 
+ 	  fputs(s3,stderr);
+       } else {
+ 	putc(c,stderr);
+       }
+   }
+   fflush(stderr);
+ }
+ 
+ 
+ msg(s0,s1,s2,s3)
+ 	char *s0,*s1,*s2,*s3;
+ {
+   char c;
+   fputs("rm: ",stdout);
+   while(c = *s0++) {
+     if(c == '%')
+       {
+ 	if((c = *s0++) == '1')
+ 	  fputs(s1,stdout);
+ 	else if(c == '2') 
+ 	  fputs(s2,stdout);
+ 	else if(c == '3') 
+ 	  fputs(s3,stdout);
+       } else {
+ 	putc(c,stdout);
+       }
+   }
+  fflush(stdout);
  }



    --John Carr (jfc@athena.mit.edu)

home help back first fref pref prev next nref lref last post