home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCP_Blars.lzh / uucp.c < prev    next >
C/C++ Source or Header  |  1991-11-02  |  7KB  |  306 lines

  1. /*
  2.  *      uucp.c
  3.  *
  4.  *      For OS-9/6809 by Mark D. Griffith
  5.  *      Copyright (c) 1990
  6.  *
  7.  */
  8.  
  9. /* Includes */
  10.  
  11. #include "uucp.h"
  12.  
  13. #include <stdio.h>
  14. #include <modes.h>
  15. #include <dir.h>
  16. #include <strings.h>
  17. #include <direct.h>
  18. #include <errno.h>
  19. #include <time.h>
  20. #include <pwd.h>
  21.  
  22. #define TRUE    (1)
  23. #define FALSE    (0)
  24. #define ERROR    (-1)
  25.  
  26. /* Globals */
  27.  
  28. #define BUFSIZE 16385        /* copy file buffer */
  29.  
  30. struct fildes outfdbuf, infdbuf;
  31. FILE   *fdfile, *fcfile, *fbfile, *log, *fp, *popen();
  32. char   *mfgets(), *ltoa();
  33.  
  34. int     isremote, bflag, cflag, nflag, nfiles;
  35. char    Filename[200][80], copy_buf[BUFSIZE];
  36. char    bfile[32], cfile[32], dfile[32], dest_file[32], cur_path[128];
  37. char    rmtname[25], rmtuser[128], myname[25], user[25];
  38. char    n_user[25], from[256], options[10], tmp[256];
  39. char   *p, *ptr;
  40. int     uucpuid, orguid;
  41. char    grade = 'Z';
  42.  
  43. main(argc, argv)
  44. int     argc;
  45. char  **argv;
  46. {
  47.     int     i = 0;
  48.     struct passwd *pwentp;
  49.  
  50.     bflag = FALSE;
  51.     cflag = FALSE;
  52.     nflag = FALSE;
  53.     nfiles = 0;
  54.     strcpy(options, "-dC");
  55.     *n_user = '\0';
  56.  
  57.     if (argc < 3)
  58.     usage();
  59.  
  60.  
  61.     if (getwd(cur_path) == NULL) {
  62.     printf("\nUnable to determine current directory path\n");
  63.     exit(1);
  64.     }
  65.     strcat(cur_path, "/");
  66.  
  67.     /* Find out who we are */
  68.  
  69.     pwentp = getpwuid(orguid = getuid());
  70.     if (pwentp == NULL) {
  71.     fprintf(stderr, "Could not get user name\n");
  72.     exit(1);
  73.     }
  74.     strcpy(user, pwentp->pw_name);
  75.     pwentp = getpwname("uucp");
  76.     if (pwentp == NULL) {
  77.     fprintf(stderr, "Could not get uucp's uid\n");
  78.     exit(1);
  79.     }
  80.     uucpuid = pwentp->pw_gid << 16 | pwentp->pw_uid;
  81.     endpwent();
  82.     strcpy(n_user, user);
  83.  
  84.     /* *  Parse the command line arguements */
  85.  
  86.     while ((--argc > 0)) {
  87.     if (*(p = *++argv) == '-') {
  88.         while (*++p) {
  89.         switch (*p) {
  90.  
  91.         case 'c':    /* don't copy source file */
  92.             cflag = TRUE;
  93.             options[2] = 'c';
  94.             break;
  95.  
  96.         case 'n':    /* mail remote user when done */
  97.             strcat(options, "n");
  98.  
  99.             while (*++p == ' ')    /* skip any leading white space */
  100.             ;
  101.  
  102.             strcpy(n_user, p);
  103.             p += (strlen(n_user) + 1);
  104.             break;
  105.  
  106.         case 'b':    /* force a binary file */
  107.             bflag = TRUE;
  108.             break;
  109.  
  110.         case 'g':    /* specify grade */
  111.             grade = *++p;
  112.             break;
  113.  
  114.         case '?':
  115.             usage();
  116.             break;
  117.  
  118.         default:
  119.             fprintf(stderr, "uucp - unknown flag : -%c\n", *p);
  120.  
  121.         }
  122.         }
  123.     } else {
  124.  
  125.         if ((ptr = index(p, '!')) == NULL) {    /* if not the remote */
  126.  
  127.         if (*p != '/' && *p != '~') {        /* if not a full */
  128.             strcpy(Filename[nfiles], cur_path);    /* pathname, prepend */
  129.             strcat(Filename[nfiles++], p);    /* dir path */
  130.         } else
  131.             strcpy(Filename[nfiles++], p);    /* copy the filename */
  132.  
  133.         isremote = FALSE;
  134.  
  135.         } else {
  136.  
  137.         *ptr = '\0';    /* got the remote name */
  138.         isremote = TRUE;
  139.         strcpy(rmtname, p);    /* separate into rmote */
  140.         strcpy(rmtuser, ptr + 1);    /* and remote usename */
  141.         }
  142.     }
  143.     }                /* end of outer while */
  144.  
  145.     /* See if we talk to requested remote machine */
  146.  
  147.     if (CheckLegalName(rmtname) == FALSE) {
  148.     printf("\nRemote system '%s' not known\n", rmtname);
  149.     exit(0);
  150.     }
  151.     /* Change to the spool directory */
  152.  
  153.     setuid(uucpuid);
  154.  
  155.     if (chdir(SPOOLDIR) < 0 || (chdir(rmtname) < 0 &&
  156.             (makdir(rmtname, S_IFDIR, S_IFDIR | S_IREAD | S_IWRITE) < 0 ||
  157.         chdir(rmtname) < 0))) {
  158.     fprintf(stderr, "Can't chdir to spool directory for %s\n", rmtname);
  159.     exit(1);
  160.     }
  161.  
  162.     /* *      Create the C. work file for the transfer */
  163.     sprintf(cfile, "C.%.7s%c%04x", rmtname, grade, getseq());
  164.  
  165.     if ((fcfile = fopen(cfile, "w")) == NULL) {
  166.     fprintf(stderr, "Can't create %s\n", cfile);
  167.     exit(1);
  168.     }
  169.     setuid(orguid);
  170.  
  171.     for (i = nfiles - 1; i > -1; i--) {
  172.  
  173.     if ((p = rindex(Filename[i], '/')) != 0)
  174.         strcpy(dest_file, ++p);
  175.     else
  176.         strcpy(dest_file, Filename[i]);
  177.  
  178.     if (isremote) {
  179.         if (cflag) {
  180.         sprintf(tmp, "S %s %s %s %s %s 644 %s\012", Filename[i],
  181.             rmtuser, user, options, dest_file, n_user);
  182.         } else {
  183.         sprintf(cur_path, "%s/%s/T.%.7s%04x", SPOOLDIR, rmtname,
  184.             rmtname, getseq());
  185.         copy(Filename[i], cur_path);    /* copy file into spool dir */
  186.  
  187.         sprintf(tmp, "S %s %s %s %s %s 644 %s\012", cur_path, rmtuser,
  188.             user, options, dest_file, n_user);
  189.         }
  190.     } else {
  191.         sprintf(tmp, "R %s %s %s %s dummy 777 %s\012", rmtuser,
  192.                 Filename[i], user, options, n_user);
  193.     }
  194.     fwrite(tmp, sizeof(char), strlen(tmp), fcfile);
  195.     }
  196.     fclose(fcfile);
  197. }
  198.  
  199. usage()
  200. {
  201.     fprintf(stderr, "\n\
  202. Usage: uucp [flags] filename [remote!]user\n\
  203.   -c     don't copy source file (forces -b)\n\
  204.   -n     mail remote user when done\n\
  205.   -b     binary file\n");
  206.     exit(0);
  207. }
  208.  
  209. /*
  210.  *      Check the machine name by reading the SYSTEMS file
  211.  *
  212.  *      returns FALSE if not found, else TRUE
  213.  *
  214.  */
  215.  
  216. CheckLegalName(name)
  217. register char *name;
  218. {
  219.     FILE   *fd;
  220.     char    line[132], tmp[16];
  221.  
  222.     if ((fd = fopen(SYSTEMS, "r")) == NULL)
  223.     return (FALSE);
  224.  
  225.     while (mfgets(line, sizeof(line), fd) != NULL) {
  226.  
  227.     sscanf(line, "%s ", tmp);
  228.  
  229.     if (strnucmp(name, tmp, strlen(name)) == 0) {
  230.         fclose(fd);
  231.         return (TRUE);
  232.     }
  233.     }
  234.  
  235.     fclose(fd);
  236.  
  237.     return (FALSE);
  238. }
  239.  
  240. copy(from, to)
  241. char   *from, *to;
  242. {
  243.     register char *cp;
  244.     register int n, ipn, opn;
  245.  
  246.     if ((ipn = open(from, 1)) == ERROR) {
  247.     printf("Unable to open %s .... aborting\n", from);
  248.     exit(0);
  249.     }
  250.     if ((_gs_gfd(ipn, &infdbuf, sizeof(infdbuf))) == ERROR) {
  251.     close(ipn);
  252.     fprintf(stderr, "Can't get file descriptor data from %s .... aborting\n", from);
  253.     exit(1);
  254.     }
  255.     setuid(uucpuid);
  256.     if ((opn = create(to, S_IWRITE | S_ISIZE, S_IREAD | S_IWRITE,
  257.                 _gs_size(ipn))) == ERROR) {
  258.     close(ipn);
  259.     fprintf(stderr, "Unable to copy '%s' to '%s' aborting\n", from, to);
  260.     exit(1);
  261.     }
  262.     setuid(orguid);
  263.  
  264.     printf("Copying %s to %s mode %s\n", from, to,
  265.         bflag ? "binary" : "ascii");
  266.  
  267.     while ((n = read(ipn, copy_buf, BUFSIZE)) > 0) {
  268.     if (!bflag) {
  269.         for (cp = copy_buf; cp < ©_buf[n]; cp++) {
  270.         if (*cp == '\n')
  271.             *cp = '\l';
  272.         }
  273.     }
  274.     if ((write(opn, copy_buf, n)) != n) {
  275.         close(ipn);
  276.         close(opn);
  277.         unlink(to);
  278.         printf("Error# %d while copying %s .... aborting\n", errno, from);
  279.         exit(0);
  280.     }
  281.     }
  282.  
  283.     close(ipn);
  284.     close(opn);
  285. }
  286.  
  287. /* strnucmp
  288.  *  compares s2 to s1 thru null on s2 or at most n chars
  289.  *  returns 0 if equalthru null, or s1 > or < s2 at the nth char
  290.  */
  291.  
  292. strnucmp(s1, s2, n)
  293. register char *s1;
  294. register char *s2;
  295. register int n;
  296. {
  297.     while ((n-- > 0) && (toupper(*s1) == toupper(*s2))) {
  298.     if (*s2++ == 0)
  299.         return (0);
  300.     s1++;
  301.     }
  302.     return ((n < 0) ? 0 : (toupper(*s1) - toupper(*s2)));
  303. }
  304.  
  305. /* EOF */
  306.