]> diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/support/bboards/RCS/bbexp.c,v
Always check that mktemp()/mktemp2() succeeds before trying to
[nmh] / docs / historical / mh-6.8.5 / support / bboards / RCS / bbexp.c,v
1 head 1.6;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.6
9 date 92.12.15.00.20.22; author jromine; state Exp;
10 branches;
11 next 1.5;
12
13 1.5
14 date 92.02.03.17.51.21; author jromine; state Exp;
15 branches;
16 next 1.4;
17
18 1.4
19 date 90.04.05.15.32.49; author sources; state Exp;
20 branches;
21 next 1.3;
22
23 1.3
24 date 90.04.05.14.50.34; author sources; state Exp;
25 branches;
26 next 1.2;
27
28 1.2
29 date 90.03.20.16.25.09; author sources; state Exp;
30 branches;
31 next 1.1;
32
33 1.1
34 date 90.03.20.16.23.56; author sources; state Exp;
35 branches;
36 next ;
37
38
39 desc
40 @@
41
42
43 1.6
44 log
45 @endif sugar
46 @
47 text
48 @/* bbexp.c - expunge the BBoards area */
49 #ifndef lint
50 static char ident[] = "@@(#)$Id: bbexp.c,v 1.5 1992/02/03 17:51:21 jromine Exp jromine $";
51 #endif /* lint */
52
53 #include "../h/mh.h"
54 #include "../h/dropsbr.h"
55 #include "../zotnet/bboards.h"
56 #include <pwd.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61
62
63 #define FIRST 12
64 #define SECOND 20
65
66
67 static int broken_pipe;
68
69 static int move();
70 static process(), chgrp();
71 int pipeser ();
72
73
74 #ifndef __STDC__
75 #ifdef SYS5
76 struct passwd *getpwnam ();
77 #endif
78 #endif
79
80 /* \f */
81
82 /* ARGSUSED */
83
84 main (argc, argv)
85 int argc;
86 char **argv;
87 {
88 int first = FIRST,
89 second = SECOND;
90 char *cp;
91 struct bboard *bb;
92 struct passwd *pw;
93
94 invo_name = r1bindex (*argv++, '/');
95 m_foil (NULLCP);
96
97 if ((pw = getpwnam (BBOARDS)) == NULL)
98 adios (NULLCP, "no entry for ~%s", BBOARDS);
99 if (pw -> pw_uid != geteuid ())
100 adios (NULLCP, "not running setuid to %s", BBOARDS);
101
102 if (*argv && **argv == '-') {
103 if ((first = atoi (*argv + 1)) < 1)
104 first = FIRST;
105 argv++;
106 }
107 if (*argv && **argv == '-') {
108 if ((second = atoi (*argv + 1)) < 1)
109 second = SECOND;
110 argv++;
111 }
112
113 (void) setbbent (SB_STAY);
114 if (*argv == NULL)
115 while (bb = getbbent ()) {
116 if ((bb -> bb_flags & BB_ARCH) & (BB_ASAV | BB_AREM))
117 process (bb, pw, first, second);
118 }
119 else
120 while (cp = *argv++)
121 if ((bb = getbbnam (cp)) || (bb = getbbaka (cp))) {
122 if ((bb -> bb_flags & BB_ARCH) & (BB_ASAV | BB_AREM))
123 process (bb, pw, first, second);
124 }
125 else
126 advise (NULLCP, "no such BBoard as %s", cp);
127 (void) endbbent ();
128
129 exit (0);
130 }
131
132 /* \f */
133
134 static process (bb, pw, first, second)
135 struct bboard *bb;
136 struct passwd *pw;
137 int first,
138 second;
139 {
140 int fd,
141 td;
142 char *cp,
143 command[BUFSIZ],
144 tmpfil[BUFSIZ];
145 FILE *pf;
146 struct stat st;
147
148 if ((fd = lkopen (bb -> bb_file, 6)) == NOTOK) {
149 advise (bb -> bb_file, "unable to lock and open");
150 return;
151 }
152
153 (void) sprintf (tmpfil, "%s/#bbexpXXXXXX", pw -> pw_dir);
154 (void) unlink (mktemp (tmpfil));
155 if ((td = creat (tmpfil, BBMODE)) == NOTOK) {
156 advise (tmpfil, "unable to create");
157 goto out1;
158 }
159 (void) close (td);
160 if ((td = creat (cp = map_name (tmpfil), BBMODE)) == NOTOK) {
161 advise (cp, "unable to create");
162 goto out2;
163 }
164 (void) close (td);
165
166 if ((bb -> bb_flags & BB_ARCH)
167 && stat (bb -> bb_archive, &st) == NOTOK
168 && stat (bb -> bb_file, &st) != NOTOK
169 && (td = creat (bb -> bb_archive, (int) (st.st_mode & 0777))) != NOTOK)
170 (void) close (td);
171
172 (void) sprintf (command, "%s %s%s", mshproc, bb -> bb_file,
173 isatty (fileno (stdout)) ? " 2>&1 | cat" : "");
174 printf ("%s (%s old messages)\n", command,
175 (bb -> bb_flags & BB_ARCH) == BB_ASAV ? "archive" : "remove");
176 (void) fflush (stdout);
177 if ((pf = popen (command, "w")) == NULL) {
178 advise (NULLCP, "unable to popen \"%s\" for writing", command);
179 goto out3;
180 }
181 (void) signal (SIGPIPE, pipeser);
182 broken_pipe = 0;
183
184 fprintf (pf, "pick %s -before -%d -sequence select -zero\n",
185 "-datefield BB-Posted", first);
186 fprintf (pf, "pick -before -%d -sequence select -nozero\n", second);
187 fprintf (pf, "scan select\n");
188 if ((bb -> bb_flags & BB_ARCH) == BB_ASAV)
189 fprintf (pf, "pack select -file %s\n", bb -> bb_archive);
190 fprintf (pf, "rmm select\n");
191 fprintf (pf, "packf all -file %s\n", tmpfil);
192 #ifdef notdef /* want real EOF to end it */
193 fprintf (pf, "quit\n");
194 #endif /* notdef */
195 if (td = pclose (pf))
196 advise (NULLCP, "msh returns %d", td);
197 (void) signal (SIGPIPE, SIG_DFL);
198
199 if (move (tmpfil, bb -> bb_file) != NOTOK)
200 (void) move (cp, bb -> bb_map);
201
202 out3: ;
203 (void) unlink (cp);
204 out2: ;
205 (void) unlink (tmpfil);
206 out1: ;
207 (void) lkclose (fd, bb -> bb_file);
208 }
209
210 /* \f */
211
212 static int move (input, output)
213 char *input,
214 *output;
215 {
216 int i,
217 in,
218 out;
219 struct stat st1,
220 st2;
221
222 if ((in = open (input, 0)) == NOTOK) {
223 advise (input, "unable to re-open");
224 return NOTOK;
225 }
226
227 i = stat (output, &st1);
228 if ((out = creat (output, BBMODE)) == NOTOK) {
229 advise (output, "unable to re-create");
230 return NOTOK;
231 }
232 if (i != NOTOK && chmod (output, (int) (st1.st_mode & 0777)) == NOTOK)
233 admonish (output, "unable to change mode of");
234 if (i != NOTOK && stat (output, &st2) != NOTOK && st2.st_gid != st1.st_gid)
235 chgrp (output, st1.st_gid);
236
237 cpydata (in, out, input, output);
238
239 (void) close (in);
240 (void) close (out);
241
242 return OK;
243 }
244
245 /* \f */
246
247 static chgrp (file, gid)
248 char *file;
249 short gid;
250 {
251 int child_id;
252 char group[BUFSIZ];
253
254 switch (child_id = fork ()) {
255 case NOTOK:
256 admonish ("fork", "unable to");
257 return;
258
259 case OK:
260 (void) setuid (geteuid ());
261 (void) sprintf (group, "%d", gid);
262 execlp ("/bin/chgrp", chgrp, group, file, NULLCP);
263 fprintf (stderr, "unable to exec ");
264 perror ("/bin/chgrp");
265 _exit (1);
266
267 default:
268 (void) pidwait (child_id, OK);
269 break;
270 }
271 }
272
273 /* \f */
274
275 /* ARGSUSED */
276
277 static int pipeser (i)
278 int i;
279 {
280 #ifndef BSD42
281 (void) signal (SIGPIPE, pipeser);
282 #endif /* not BSD42 */
283
284 if (!broken_pipe++)
285 advise (NULLCP, "broken pipe");
286 }
287 @
288
289
290 1.5
291 log
292 @SYS5
293 STDC
294 @
295 text
296 @d3 2
297 a4 2
298 static char ident[] = "@@(#)$Id: bbexp.c,v 1.4 1990/04/05 15:32:49 sources Exp jromine $";
299 #endif lint
300 d147 1
301 a147 1
302 #endif notdef
303 d235 1
304 a235 1
305 #endif not BSD42
306 @
307
308
309 1.4
310 log
311 @add ID
312 @
313 text
314 @d3 1
315 a3 1
316 static char ident[] = "@@(#)$Id:$";
317 d27 2
318 d30 2
319 @
320
321
322 1.3
323 log
324 @add ID
325 @
326 text
327 @d3 1
328 a3 1
329 static char ident[] = "$Id:";
330 @
331
332
333 1.2
334 log
335 @ansi fixup
336 @
337 text
338 @d2 3
339 @
340
341
342 1.1
343 log
344 @Initial revision
345 @
346 text
347 @d19 2
348 @