home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / uucp / uucp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-01-10  |  5.2 KB  |  256 lines

  1. #include "uucp.h"
  2. #include "uucpdefs.h"
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5.  
  6. /*
  7.  *    uucp
  8.  */
  9.  
  10. int Uid;
  11. int Startjob = 1;
  12. char Path[100], Optns[10];
  13. char Grade = 'n';
  14. int Copy = 1;
  15.  
  16. main(argc, argv)
  17. char *argv[];
  18. {
  19.     int ret;
  20.     char *sysfile1, *sysfile2, *cp;
  21.     char file1[MAXFULLNAME], file2[MAXFULLNAME];
  22.     extern char *index();
  23.  
  24.     Optns[0] = '-';
  25.     Optns[1] = '\0';
  26.     while(argc>1 && argv[1][0] == '-'){
  27.         switch(argv[1][1]){
  28.         case 'c':
  29.             Copy = 0;
  30.             break;
  31.         case 'd':
  32.             strcat(Optns, "d");
  33.             break;
  34.         case 'e':
  35.             fprintf(stderr, "-e option removed\n");
  36.             break;
  37.         case 'g':
  38.             Grade = argv[1][2]; break;
  39.         case 'm':
  40.             strcat(Optns, "m");
  41.             break;
  42.         case 'r':
  43.             Startjob = 0;
  44.             break;
  45.         case 's':
  46.             Spool = &argv[1][2]; break;
  47.         case 'x':
  48.             Debug = atoi(&argv[1][2]);
  49.             if (Debug <= 0)
  50.                 Debug = 1;
  51.             break;
  52.         default:
  53.             printf("unknown flag %s\n", argv[1]); break;
  54.         }
  55.         --argc;  argv++;
  56.     }
  57.     DEBUG(4, "\n\n** %s **\n", "START");
  58.     ret = gwd(Wrkdir);
  59.     ASSERT(ret == 0, "GWD FAILED %d", ret);
  60.     chdir(Spool);
  61.  
  62.     Uid = getuid();
  63.     ret = guinfo(Uid, User, Path);
  64.     ASSERT(ret == 0, "CAN NOT FIND UID %d\n", Uid);
  65.     DEBUG(4, "UID %d, ", Uid);
  66.     DEBUG(4, "User %s,", User);
  67.     DEBUG(4, "PATH %s\n", Path);
  68.     if (argc < 3) {
  69.         fprintf(stderr, "usage uucp from ... to\n");
  70.         cleanup(0);
  71.     }
  72.  
  73.  
  74.     /*  set up "to" system and file names  */
  75.     if ((cp = index(argv[argc - 1], '!')) != NULL) {
  76.         sysfile2 = argv[argc - 1];
  77.         *cp = '\0';
  78.         if (*sysfile2 == '\0')
  79.             sysfile2 = Myname;
  80.         else
  81.             sprintf(Rmtname, "%.7s", sysfile2);
  82.         if (versys(sysfile2) != 0) {
  83.             fprintf(stderr, "bad system name: %s\n", sysfile2);
  84.             cleanup(0);
  85.         }
  86.         strcpy(file2, cp + 1);
  87.     }
  88.     else {
  89.         sysfile2 = Myname;
  90.         strcpy(file2, argv[argc - 1]);
  91.     }
  92.  
  93.  
  94.     /*  do each from argument  */
  95.     while (argc > 2) {
  96.         if ((cp = index(argv[1], '!')) != NULL) {
  97.             sysfile1 = argv[1];
  98.             *cp = '\0';
  99.             if (*sysfile1 == '\0')
  100.                 sysfile1 = Myname;
  101.             else
  102.                 sprintf(Rmtname, "%.7s", sysfile1);
  103.             if (versys(sysfile1) != 0) {
  104.                 fprintf(stderr, "bad system name: %s\n", sysfile1);
  105.                 cleanup(0);
  106.             }
  107.             strcpy(file1, cp + 1);
  108.         }
  109.         else {
  110.             sysfile1 = Myname;
  111.             strcpy(file1, argv[1]);
  112.         }
  113.         DEBUG(4, "file1 - %s\n", file1);
  114.         copy(sysfile1, file1, sysfile2, file2);
  115.         --argc;
  116.         argv++;
  117.     }
  118.  
  119.     if (Startjob)
  120.         xuucico("");
  121.     cleanup(0);
  122. }
  123.  
  124. cleanup(code)
  125. int code;
  126. {
  127.     logcls();
  128.     rmlock(NULL);
  129.     exit(code);
  130. }
  131.  
  132.  
  133. /***
  134.  *    copy(s1, f1, s2, f2)    generate copy files
  135.  *    char *s1, *f1, *s2, *f2;
  136.  *
  137.  *    return codes 0  |  FAIL
  138.  */
  139.  
  140. copy(s1, f1, s2, f2)
  141. char *s1, *f1, *s2, *f2;
  142. {
  143.     int ret, type;
  144.     struct stat stbuf;
  145.     char cfile[NAMESIZE], dfile[NAMESIZE];
  146.     char file1[MAXFULLNAME], file2[MAXFULLNAME];
  147.     FILE *cfp;
  148.     extern char *index();
  149.  
  150.     type = 0;
  151.     strcpy(file1, f1);
  152.     strcpy(file2, f2);
  153.     if (strcmp(s1, Myname) != SAME)
  154.         type = 1;
  155.     if (strcmp(s2, Myname) != SAME)
  156.         type += 2;
  157.     if (type & 01)
  158.         if ((index(file1, '*') != NULL
  159.           || index(file1, '?') != NULL
  160.           || index(file1, '[') != NULL))
  161.             type = 4;
  162.     switch (type) {
  163.     case 0:
  164.         /* all work here */
  165.         DEBUG(4, "all work here %d\n", type);
  166.         expfile(file1);
  167.         expfile(file2);
  168.         if (chkpth(User, "", file1) != 0
  169.         || chkpth(User, "", file2) != 0) {
  170.             fprintf(stderr, "permission denied\n");
  171.             cleanup(1);
  172.         }
  173.         xcp(file1, file2);
  174.         logent("WORK HERE", "DONE");
  175.         return(0);
  176.     case 1:
  177.         /* receive file */
  178.         DEBUG(4, "receive file - %d\n", type);
  179.         if (file1[0] != '~')
  180.             expfile(file1);
  181.         expfile(file2);
  182.         if (chkpth(User, "", file2) != 0) {
  183.             fprintf(stderr, "permission denied\n");
  184.             return(FAIL);
  185.         }
  186.         gename(CMDPRE, s1, Grade, cfile);
  187.         strcpy(dfile, cfile);
  188.         dfile[0] = DATAPRE;
  189.         cfp = fopen(cfile, "w");
  190.         ASSERT(cfp != NULL, "CAN NOT OPEN %s", cfile);
  191.         fprintf(cfp, "R %s %s %s %s\n", file1, file2, User, Optns);
  192.         fclose(cfp);
  193.         break;
  194.     case 2:
  195.         /* send file */
  196.         expfile(file1);
  197.         if (file2[0] != '~')
  198.             expfile(file2);
  199.         DEBUG(4, "send file - %d\n", type);
  200.  
  201.         gename(CMDPRE, s2, Grade, cfile);
  202.         strcpy(dfile, cfile);
  203.         dfile[0] = DATAPRE;
  204.         if (chkpth(User, "", file1) != 0) {
  205.             fprintf(stderr, "permission denied %s\n", file1);
  206.             return(FAIL);
  207.         }
  208.         ret = stat(file1, &stbuf);
  209.         if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
  210.             fprintf(stderr, "directory name illegal - %s\n",
  211.               file1);
  212.             return(FAIL);
  213.         }
  214.         if (Copy) {
  215.             if (xcp(file1, dfile) != 0) {
  216.                 fprintf(stderr, "can't copy %s\n", file1);
  217.                 return(FAIL);
  218.             }
  219.             chmod(dfile, 0666);
  220.         }
  221.         else {
  222.             if ((stbuf.st_mode & 04) == 0) {
  223.                 fprintf(stderr, "uucico can't access %s (-c specified)\n", file1);
  224.                 return(FAIL);
  225.             }
  226.         }
  227.         cfp = fopen(cfile, "w");
  228.         ASSERT(cfp != NULL, "CAN NOT OPEN %s", cfile);
  229.         chmod(cfile, 0200);
  230.         fprintf(cfp, "S %s %s %s %s %s %o\n", file1, file2,
  231.             User, Optns, dfile, stbuf.st_mode & 0777);
  232.         fclose(cfp);
  233.         chmod(cfile, 0666);
  234.         break;
  235.     case 3:
  236.     case 4:
  237.         /*  send uucp command for execution on s2  */
  238.         DEBUG(4, "send uucp command - %d\n", type);
  239.         if (strcmp(s2,  Myname) == SAME) {
  240.             expfile(file2);
  241.             if (chkpth(User, "", file2) != 0) {
  242.                 fprintf(stderr, "permission denied\n");
  243.                 return(FAIL);
  244.             }
  245.         }
  246.         gename(CMDPRE, s1, Grade, cfile);
  247.         cfp = fopen(cfile, "w");
  248.         ASSERT(cfp != NULL, "CAN NOT OPEN %s", cfile);
  249.         fprintf(cfp, "X %s %s!%s\n", file1, s2, file2);
  250.         fclose(cfp);
  251.         break;
  252.     }
  253.     logent(cfile, "QUEUED");
  254.     return(0);
  255. }
  256.