home *** CD-ROM | disk | FTP | other *** search
- /*#define PROCDEBUG*/
- /*
- * UUX.C
- *
- * Copyright 1988 by William Loftus. All rights reserved.
- * Copyright 1993 by Michael B. Smith. All rights reserved.
- *
- * Example: uux mail-message "burdvax!rmail wpl"
- *
- * NOTE: UUX uses C.<host>N<seqno> instead of C.<host>A<seqno> to ensure
- * UUX transfers occur *after* any email
- *
- * NOTE: The DEFAULT grade is 'N'. The user may set it to whatever they wish.
- *
- * The DEFAULT user is from "UserName" in UULib:Config. The user may
- * set it to whatever they wish.
- *
- * The DEFAULT is to copy the file. Setting -c causes this not to
- * be done.
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include "version.h"
- #include "protos.h"
- #include "owndevunit.h"
-
- IDENT (".11");
-
- #define ARRAY_SIZE 128
-
- #ifdef PROCDEBUG
- #define D(x) printf x
- #else
- #define D(x)
- #endif
-
- #ifdef NULL
- #undef NULL
- #endif
-
- #define NULL 0
-
- char
- *name = NULL,
- *NodeName = NULL,
- *request_addr = NULL, /* -a */
- ExecFile [ARRAY_SIZE],
- RemoteExecFile [ARRAY_SIZE],
- CommandFile [ARRAY_SIZE],
- DataFile [ARRAY_SIZE],
- RemoteDataFile [ARRAY_SIZE],
- path [ARRAY_SIZE];
-
- int
- _bufsiz = 8192, /* stdio buffer size */
- UseSubDirs = 0,
- nocopy = 0, /* -c (also affects -C) */
- nomail = 0, /* -n */
- prjobid = 0, /* -j */
- nouucico = 1, /* -r -- we don't anyway */
- mailonerr = 0, /* -z */
- linkit = 0, /* -l */
- usestdin = 0, /* -p */
- seq;
-
- struct Library
- *OwnDevUnitBase = NULL;
- static const char
- odu_name [] = { ODU_NAME };
-
- void myexit (void);
- int Copy (const BPTR, const char *);
- int Queue (const int, const char *, const BPTR,
- const char *, const char *, const char *);
-
- int
- usage (void)
- {
- if (name) {
- fprintf (stderr, "Usage: %s [-cCjlnpr] [-a addr] [-g grade] [-u user]\n"
- "\t\t[<filename> | -] <command>\n\n", name);
- fprintf (stderr, "Example: %s mail-message \"burdvax!rmail wpl\"\n", name);
- }
-
- exit (30);
- }
-
- int
- brk (void)
- {
- return 0;
- }
-
- int
- main (int argc, char **argv)
- {
- int
- i,
- avail,
- error;
- char
- *user = NULL, /* -u */
- *grade = "N", /* -g */
- *cmd = NULL, /* specified command */
- *file = NULL, /* specified data file */
- *tmp;
- BPTR
- fh = NULL; /* specified data file */
-
- if (argc == 0)
- /* can't run from WB */
- exit (30);
-
- name = argv [0];
- memset (path, '\0', sizeof (path));
-
- atexit (myexit);
- onbreak (brk);
-
- if ((OwnDevUnitBase = OpenLibrary (odu_name, 0)) == NULL) {
- fprintf (stderr, "Unable to open %s\n", odu_name);
- exit (30);
- }
-
- tmp = GetConfig (USESUBDIRS, "n");
- if (*tmp == 'y' || *tmp == 'Y' || *tmp == '1')
- UseSubDirs = 1;
- D (("UseSubDirs %ld\n", UseSubDirs));
-
- NodeName = FindConfig (NODENAME);
- if (!NodeName) {
- fprintf (stderr, "Cannot find local NodeName");
- exit (30);
- }
- D (("NodeName '%s'\n", NodeName));
-
- if (argc < 3)
- usage ();
-
- avail = argc - 1;
- for (i = 1; i < argc; i++) {
- tmp = argv [i];
- if (*tmp != '-')
- break;
- avail--;
- tmp++;
- switch (*tmp) {
- case '\0':
- case 'p':
- /*
- ** use stdin as the file
- */
- nocopy = 0; /* make sure it gets copied */
- usestdin = 1;
- D (("set usestdin = 1, nocopy = 0\n"));
- break;
-
- case 'a':
- /*
- ** send mail to specified address
- */
- tmp++;
-
- if (*tmp)
- request_addr = tmp;
- else {
- avail--;
- request_addr = argv [++i];
- }
- D (("set request_addr = '%s'\n", request_addr));
- break;
-
- case 'C':
- /*
- ** do copy local files to spool
- */
- nocopy = 0;
- D (("set nocopy = 0\n"));
- break;
-
- case 'c':
- /*
- ** do not copy local files to spool
- */
- nocopy = 1;
- D (("set nocopy = 1\n"));
- break;
-
- case 'g':
- /*
- ** set grade (priority) of transfer
- */
- 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':
- {
- char
- *configfile;
-
- tmp++;
- if (*tmp)
- configfile = tmp;
- else {
- avail--;
- configfile = argv [++i];
- }
- D (("ignored config file '%s'\n", configfile));
- /* FIXME: implement alternate configuration files */
- fprintf (stderr, "Warning: the use alternate configuration files is not supported\n");
- break;
- }
-
- case 'j':
- /*
- ** print the jobid
- */
- prjobid = 1;
- D (("set prjobid = 1\n"));
- break;
-
- case 'l':
- /*
- ** link the file, instead of copying
- */
- if (DOSBase->dl_lib.lib_Version > 36) {
- linkit = 1;
- D (("set linkit = 1\n"));
- }
- else {
- D (("can't set linkit below 2.04\n"));
- }
- /* FIXME: next two lines (link vs. copy) */
- nocopy = 0;
- fprintf (stderr, "Warning: linking not implemented. File will be copied.\n");
- break;
-
- case 'n':
- /*
- ** do not send mail, even on error
- */
- nomail = 1;
- D (("set nomail = 1\n"));
- break;
-
- case 'r':
- /*
- ** don't start UUCICO immediately
- */
- nouucico = 1;
- /* FIXME: implement -r properly. Maybe. */
- D (("set nouucico = 1\n"));
- break;
-
- case 'u':
- /*
- ** we are this user
- */
- tmp++;
-
- if (*tmp)
- user = tmp;
- else {
- avail--;
- user = argv [++i];
- }
-
- D (("set user to '%s'\n", user));
- break;
-
- case 'x':
- /*
- ** set debugging options
- */
- {
- char
- *debug;
-
- tmp++;
- if (tmp)
- debug = tmp;
- else {
- avail--;
- debug = argv [++i];
- }
- D (("ignored debug '%s'\n", debug));
- /* FIXME: implement debug */
- fprintf (stderr, "Warning: debug options not implemented\n");
- break;
- }
-
- case 'z':
- /*
- ** send mail on error
- */
- mailonerr = 1;
- D (("set mailonerr = 1\n"));
- break;
-
- default:
- usage ();
- }
- }
-
- if (avail != 2)
- if (!(avail == 1 && usestdin)) {
- D (("avail %ld\n", avail));
- usage ();
- }
-
- if (!user)
- user = GetUserName ();
- if (user == NULL) {
- fprintf (stderr, "Cannot find local UserName\n");
- exit (30);
- }
- D (("user '%s'\n", user));
-
- cmd = argv [argc - 1];
-
- if (usestdin) {
- file = "<stdin>";
- fh = Input ();
- }
- else {
- file = argv [argc - 2];
- fh = Open (file, MODE_OLDFILE);
- }
-
- if (!fh) {
- fprintf (stderr, "Cannot open '%s' (error %ld)\n", file, IoErr ());
- exit (30);
- }
-
- getcwd (path, ARRAY_SIZE);
- safe_chdir (GetConfigDir (UUSPOOL));
-
- seq = GetSequence (4);
- if (seq >= 0) {
- error = Queue (nocopy, file, fh, user, grade, cmd);
-
- UnLockFile (ExecFile);
- UnLockFile (CommandFile);
- UnLockFile (DataFile);
- }
-
- if (usestdin == 0)
- Close (fh);
-
- if (seq < 0 || error < 0)
- exit (30);
-
- exit (0);
- }
-
- void
- myexit (void)
- {
- UnLockFiles ();
-
- if (OwnDevUnitBase) {
- CloseLibrary(OwnDevUnitBase);
- OwnDevUnitBase = NULL;
- }
-
- return;
- }
-
- int
- Queue (const int nocopy, const char *filename, const BPTR fh,
- const char *user, const char *grade, const char *command)
- {
- char
- *p,
- *q,
- *pseq = SeqToName (seq),
- system_name [32];
- int
- it_is_me;
- BPTR
- lock;
- FILE
- *fp;
-
- p = command;
- q = system_name;
-
- while (*p != '!')
- *q++ = *p++;
- *q = '\0';
- p++; /* points after ! now */
-
- it_is_me = stricmp (system_name, NodeName) ? 0 : 1;
- if (it_is_me == 0 && !is_in_L_sys_file (system_name)) {
- fprintf (stderr, "System \"%s\" not in L.sys file.\n", system_name);
- return -1;
- }
- D (("system_name '%s', NodeName '%s', it_is_me %ld\n", system_name, NodeName, it_is_me));
-
- /* we are currently in UUSPOOL, see if we need to switch directories */
- if (UseSubDirs && (lock = Lock (system_name, ACCESS_READ))) {
- safe_chdir (system_name);
- UnLock (lock);
- D (("in uuspool:%s\n", system_name));
- }
- else
- UseSubDirs = 0;
-
- /*
- * exec_file Exec file as it appears on remote machine
- * x_exec_file Remote's Exec file as it appears on this machine
- * command_file Command file (this machine only)
- * data_file DataFile as it appears on
- * x_data_file
- *
- * local (overload) txfer_to remote (uunet)
- *
- * C.uunetNxxxx <not txfered> command file
- * D.uunetXxxxx X.overloaXxxxx exec / Coexec
- * D.uunetBxxxx D.overloaBxxxx data file
- */
-
- sprintf (CommandFile, "C.%.7s%s%s", system_name, grade, pseq);
-
- if (it_is_me) {
- sprintf (ExecFile, "X.%.7sX%s", system_name, pseq);
- }
- else {
- sprintf (ExecFile, "D.%.7sX%s", system_name, pseq);
- }
-
- if (nocopy) {
- char
- *p = path + (strlen (path) - 1);
-
- if (*p == ':' || *p == '/')
- sprintf (DataFile, "%s%s", path, filename);
- else
- sprintf (DataFile, "%s/%s", path, filename);
- }
- else {
- sprintf (DataFile, "D.%.7sB%s", system_name, pseq);
- }
-
- ++seq;
- sprintf (RemoteExecFile, "X.%.7sX%s", NodeName, SeqToName (seq));
- ++seq;
- sprintf (RemoteDataFile, "D.%.7sB%s", NodeName, SeqToName (seq));
-
- D (("nocopy %ld UseSubDirs %ld file '%s' user '%s' grade '%s'\n",
- nocopy, UseSubDirs, filename, user, grade));
- D (("Datafile '%s'\n", DataFile));
- D (("Execfile '%s'\n", ExecFile));
- D (("Cmdfile '%s'\n", CommandFile));
- D (("RmtExec '%s'\n", RemoteExecFile));
- D (("RmtData '%s'\n", RemoteDataFile));
-
- LockFile (CommandFile);
- LockFile (ExecFile);
- LockFile (DataFile);
-
- fp = fopen (ExecFile, "w");
- if (!fp) {
- perror (ExecFile);
- return -1;
- }
-
- fprintf (fp, "U %s %s\n", user, NodeName);
- fprintf (fp, "F %s\n", it_is_me ? DataFile : RemoteDataFile);
- fprintf (fp, "I %s\n", it_is_me ? DataFile : RemoteDataFile);
- fprintf (fp, "C %s\n", p);
- if (mailonerr)
- fprintf (fp, "Z\n");
- if (nomail)
- fprintf (fp, "N\n");
- if (request_addr)
- fprintf (fp, "R %s\n", request_addr);
- fclose (fp);
-
- if (it_is_me == 0) {
- fp = fopen (CommandFile, "w");
- if (!fp) {
- perror (CommandFile);
- return -1;
- }
-
- fprintf (fp,"S %s %s %s - %s 0666\n",
- DataFile, RemoteDataFile, user, DataFile);
- fprintf (fp,"S %s %s %s - %s 0666\n",
- ExecFile, RemoteExecFile, user, ExecFile);
-
- fclose (fp);
-
- if (prjobid)
- printf ("job %s\n", CommandFile + 2);
- }
- else
- if (prjobid)
- printf ("job %s\n", ExecFile + 2);
-
- if (nocopy)
- return 1;
-
- return Copy (fh, DataFile);
- }
-
- 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;
- }
-