static char *sccsid
= "@(#)rm.c 4.23 (Berkeley) %G%";
* rm - for ReMoving files, directories & trees.
int fflg
; /* -f force - supress error messages */
int iflg
; /* -i interrogate user on each file */
int rflg
; /* -r recurse */
int errcode
; /* true if errors occured */
while ((ch
= getopt(argc
, argv
, "-Rfir")) != EOF
)
char *path
; /* pointer to malloc'ed buffer for path */
char *pathp
; /* current pointer to end of path */
int pathsz
; /* size of path */
#define isdot(a) (a[0] == '.' && (!a[1] || a[1] == '.' && !a[2]))
* Return TRUE if sucessful. Recursive with -r (rflg)
int ok
; /* true if recursive rm succeeded */
struct stat buf
; /* for finding out what a file is */
struct direct
*dp
; /* for reading a directory */
DIR *dirp
; /* for reading a directory */
char prevname
[MAXNAMLEN
+ 1]; /* previous name for -r */
fprintf(stderr
, "rm: cannot remove `.' or `..'\n");
fprintf(stderr
, "rm: %s nonexistent\n", arg
);
if ((buf
.st_mode
&S_IFMT
) == S_IFDIR
) {
fprintf(stderr
, "rm: %s directory\n", arg
);
if (iflg
&& level
!= 0) {
printf("rm: remove directory %s? ", arg
);
return (0); /* didn't remove everything */
if (access(arg
, R_OK
|W_OK
|X_OK
) != 0) {
return (1); /* salvaged: removed empty dir */
fprintf(stderr
, "rm: %s not changed\n", arg
);
if ((dirp
= opendir(arg
)) == NULL
) {
fprintf(stderr
, "rm: cannot read %s?\n", arg
);
while ((dp
= readdir(dirp
)) != NULL
) {
strcpy(prevname
, dp
->d_name
);
ok
= rm(path
, level
+ 1);
for (cp
= pathp
; *--cp
!= '/' && cp
> path
; )
if ((dirp
= opendir(arg
)) == NULL
) {
fprintf(stderr
, "rm: cannot read %s?\n", arg
);
/* pick up where we left off */
if (prevname
[0] != '\0') {
while ((dp
= readdir(dirp
)) != NULL
&&
strcmp(prevname
, dp
->d_name
) != 0)
/* skip the one we just failed to delete */
if (dp
!= NULL
&& strcmp(cp
, dp
->d_name
)) {
"rm: internal synchronization error: %s, %s, %s\n",
strcpy(prevname
, dp
->d_name
);
printf("rm: remove %s? ", arg
);
fprintf(stderr
, "rm: %s not removed\n", arg
);
if ((buf
.st_mode
&S_IFMT
) != S_IFLNK
&& access(arg
, W_OK
) < 0) {
printf("rm: override protection %o for %s? ",
printf("rm: remove %s? ", arg
);
rm
: if (unlink(arg
) < 0) {
fprintf(stderr
, "rm: %s: ", arg
);
* Get a yes/no answer from the user.
while (b
!= '\n' && b
!= EOF
)
* Append 'name' to 'path'.
pathsz
= MAXNAMLEN
+ MAXPATHLEN
+ 2;
if ((path
= malloc((u_int
)pathsz
)) == NULL
) {
fprintf(stderr
, "rm: ran out of memory\n");
} else if (pathp
+ n
+ 2 > path
+ pathsz
) {
fprintf(stderr
, "rm: path name too long: %s\n", path
);
} else if (pathp
!= path
&& pathp
[-1] != '/')
fputs("usage: rm [-rif] file ...\n", stderr
);