declare wmemall
[unix-history] / usr / src / bin / rm / rm.c
CommitLineData
f4d092e2 1static char *sccsid = "@(#)rm.c 4.9 (Berkeley) %G%";
ae80f4a2
BJ
2int errcode;
3
4#include <stdio.h>
d7ab2ea5 5#include <sys/param.h>
ae80f4a2 6#include <sys/stat.h>
f4d092e2 7#include <dir.h>
ae80f4a2
BJ
8
9char *sprintf();
10
11main(argc, argv)
12char *argv[];
13{
14 register char *arg;
15 int fflg, iflg, rflg;
16
17 fflg = 0;
18 if (isatty(0) == 0)
19 fflg++;
20 iflg = 0;
21 rflg = 0;
22 while(argc>1 && argv[1][0]=='-') {
23 arg = *++argv;
24 argc--;
ca02483b
BJ
25
26 /*
27 * all files following a null option are considered file names
28 */
29 if (*(arg+1) == '\0') break;
30
ae80f4a2
BJ
31 while(*++arg != '\0')
32 switch(*arg) {
33 case 'f':
34 fflg++;
35 break;
36 case 'i':
37 iflg++;
38 break;
39 case 'r':
40 rflg++;
41 break;
42 default:
43 printf("rm: unknown option %s\n", *argv);
44 exit(1);
45 }
46 }
47 while(--argc > 0) {
48 if(!strcmp(*++argv, "..")) {
49 fprintf(stderr, "rm: cannot remove `..'\n");
50 continue;
51 }
52 rm(*argv, fflg, rflg, iflg, 0);
53 }
54
55 exit(errcode);
56}
57
58rm(arg, fflg, rflg, iflg, level)
59char arg[];
60{
61 struct stat buf;
c1a06f06
KM
62 struct direct *dp;
63 DIR *dirp;
64 char name[BUFSIZ];
ae80f4a2
BJ
65 int d;
66
dd3897d5 67 if(lstat(arg, &buf)) {
ae80f4a2
BJ
68 if (fflg==0) {
69 printf("rm: %s nonexistent\n", arg);
70 ++errcode;
71 }
72 return;
73 }
74 if ((buf.st_mode&S_IFMT) == S_IFDIR) {
75 if(rflg) {
76 if (access(arg, 02) < 0) {
77 if (fflg==0)
78 printf("%s not changed\n", arg);
79 errcode++;
80 return;
81 }
82 if(iflg && level!=0) {
28c2b1a2 83 printf("remove directory %s? ", arg);
ae80f4a2
BJ
84 if(!yes())
85 return;
86 }
c1a06f06 87 if((dirp = opendir(arg)) == NULL) {
28c2b1a2 88 printf("rm: cannot read %s?\n", arg);
ae80f4a2
BJ
89 exit(1);
90 }
c1a06f06
KM
91 while((dp = readdir(dirp)) != NULL) {
92 if(dp->d_ino != 0 && !dotname(dp->d_name)) {
dfbe9024 93 sprintf(name, "%s/%s", arg, dp->d_name);
ae80f4a2
BJ
94 rm(name, fflg, rflg, iflg, level+1);
95 }
96 }
c1a06f06 97 closedir(dirp);
ae80f4a2
BJ
98 errcode += rmdir(arg, iflg);
99 return;
100 }
101 printf("rm: %s directory\n", arg);
102 ++errcode;
103 return;
104 }
105
106 if(iflg) {
28c2b1a2 107 printf("rm: remove %s? ", arg);
ae80f4a2
BJ
108 if(!yes())
109 return;
110 }
111 else if(!fflg) {
112 if (access(arg, 02)<0) {
dfbe9024
BJ
113 printf("rm: override protection %o for %s? ", buf.st_mode&0777, arg);
114 if(!yes())
ae80f4a2
BJ
115 return;
116 }
117 }
118 if(unlink(arg) && (fflg==0 || iflg)) {
119 printf("rm: %s not removed\n", arg);
120 ++errcode;
121 }
122}
123
124dotname(s)
125char *s;
126{
127 if(s[0] == '.')
128 if(s[1] == '.')
129 if(s[2] == '\0')
130 return(1);
131 else
132 return(0);
133 else if(s[1] == '\0')
134 return(1);
135 return(0);
136}
137
138rmdir(f, iflg)
139char *f;
140{
141 int status, i;
142
143 if(dotname(f))
144 return(0);
145 if(iflg) {
28c2b1a2 146 printf("rm: remove %s? ", f);
ae80f4a2
BJ
147 if(!yes())
148 return(0);
149 }
150 while((i=fork()) == -1)
151 sleep(3);
152 if(i) {
153 wait(&status);
154 return(status);
155 }
156 execl("/bin/rmdir", "rmdir", f, 0);
157 execl("/usr/bin/rmdir", "rmdir", f, 0);
158 printf("rm: can't find rmdir\n");
159 exit(1);
160}
161
162yes()
163{
164 int i, b;
165
166 i = b = getchar();
167 while(b != '\n' && b != EOF)
168 b = getchar();
169 return(i == 'y');
170}