home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCPbb_2_1_src.lzh / UUCPBB21 / uucp.c < prev    next >
Text File  |  1994-09-25  |  7KB  |  264 lines

  1. /*  uucp.c   UNIX-to-UNIX copy program, send/request files to/from remote.
  2.     Copyright (C) 1990, 1993  Rick Adams and Bob Billson
  3.  
  4.     This file is part of the OS-9 UUCP package, UUCPbb.
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     The author of UUCPbb, Bob Billson, can be contacted at:
  21.     bob@kc2wz.bubble.org  or  uunet!kc2wz!bob  or  by snail mail:
  22.     21 Bates Way, Westfield, NJ 07090
  23. */
  24.  
  25. /*  uucp [-options] [system!]fromfile [system!]tofile  */
  26.  
  27. #define MAIN
  28.  
  29. #include "uucp.h"
  30.  
  31. extern QQ unsigned myuid;
  32. extern char user[];
  33. extern QQ char *nodename;
  34.  
  35. QQ FILE *log;
  36.  
  37.  
  38. main (argc, argv)
  39. int argc;
  40. char *argv[];
  41. {
  42.      char options[10], fromfile[100], tofile[100];
  43.      register int i;
  44.      char *p1, *p2;
  45.  
  46.      if ((argc < 2) ||  strcmp (argv[1], "-?") == 0)
  47.           usage();
  48.  
  49.      log = stderr;
  50.  
  51.      if (getparam() == FALSE)
  52.           exit (0);
  53.  
  54.      if ((spooldir = getdirs ("spooldir")) == NULL)
  55.           fatal ("spooldir not in Parameters");
  56.  
  57.      /* get user name */
  58.      getuser (user);
  59.  
  60.      /* parse all the arguments */
  61.      strcpy (options, "-");
  62.      *fromfile = *tofile = '\0';
  63.  
  64.      for (i = 1; i < argc; i++)
  65.           if (argv[i][0] == '-')
  66.                strcat (options, &argv[i][1]);
  67.           else
  68.             {  if (fromfile[0] == '\0')
  69.                     strcpy (fromfile, argv[i]);
  70.                else if (tofile[0] == '\0')
  71.                     strcpy (tofile, argv[i]);
  72.                else
  73.                     fatal ("too many file names");
  74.             }
  75.  
  76.      /* receive file from remote, or transmit file to remote? */
  77.      p1 = strchr (fromfile, '!');
  78.      p2 = strchr (tofile, '!');
  79.  
  80.      if ((p1 == NULL) && (p2 == NULL))
  81.           fatal ("no remote system specified");
  82.      else if ((p1 != NULL) && (p2 != NULL))
  83.           fatal ("can't do 3rd party copies");
  84.      else if (p1 == NULL)
  85.        {
  86.           /* uucp fromfile system!tofile */
  87.           *p2++ = '\0';
  88.           senduucp (tofile, options, fromfile, p2);
  89.        }
  90.      else
  91.        {
  92.           /* uucp system!fromfile tofile */
  93.           *p1 = '\0';
  94.           ++p1;
  95.           recvuucp (fromfile, options, p1, tofile);
  96.        }
  97.      exit (0);
  98. }
  99.  
  100.  
  101.  
  102. /* senduucp  --queue job to send file
  103.  
  104.           fromfile is local filename
  105.           tofile is "system!remotefile" */
  106.  
  107. int senduucp (system, options, fromfile, tofile)
  108. char *system, *options, *fromfile, *tofile;
  109. {
  110.      char line[100], cname[16], dname[16], tmp[256];
  111.      FILE *qfile, *file;
  112.      register int count;
  113.  
  114.      /* figure out filenames */
  115.      getfnames (system, cname, dname);
  116.  
  117.      if ((file = fopen (fromfile, "r")) == NULL)
  118.        {
  119.           sprintf (tmp, "can't open '%s'...error #%d", fromfile, errno);
  120.           fatal (tmp);
  121.        }
  122.  
  123.      /* go to proper spool file */
  124.      sprintf (line, "%s/%s", spooldir, system);
  125.      asetuid (0);
  126.  
  127.      if (chdir (line) != 0)
  128.        {
  129.           sprintf (tmp, "can't change to spool directory for '%s'", system);
  130.           fatal (tmp);
  131.        }
  132.  
  133.      asetuid (myuid);
  134.  
  135.      /* create spooled data file */
  136.      if (strchr (options, 'c') == NULL)
  137.        {
  138.           asetuid (0);
  139.  
  140.           if ((qfile = fopen (dname, "w")) == NULL)
  141.             {
  142.                sprintf (tmp, "can't create spooled data file '%s'", dname);
  143.                fatal (tmp);
  144.             }
  145.  
  146.           asetuid (myuid);
  147.  
  148.           /* copy file to spool directory */
  149.           while ((count = fread (tmp, sizeof (char), sizeof (tmp), file)) != 0)
  150.                fwrite (tmp, sizeof (char), count, qfile);
  151.  
  152.           fclose (qfile);
  153.        }
  154.  
  155.      /* close data file */
  156.      fclose (file);
  157.  
  158.      /* write control file */
  159.      asetuid (0);
  160.  
  161.      if ((qfile = fopen (cname, "w")) == NULL)
  162.           fatal("can't create control file");
  163.  
  164.      asetuid (myuid);
  165.  
  166.      /* S fromname toname user options dname perms user */
  167.      if (strchr (options, 'c') == NULL)
  168.           fprintf (qfile, "S %s %s %s %s %s 0666 %s\n",
  169.                           fromfile, tofile, user, options, dname, user);
  170.      else
  171.           fprintf (qfile, "S %s %s %s %s D.0 0666 %s\n",
  172.                           fromfile, tofile, user, options, user);
  173.      fclose (qfile);
  174.  
  175.      /* be sure files belong to the proper owner */
  176.      asetuid (0);
  177.      chown (dname, myuid);
  178.      chown (cname, myuid);
  179. }
  180.  
  181.  
  182.  
  183. /* recvuucp  --queue job to receive file
  184.  
  185.            fromfile is "system!remotefile"
  186.            tofile is local file   */
  187.  
  188. int recvuucp (system, options, fromfile, tofile)
  189. char *system, *options, *fromfile, *tofile;
  190. {
  191.      char line[100], cname[16], dname[16];
  192.      register FILE *qfile;
  193.  
  194.      /* figure out filenames */
  195.      getfnames (system, cname, dname);
  196.  
  197.      /* go to proper spool file */
  198.      asetuid (0);
  199.      sprintf (line, "%s/%s", spooldir, system);
  200.  
  201.      if (chdir (line) != 0)
  202.           fatal ("can't change to spool directory");
  203.  
  204.      /* write control file */
  205.      if ((qfile = fopen (cname, "w")) == NULL)
  206.           fatal ("can't create control file");
  207.  
  208.      asetuid (myuid);
  209.  
  210.      /* R fromname toname user options dname perms user */
  211.      if (strchr (options, 'c') == NULL)
  212.           fprintf (qfile, "R %s %s %s %s %s 0666 %s\n",
  213.                           fromfile, tofile, user, options, dname, user);
  214.      else
  215.           fprintf(qfile, "R %s %s %s %s D.0 0666 %s\n",
  216.                           fromfile, tofile, user, options, user);
  217.      fclose(qfile);
  218. }
  219.  
  220.  
  221.  
  222. int getfnames (system, controlfile, datafile)
  223. char *system, *controlfile, *datafile;
  224. {
  225.      char seq[10], *genseq();
  226.  
  227.      /* generate sequence number -- Changed REB */
  228.      strcpy (seq, genseq() );
  229.  
  230.      /* D.systemXXXXXXX filename (uucp spooled data file) */
  231.      sprintf (datafile, "D.%.8s%s", nodename, seq);
  232.  
  233.      /* C.systmCXXXXXXX (spooled control file) */
  234.      sprintf (controlfile, "C.%.8sC%s", system, seq);
  235. }
  236.  
  237.  
  238.  
  239. int fatal (msg)
  240. char *msg;
  241. {
  242.      asetuid (myuid);
  243.      fprintf (stderr, "uucp: %s", msg);
  244.  
  245.      if (errno != 0)
  246.           fprintf (stderr, "...error %d", errno);
  247.  
  248.      putc ('\n', stderr);
  249.      exit (0);
  250. }
  251.  
  252.  
  253.  
  254. int usage()
  255. {
  256.      fputs ("uucp:  unix to unix copy program\n\n", stderr);
  257.      fputs ("       uucp [-options] [system!]fromfile [system!]tofile\n\n",
  258.             stderr);
  259.      fprintf (stderr, "v%s (%s) This is free software released under the GNU General Public\n",
  260.                       version, VERDATE);
  261.      fputs ("License.  Please send suggestions/bug reports to:  bob@kc2wz.bubble.org\n", stderr);
  262.      exit (0);
  263. }
  264.