home *** CD-ROM | disk | FTP | other *** search
- /*#define DEBUG*/
- /*
- * UUCP.c originally written by William Loftus
- *
- * Copyright 1988 by William Loftus. All rights reserved.
- * Copyright 1990 by Matthew Dillon. All rights reserved.
- * Copyright 1993, 1994 by Michael B. Smith. All rights reserved.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "version.h"
- #include "protos.h"
- #include <owndevunit.h>
-
- IDENT (".07");
-
- #define ARRAY_SIZE 128
-
- #ifdef DEBUG
- #define D(x) printf x
- #else
- #define D(x)
- #endif
-
- char
- *config = NULL, /* -I */
- *name = NULL,
- *mailuser = NULL, /* -n */
- path [ARRAY_SIZE],
- command_file [ARRAY_SIZE],
- data_file [ARRAY_SIZE];
- int
- UseSubDirs = 0,
- prJobid = 0, /* -j */
- doCopy = 0, /* -c/-C */
- doCreate = 1, /* -d/-f */
- noUUCICO = 1, /* -r is the only way we work */
- mailstat = 0, /* -m (& -n) */
- seq;
- struct Library
- *OwnDevUnitBase = NULL;
- static const char
- odu_name [] = { ODU_NAME };
-
- void BuildSendFile (const char *cmd, const char *file, const char *grade, const char *user);
- void BuildReceiveFile (const char *cmd, const char *file, const char *grade, const char *user);
- void myexit (void);
- const char *expand_file (const char *file_name);
- int Copy (const BPTR fh, const char *to);
-
- void
- usage (void)
- {
- fprintf (stderr, "usage: %s [-g grade] [-u user] from_file to_file\n", name);
- exit (30);
- }
-
- int
- main (int argc, char **argv)
- {
- int
- avail,
- i;
- char
- *tmp,
- *grade = "A",
- *user = NULL,
- *from,
- *from_buf,
- *dest,
- *to_buf;
-
- name = argv [0];
-
- if (argc == 0)
- exit (30);
- if (argc < 3)
- usage ();
-
- if (argc > 3) {
- avail = argc - 1;
- for (i = 1; i < argc; i++) {
- tmp = argv [i];
- if (*tmp != '-')
- break;
- tmp++;
- avail--;
- switch (*tmp) {
- case 'c':
- doCopy = 0;
- D (("set doCopy = 0\n"));
- break;
-
- case 'C':
- doCopy = 1;
- D (("set doCopy = 1\n"));
- break;
-
- case 'd':
- doCreate = 1;
- D (("set doCreate = 1\n"));
- break;
-
- case 'f':
- doCreate = 0;
- D (("set doCreate = 0\n"));
- break;
-
- case 'g':
- tmp++;
- if (*tmp)
- grade = tmp;
- else {
- avail--;
- grade = argv [++i];
- }
-
- if (strlen (grade) > 1)
- usage ();
-
- D (("set grade to '%s'\n", grade));
- break;
-
- case 'I':
- /* FIXME: -I config option */
- tmp++;
- if (*tmp)
- config = tmp;
- else {
- avail--;
- config = argv [++i];
- }
- D (("config '%s'\n", config));
- fprintf (stderr, "Warning: setting '-I %s' is ignored\n", config);
- break;
-
- case 'j':
- prJobid = 1;
- D (("set prJobid = 1\n"));
- break;
-
- case 'm':
- mailstat = 1;
- D (("set mailstat = 1\n"));
- break;
-
- case 'n':
- tmp++;
- if (*tmp)
- mailuser = tmp;
- else {
- avail--;
- mailuser = argv [++i];
- }
- D (("mailuser '%s'\n", mailuser));
- break;
-
- case 'r':
- noUUCICO = 1;
- D (("set noUUCICO = 1\n"));
- break;
-
- case 'u':
- tmp++;
- if (*tmp)
- user = tmp;
- else {
- avail--;
- user = argv [++i];
- }
-
- D (("set user to '%s'\n", user));
- break;
-
- case 'x':
- tmp++;
- if (*tmp)
- ;
- else {
- avail--;
- tmp = argv [++i];
- }
- /* FIXME: -x debug options? */
- fprintf (stderr, "Warning: setting '-x %s' is ignored\n", tmp);
- break;
-
- default:
- usage ();
- }
- }
-
- if (avail != 2)
- usage ();
- }
-
- from_buf = argv [argc - 2];
- to_buf = argv [argc - 1];
-
- getcwd (path, ARRAY_SIZE);
- atexit (myexit);
-
- if ((OwnDevUnitBase = OpenLibrary (odu_name, 0)) == NULL) {
- fprintf (stderr, "Unable to open %s\n", odu_name);
- exit (30);
- }
-
- if (safe_chdir (GetConfigDir (UUSPOOL))) {
- fprintf (stderr, "Couldn't change current working directory to %s\n", GetConfigDir (UUSPOOL));
- exit (30);
- }
-
- if (!user)
- user = GetUserName ();
- if (!user) {
- fprintf (stderr, "Can't find USERNAME in UULib:Config\n");
- exit (30);
- }
-
- tmp = GetConfig (USESUBDIRS, "0");
- if (*tmp == 'y' || *tmp == 'Y' || *tmp == '1')
- UseSubDirs = 1;
-
- seq = GetSequence (2);
- if (seq < 0) {
- exit (30);
- }
-
- from = strchr (from_buf, '!');
- dest = strchr (to_buf, '!');
-
- D (("from_buf '%s'\n", from_buf));
- D (("to_buf '%s'\n", to_buf));
-
- if (from && dest) {
- fprintf (stderr, "Can't specify a remote system in both arguments.\n");
- exit (30);
- }
- if (from == NULL && dest == NULL) {
- fprintf (stderr, "No remote system specified.\n");
- exit (30);
- }
-
- if (from) {
- BuildReceiveFile (from_buf, to_buf, grade, user);
- }
-
- if (dest) {
- BuildSendFile (to_buf, from_buf, grade, user);
- }
-
- exit (0);
- }
-
- void
- myexit (void)
- {
- UnLockFiles ();
-
- if (OwnDevUnitBase) {
- CloseLibrary (OwnDevUnitBase);
- OwnDevUnitBase = NULL;
- }
-
- return;
- }
-
- char
- system_name [32];
-
- char *
- set_system_name (const char *buf)
- {
- /*
- ** set_system_name
- **
- ** Puts the UUCP name from buf into system_name.
- ** Verifies that the system_name is in UULib:L.Sys
- **
- ** Returns the character after the '!' in buf
- */
- char
- *p,
- *q;
-
- p = buf;
- q = system_name;
-
- while (*p != '!')
- *q++ = *p++;
- *q = '\0';
- D (("system_name '%s'\n", system_name));
-
- if (!is_in_L_sys_file (system_name)) {
- fprintf (stderr, "System \"%s\" not in L.sys file.\n", system_name);
- exit (30);
- }
-
- return ++p;
- }
-
- FILE *
- open_command_file (const char *system_name, const char *grade)
- {
- FILE
- *fp;
- BPTR
- lock;
- char
- *ptr;
-
- if (UseSubDirs && (lock = Lock ((UBYTE *) system_name, ACCESS_READ))) {
- UnLock (lock);
- strcpy (command_file, system_name);
- strcat (command_file, "/");
- ptr = command_file + strlen (command_file);
- }
- else {
- ptr = command_file;
- *ptr = '\0';
- }
-
- strcpy (data_file, ptr);
-
- sprintf (ptr, "C.%.7s%s%s", system_name, grade, SeqToName (seq));
- D (("command file name '%s'\n", command_file));
-
- /* I may never use data_file, but lets prepare it for use, anyway */
- sprintf (data_file + strlen (data_file), "D.%.7sB%s", system_name, SeqToName (seq));
-
- LockFile (command_file);
- LockFile (data_file);
-
- fp = fopen (command_file, "w");
- if (!fp) {
- perror (command_file);
- UnLockFile (data_file);
- UnLockFile (command_file);
- exit (30);
- }
-
- if (prJobid)
- printf ("job %s\n", ptr + 2);
-
- return fp;
- }
-
- void
- BuildSendFile (const char *cmd, const char *file,
- const char *grade, const char *user)
- {
- BPTR
- fh;
- FILE
- *fp;
- char
- *empty = "",
- *which_file,
- *ex_file,
- *p,
- options [7];
-
- D (("BuildSendFile: cmd '%s', grade '%s', user '%s'\n",
- cmd, grade, user));
-
- ex_file = expand_file (file);
- if (!(fh = Lock (ex_file, ACCESS_READ))) {
- fprintf (stderr, "Local file does not exist '%s'\n", ex_file);
- exit (30);
- }
- UnLock (fh);
-
- p = set_system_name (cmd);
-
- fp = open_command_file (system_name, grade);
-
- fh = Open (ex_file, MODE_OLDFILE);
- if (!fh) {
- fprintf (stderr, "Your system is screwy '%s'\n", ex_file);
- exit (30);
- }
- if (doCopy && (Copy (fh, data_file) < 0)) {
- Close (fh);
- fprintf (stderr, "Local copy failed\n");
- exit (30);
- }
- Close (fh);
-
- sprintf (options, "-%s%s%s%s",
- doCopy ? "C" : "c",
- doCreate ? "d" : "f",
- mailstat ? "m" : "",
- mailuser ? "n" : ""
- );
-
- /* srcnam destnam who flags temp mode who */
- if (doCopy)
- which_file = data_file;
- else
- which_file = ex_file;
-
- fprintf (fp, "S %s %s %s %s %s %s %s\n",
- /*
- ** FROM - what file was the original
- ** TO - name of destination file
- ** USER - name of user who requested transfer
- ** OPTS - options for transfer
- ** TEMP - if opt C then file to copy from, else if c then FROM
- ** (TEMP is deleted after transfer if C)
- ** MODE - Unix executable mode. Forced to 0666
- ** NOTI - if opt n, then the mailaddress to send notification
- ** to about the transfer.
- */
- which_file, p, user, options, which_file, "0666",
- mailuser ? mailuser : user
- );
-
- fclose (fp);
- UnLockFile (data_file);
- UnLockFile (command_file);
-
- printf ("Command queued for transfer to %s.\n", system_name);
-
- return;
- }
-
- void
- BuildReceiveFile (const char *cmd, const char *file,
- const char *grade, const char *user)
- {
- FILE
- *fp;
- char
- *ex_file,
- *p,
- options [6];
-
-
- D (("BuildReceiveFile: cmd '%s', grade '%s', user '%s'\n",
- cmd, grade, user));
-
- ex_file = expand_file (file);
- p = set_system_name (cmd);
-
- fp = open_command_file (system_name, grade);
-
- /*
- ** FROM - name of file to be sent to us
- ** TO - what the file will be called here
- ** USER - name of the user requesting the transfer
- ** OPTS - d - create directories
- ** f - fail if directories not present
- ** m - send mail to USER when transfer complete
- ** c - damned if I know. left for historical reasons.
- */
- sprintf (options, "-c%s%s",
- doCreate ? "d" : "f",
- (mailuser || mailstat) ? "m" : ""
- );
-
- /* srcnam destnam who flags */
- fprintf (fp, "R %s %s %s %s\n",
- p,
- ex_file,
- mailuser ? mailuser : user,
- options
- );
-
- fclose (fp);
- UnLockFile (data_file);
- UnLockFile (command_file);
-
- printf ("Command queued for transfer from %s.\n", system_name);
-
- return;
- }
-
- const char *
- expand_file (const char *file_name)
- {
- char
- *colon;
- static char
- name [ARRAY_SIZE];
-
- colon = strchr (file_name, ':');
- if (colon && (colon != file_name)) {
- D (("expand_file result1 '%s'\n", file_name));
-
- return file_name;
- }
-
- if (path [strlen (path) - 1] != ':') {
- sprintf (name, "%s/%s", path, file_name);
- D (("expand_file result2 '%s'\n", name));
-
- return name;
- }
-
- sprintf (name, "%s%s", path, file_name);
- D (("expand_file result3 '%s'\n", name));
-
- return name;
- }
-
- int
- Copy (const BPTR fh, const char *to)
- {
- BPTR
- fo;
- int
- len;
- static char
- to_buf [2048];
-
- DeleteFile (to);
- fo = Open (to, MODE_NEWFILE);
- if (!fo) {
- fprintf (stderr, "Cannot open '%s' (error %ld)\n", to, IoErr ());
- return -1;
- }
-
- Seek (fh, 0, OFFSET_BEGINNING);
-
- while ((len = Read (fh, to_buf, sizeof (to_buf))) > 0) {
- Write (fo, to_buf, len);
- }
-
- Close (fo);
-
- return 1;
- }
-