|
|
1.1 root 1:
2: /*
3: * This software is Copyright (c) 1985 by Rick Adams.
4: *
5: * Permission is hereby granted to copy, reproduce, redistribute or
6: * otherwise use this software as long as: there is no monetary
7: * profit gained specifically from the use or reproduction or this
8: * software, it is not sold, rented, traded or otherwise marketed, and
9: * this copyright notice is included prominently in any copy
10: * made.
11: *
12: * The author make no claims as to the fitness or correctness of
13: * this software for any use whatsoever, and it is provided as is.
14: * Any use of this software is at the user's own risk.
15: *
16: * Batch: program to batch a list of news articles into an unbatch script.
17: * Usage: /usr/lib/news/batch listfile [bytecount]
18: * where listfile is a file containing a list, one per line, of full
19: * path names of files containing articles, e.g. as produced by the F
20: * transmission option in the sys file.
21: * bytecount is the maximum number of bytes to output before exiting
22: * Output is placed on standard output.
23: *
24: * Intended usage:
25: *
26: * With the shellfile "sendbatch", with machine names as arguments:
27: * e.g
28: * sendbatch rlgvax seismo
29: *
30: * This would be invoked every hour or two from crontab.
31: *
32: */
33:
34: #ifdef SCCSID
35: static char *SccsId = "@(#)batch.c 1.19 10/7/87";
36: #endif /* SCCSID */
37:
38: #include <stdio.h>
39: #include <sys/types.h>
40: #include <sys/stat.h>
41: #include <errno.h>
42: #include "defs.h"
43:
44: #if defined(USG) || defined(BSD4_2)
45: #include <fcntl.h>
46: #endif
47:
48: struct stat sbuf;
49:
50: extern int errno;
51: extern char *sys_errlist[];
52:
53: main(argc,argv)
54: char **argv;
55: {
56: register FILE *fd, *nfd;
57: register int c;
58: register long n;
59: register char *cp;
60: char *fdstatus;
61: long maxbytes, nbytes;
62: long atol();
63: char fname[512];
64: char workfile[512];
65: char cbuf[BUFSIZ];
66: char *index(), *fgets();
67:
68: if (argc < 2) {
69: fprintf(stderr, "Usage: batch listfile [bytecount]\n");
70: exit(1);
71: }
72:
73: /*
74: * Rename real file to a work name to avoid race conditions.
75: * If workfile exists skip the rename in order
76: * to recover from a crash w/o losing anything.
77: */
78: (void) strcpy(workfile, argv[1]);
79: (void) strcat(workfile, ".work");
80: if (access(workfile, 0) < 0) {
81: if (access(argv[1], 0) < 0 && errno == ENOENT)
82: exit(0); /* no news */
83: if (rename(argv[1], workfile) < 0) {
84: logerror("rename(%s,%s) %s", argv[1], workfile,
85: sys_errlist[errno]);
86: exit(1);
87: }
88: }
89: fd = fopen(workfile, "r");
90: if (fd == NULL) {
91: logerror("fopen(%s,r) %s", workfile, sys_errlist[errno]);
92: exit(1);
93: }
94:
95: if (argc > 2)
96: maxbytes = atol(argv[2]);
97: else
98: maxbytes = 100000000L; /* backwards compatible */
99: nbytes = 0;
100: while ((fdstatus = fgets(fname, sizeof fname, fd)) != NULL) {
101: cp = index(fname, '\n');
102: if (cp)
103: *cp = '\0';
104: if (fname[0] == '\0')
105: continue;
106: nfd = fopen(fname, "r");
107: if (nfd == NULL) {
108: perror(fname);
109: continue;
110: }
111: (void) fstat(fileno(nfd), &sbuf);
112: if (cp)
113: *cp = '\n';
114: if (sbuf.st_size == 0)
115: continue;
116: nbytes += sbuf.st_size;
117: if (nbytes > maxbytes && nbytes != sbuf.st_size)
118: break;
119: printf("#! rnews %ld\n", (long)sbuf.st_size);
120: /* guess length of #! rnews string */
121: nbytes += 13;
122: n = 0;
123: while (c = fread(cbuf, 1, sizeof cbuf, nfd)) {
124: fwrite(cbuf, 1, c, stdout);
125: n += c;
126: }
127: (void) fclose(nfd);
128: if (ferror(stdout)){
129: logerror("stdout write %s", sys_errlist[errno]);
130: exit(1);
131: }
132: (void) fflush(stdout);
133: if (n != sbuf.st_size) { /* paranoia */
134: logerror("%s, expected %ld bytes, got %ld", fname,
135: n, sbuf.st_size);
136: /* breaking out of this early will end up resyncing
137: the batch files (isn't serendipity wonderful?) */
138: break;
139: }
140: }
141: if (fdstatus != NULL) { /* exceeded maxbytes */
142: char tmpfile[512];
143:
144: (void) umask(2);
145: (void) strcpy(tmpfile, argv[1]);
146: (void) strcat(tmpfile, ".tmp");
147: nfd = fopen(tmpfile, "w");
148: if (nfd == NULL) {
149: logerror("fopen(%s,w) %s", tmpfile, sys_errlist[errno]);
150: exit(1);
151: }
152: do {
153: fputs(fname, nfd);
154: } while (fgets(fname, sizeof fname, fd) != NULL);
155: if (ferror(nfd)) {
156: logerror("write(%s) %s", tmpfile, sys_errlist[errno]);
157: exit(1);
158: }
159: (void) fclose(nfd);
160: (void) fclose(fd);
161: /* will pick it up next time thru */
162: if (rename(tmpfile, workfile) < 0) {
163: logerror("rename(%s,%s) %s", tmpfile, workfile,
164: sys_errlist[errno]);
165: exit(1);
166: }
167: }
168: else
169: (void) unlink(workfile);
170: exit(0);
171: }
172:
173: /*
174: * Log the given message, with printf strings and parameters allowed,
175: * on the log file, if it can be written.
176: */
177: /* VARARGS1 */
178: logerror(fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
179: char *fmt;
180: long a1, a2, a3, a4, a5, a6, a7, a8, a9;
181: {
182: FILE *logfile;
183: char lfname[BUFLEN]; /* the log file */
184: char bfr[BUFLEN];
185: char *logtime, *ctime();
186: time_t t;
187:
188: (void) time(&t);
189: logtime = ctime(&t);
190: logtime[16] = 0;
191: logtime += 4;
192:
193: #if defined(LOGDIR) || defined(HOME)
194: (void) sprintf(lfname, "%s/%s/errlog", logdir(HOME), LIBDIR);
195: #else
196: (void) sprintf(lfname, "%s/errlog", LIBDIR);
197: #endif
198:
199: (void) sprintf(bfr, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
200: fprintf(stderr, bfr);
201: if (access(lfname, 0) == 0 && (logfile = fopen(lfname, "a")) != NULL) {
202: #if defined(USG) || defined(BSD4_2)
203: int flags;
204: flags = fcntl(fileno(logfile), F_GETFL, 0);
205: (void) fcntl(fileno(logfile), F_SETFL, flags|O_APPEND);
206: #else /* v7 */
207: (void) lseek(fileno(logfile), 0L, 2);
208: #endif /* v7 */
209: fprintf(logfile, "%s\tbatch\t%s\n", logtime, bfr);
210: (void) fclose(logfile);
211: }
212: }
213:
214: #if !defined(BSD4_2)
215: rename(from, to)
216: register char *from, *to;
217: {
218: (void) unlink(to);
219: if (link(from, to) < 0)
220: return -1;
221:
222: (void) unlink(from);
223: return 0;
224: }
225: #endif /* !BSD4_2 */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.