home *** CD-ROM | disk | FTP | other *** search
- /*
- * This software is Copyright 1985, 1989 by Rick Adams.
- *
- * Permission is hereby granted to copy, reproduce, redistribute or
- * otherwise use this software as long as: there is no monetary
- * profit gained specifically from the use or reproduction or this
- * software, it is not sold, rented, traded or otherwise marketed, and
- * this copyright notice is included prominently in any copy
- * made.
- *
- * The author make no claims as to the fitness or correctness of
- * this software for any use whatsoever, and it is provided as is.
- * Any use of this software is at the user's own risk.
- *
- * Batch: program to batch a list of news articles into an unbatch script.
- * Usage: /usr/lib/news/batch listfile [bytecount]
- * where listfile is a file containing a list, one per line, of full
- * path names of files containing articles, e.g. as produced by the F
- * transmission option in the sys file.
- * bytecount is the maximum number of bytes to output before exiting
- * Output is placed on standard output.
- *
- * Intended usage:
- *
- * With the shellfile "sendbatch", with machine names as arguments:
- * e.g
- * sendbatch rlgvax seismo
- *
- * This would be invoked every hour or two from crontab.
- *
- * 10-Jul-90 Made somee changes to get it work with AmigaUUCP Plus
- * Ingo Feulner
- */
-
-
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <fcntl.h>
- #include "uucp:src/include/config.h"
-
- #include <exec/types.h>
- #include <libraries/dos.h>
- #include <proto/dos.h>
-
- #define index strchr
- #define BUFLEN 256
- #define FIB FileInfoBlock
-
- /* version string for 2.0 */
- static char *version20 = "$VER: batch 1.00 (5 Aug 90)\n\r";
-
- void logerror();
- int stat();
-
- struct stat sbuf;
-
- extern int errno;
- extern char *sys_errlist[];
-
- main(argc,argv)
- char **argv;
- {
- FILE *fd, *nfd;
- char *spooldir; /* News Directory */
- int c;
- long n;
- char path[256];
- char *cp;
- char *fdstatus;
- long maxbytes, nbytes;
- long atol();
- int spooldirlen;
- char fname[512];
- char workfile[512];
- char cbuf[BUFSIZ];
- char *index(), *fgets();
-
- if (argc < 2)
- {
- fprintf(stderr, "AmigaUUCP Plus: batch 1.00 (5 Aug 90)\n");
- fprintf(stderr, "Written by Ingo Feulner\n\n");
- fprintf(stderr, "Usage: batch listfile [bytecount]\n");
- exit(1);
- }
-
- getcwd(path, 255); /* Get current directory */
-
- spooldir = GetConfigDir(UUNEWS);
- spooldirlen = strlen(spooldir);
-
- /*
- * Rename real file to a work name to avoid race conditions.
- * If workfile exists skip the rename in order
- * to recover from a crash w/o losing anything.
- */
-
- if (chdir(spooldir) < 0)
- {
- logerror("chdir(%s): %s", spooldir, sys_errlist[errno]);
- exit(1);
- }
-
- (void) strcpy(workfile, argv[1]);
- (void) strcat(workfile, ".work");
- if (access(workfile, 0) < 0)
- {
- if (access(argv[1], 0) < 0 && errno == ENOENT)
- exit(0); /* no news */
- if (rename(argv[1], workfile) < 0)
- {
- logerror("rename(%s,%s) %s", argv[1], workfile,
- sys_errlist[errno]);
- exit(1);
- }
- }
- fd = fopen(workfile, "r");
- if (fd == NULL)
- {
- logerror("fopen(%s,r) %s", workfile, sys_errlist[errno]);
- exit(1);
- }
-
- if (argc > 2)
- maxbytes = atol(argv[2]);
- else
- maxbytes = 100000000L; /* backwards compatible */
- nbytes = 0;
- while ((fdstatus = fgets(fname, sizeof fname, fd)) != NULL)
- {
- cp = index(fname, '\n');
- if (cp)
- *cp = '\0';
- if (fname[0] == '\0')
- continue;
- /* this optimization speeds up batching significantly */
- if (fname[0] == '/' && fname[spooldirlen-1] == '/')
- nfd = fopen(&fname[spooldirlen], "r");
- else
- nfd = fopen(fname, "r");
-
- if (nfd == NULL)
- {
- perror(fname);
- continue;
- }
- (void) stat(fname, &sbuf);
- if (cp)
- *cp = '\n';
- if (sbuf.st_size == 0)
- {
- (void) fclose(nfd);
- continue;
- }
- nbytes += sbuf.st_size;
- if (nbytes > maxbytes && nbytes != sbuf.st_size)
- break;
- printf("#! rnews %ld\n", (long)sbuf.st_size);
- /* guess length of #! rnews string */
- nbytes += 13;
- n = 0;
- while (c = fread(cbuf, 1, sizeof cbuf, nfd))
- {
- fwrite(cbuf, 1, c, stdout);
- n += c;
- }
- (void) fclose(nfd);
- if (ferror(stdout))
- {
- logerror("stdout write %s", sys_errlist[errno]);
- exit(1);
- }
- (void) fflush(stdout);
- if (n != sbuf.st_size) { /* paranoia */
- logerror("%s, expected %ld bytes, got %ld", fname,
- n, sbuf.st_size);
- /* breaking out of this early will end up resyncing
- the batch files (isn't serendipity wonderful?) */
- break;
- }
- }
-
- if (fdstatus != NULL) /* exceeded maxbytes */
- {
- char tmpfile[512];
-
- (void) strcpy(tmpfile, argv[1]);
- (void) strcat(tmpfile, ".tmp");
- nfd = fopen(tmpfile, "w");
- if (nfd == NULL)
- {
- logerror("fopen(%s,w) %s", tmpfile, sys_errlist[errno]);
- exit(1);
- }
-
- do
- {
- fputs(fname, nfd);
- } while (fgets(fname, sizeof fname, fd) != NULL);
- if (ferror(nfd))
- {
- logerror("write(%s) %s", tmpfile, sys_errlist[errno]);
- exit(1);
- }
- (void) fclose(nfd);
- (void) fclose(fd);
- /* will pick it up next time thru */
-
- (void)remove(workfile);
- if (rename(tmpfile, workfile) < 0)
- {
- logerror("rename(%s,%s) %s", tmpfile, workfile,
- sys_errlist[errno]);
- exit(1);
- }
- }
- else
- {
- fclose(fd);
- (void) remove(workfile);
- }
-
- chdir(path);
- exit(0);
- /*NOTREACHED*/
- }
-
- /*
- * Log the given message, with printf strings and parameters allowed,
- * on the log file, if it can be written.
- */
- /* VARARGS1 */
- void logerror(fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
- char *fmt;
- long a1, a2, a3, a4, a5, a6, a7, a8, a9;
- {
- FILE *logfile;
- char *libdir;
-
- char lfname[BUFLEN]; /* the log file */
- char bfr[BUFLEN];
- char *logtime, *ctime();
- time_t t;
-
- libdir = GetConfigDir(UULIB);
-
- (void) time(&t);
- logtime = ctime(&t);
- logtime[16] = 0;
- logtime += 4;
-
- (void) sprintf(lfname, "%s/batch.errlog", libdir);
-
- (void) sprintf(bfr, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
- fprintf(stderr, bfr);
- if (access(lfname, 0) == 0 && (logfile = fopen(lfname, "a")) != NULL)
- {
- (void) lseek(fileno(logfile), 0L, 2);
- fprintf(logfile, "%s\tbatch\t%s\n", logtime, bfr);
- (void) fclose(logfile);
- }
- }
-
-
- stat(char *filename, struct stat *statbuf)
- {
- BPTR alock, parent;
- struct FIB *fib;
- int modes;
- extern long timezone;
-
- if ((!statbuf) || (!filename))
- {
- errno = EFAULT;
- return (-1);
- }
- if (alock = Lock(filename, ACCESS_READ))
- {
- if((fib = (struct FileInfoBlock *)malloc(sizeof(struct FIB))) == NULL)
- {
- UnLock(alock);
- errno = ENOMEM;
- return (-1); /* malloc failed */
- }
- Examine(alock, fib);
- if (!(parent = ParentDir(alock)))
- {
- statbuf->st_mode = 0700;
- statbuf->st_prot = ~0xf;
- }
- else
- {
- UnLock(parent);
- modes = (~fib->fib_Protection >> 1) & 0x7;
- statbuf->st_mode = (modes << 6);
- statbuf->st_prot = fib->fib_Protection;
- }
- if (fib->fib_EntryType > 0)
- statbuf->st_mode |= S_IFDIR;
- else
- statbuf->st_mode |= S_IFREG;
- statbuf->st_dev = 0;
- statbuf->st_ino = 0;
- statbuf->st_size = fib->fib_Size;
- statbuf->st_rdev = fib->fib_DiskKey;
- statbuf->st_gid = 0;
- statbuf->st_uid = 0;
-
- /*
- * getft() doesn't compensate for time zones.
- */
- statbuf->st_mtime = getft(filename) + timezone;
- statbuf->st_nlink = 1;
-
- /*
- * These next fields really only exist for Amiga tar's benefit
- */
- strcpy(statbuf->st_comment, fib->fib_Comment);
- memcpy((char *) &(statbuf->st_date), (char *) &(fib->fib_Date),
- sizeof(statbuf->st_date));
- free(fib);
- UnLock(alock);
- return (0);
- } else
- {
- errno = ENOENT;
- return (-1); /* couldn't access file */
- }
- }
-