]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/progs/rmf.c
Replaced use of snprintf() with memcpy()/memmove().
[nmh] / docs / historical / mh-jun-1982 / progs / rmf.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include "../mh.h"
9 #include <stdio.h>
10 #include <strings.h>
11
12 extern struct swit anoyes[]; /* Std no/yes gans array */
13
14 int subf;
15
16 struct dirent {
17 short inum;
18 char name[14];
19 int pad;
20 };
21
22 struct swit switches[] = {
23 "help", 4, /* 0 */
24 0, 0
25 };
26
27
28 /*ARGSUSED*/
29 main(argc, argv)
30 char *argv[];
31 {
32 register char *cp, **ap;
33 char *folder, buf[128];
34 int def_fold = 0;
35 char *arguments[50], **argp;
36
37 invo_name = argv[0];
38 #ifdef NEWS
39 m_news();
40 #endif
41 folder = 0;
42 cp = r1bindex(argv[0], '/');
43 if((cp = m_find(cp)) != NULL) {
44 ap = brkstring(cp = getcpy(cp), " ", "\n");
45 ap = copyip(ap, arguments);
46 } else
47 ap = arguments;
48 VOID copyip(argv+1, ap);
49 argp = arguments;
50 while(cp = *argp++) {
51 if(*cp == '-')
52 switch(smatch(++cp, switches)) {
53 case -2:ambigsw(cp, switches); /* ambiguous */
54 goto leave;
55 /* unknown */
56 case -1:fprintf(stderr, "rmf: -%s unknown\n", cp);
57 goto leave;
58 /* -help */
59 case 0: help("rmf [+folder] [switches]", switches);
60 goto leave;
61 }
62 if(*cp == '+')
63 if(folder) {
64 fprintf(stderr, "Only one folder at a time.\n");
65 goto leave;
66 } else
67 folder = path(cp+1, TFOLDER);
68 else {
69 fprintf(stderr, "Usage: rmf [+folder]\n");
70 goto leave;
71 }
72 }
73 if(!m_find("path")) free(path("./", TFOLDER));
74 if(!folder) {
75 folder = m_getfolder();
76 def_fold++;
77 }
78 subf = !((!index(folder, '/')) | (*folder == '/') | (*folder == '.'));
79 if(def_fold && !subf) {
80 cp = concat("Remove folder \"", folder, "\" ?? ", NULLCP);
81 if(!gans(cp, anoyes))
82 goto leave;
83 free(cp);
84 }
85 if(rmfold(folder))
86 goto leave;
87 if(subf) { /* make parent "current" */
88 cp = copy(folder, buf);
89 while(cp > buf && *cp != '/') --cp;
90 if(cp > buf) {
91 *cp = 0;
92 if(strcmp(m_find(pfolder), buf) != 0) {
93 printf("[+%s now current]\n", buf);
94 m_replace(pfolder, buf);
95 }
96 }
97 }
98 leave:
99 m_update();
100 done(0);
101 }
102
103 struct dirent ent;
104
105 rmfold(fold)
106 char *fold;
107 {
108 register char *maildir;
109 int i, leftover, cd;
110
111 leftover = 0;
112 if(!subf && strcmp(m_find(pfolder), fold) == 0) /* make default "current"*/
113 if(strcmp(m_find(pfolder), defalt) != 0) {
114 printf("[+%s now current]\n", defalt);
115 VOID fflush(stdout); /*??*/
116 m_replace(pfolder, defalt);
117 }
118 maildir = m_maildir(fold);
119 if((cd = chdir(maildir)) < 0)
120 goto funnyfold;
121 if(access(".", 2) == -1) {
122 funnyfold: if(!m_delete(concat("cur-", fold, NULLCP)))
123 printf("[Folder %s de-referenced]\n", fold);
124 else
125 fprintf(stderr, "You have no profile entry for the %s folder %s\n",
126 cd < 0 ? "unreadable" : "read-only", fold);
127 return(1);
128 }
129 i = open(".", 0);
130 ent.pad = 0;
131 while(read(i, (char *)&ent.inum, sizeof ent.name + sizeof ent.inum))
132 if(ent.inum)
133 if((ent.name[0] >= '0' && ent.name[0] <= '9') ||
134 ent.name[0] == ',' ||
135 (ent.name[0] == '.' && ent.name[1] && ent.name[1] != '.') ||
136 strcmp(ent.name, "cur") == 0 ||
137 strcmp(ent.name, "@") == 0) {
138 if(unlink(ent.name) == -1) {
139 fprintf(stderr, "Can't unlink %s:%s\n", fold,ent.name);
140 leftover++;
141 }
142 } else if(strcmp(ent.name,".") != 0 &&
143 strcmp(ent.name,"..") != 0) {
144 fprintf(stderr, "File \"%s/%s\" not deleted!\n",
145 fold, ent.name);
146 leftover++;
147 }
148 VOID close(i);
149 VOID chdir(".."); /* Move out of dir to be deleted */
150 if(!leftover && removedir(maildir))
151 return(0);
152 else
153 fprintf(stderr, "Folder %s not removed!\n", fold);
154 return(1);
155 }
156
157
158 removedir(dir)
159 char *dir;
160 {
161 register int pid, wpid;
162 int status;
163
164 if((pid = fork()) == 0) {
165 m_update();
166 VOID fflush(stdout);
167 execl("/bin/rmdir", "rmdir", dir, 0);
168 execl("/usr/bin/rmdir", "rmdir", dir, 0);
169 fprintf(stderr, "Can't exec rmdir!!?\n");
170 return(0);
171 }
172 if(pid == -1) {
173 fprintf(stderr, "Can't fork\n");
174 return(0);
175 }
176 while((wpid = wait(&status)) != pid && wpid != -1) ;
177 if(status) {
178 fprintf(stderr, "Bad exit status (%o) from rmdir.\n", status);
179 /* return(0); */
180 }
181 retur