home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / uucp_mods.lha.30.12.93 / uucp_mods / uucico / uucp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  4.9 KB  |  268 lines

  1.  
  2. /*
  3.  *   UUCP.c written by William Loftus
  4.  *   Copyright 1988 by William Loftus.    All rights reserved.
  5.  *
  6.  *  $Header: Beta:src/uucp/src/uucico/RCS/uucp.c,v 1.1 90/02/02 11:56:13 dillon Exp Locker: dillon $
  7.  *
  8.  *  Heirarchical Spool Conversion 1 Dec 93,
  9.  *                             Mike J. Bruins. bruins@hal9000.apana.org.au
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "version.h"
  16. #include <log.h>
  17. #include "protos.h"
  18. #include <owndevunit.h>
  19.  
  20. IDENT(".02");
  21.  
  22. char to_buf[128];
  23. char from_buf[128];
  24. char path[128];
  25. char user[16];
  26.  
  27. char command_file[128];
  28. int seq;
  29. struct Library *OwnDevUnitBase;
  30.  
  31. void BuildReceiveFile();
  32. void BuildSendFile();
  33. void read_ctl();
  34. void myexit(void);
  35. char *expand_file();
  36.  
  37. #define TRUE 1
  38. #define FALSE 0
  39.  
  40. main(argc,argv)
  41. int argc;
  42. char **argv;
  43. {
  44.     int to_bang, from_bang;
  45.  
  46.     LogProgram = "uucp";
  47.     LogWho  = GetUserName();
  48.  
  49.     if (argc != 3) {
  50.     printf("Usage: UUCP from_file to_file\n");
  51.     exit(1);
  52.     } else {
  53.     strcat(from_buf, argv[1]);
  54.     strcat(to_buf, argv[2]);
  55.     }
  56.     if ((OwnDevUnitBase = OpenLibrary(ODU_NAME, 0)) == NULL) {
  57.     printf("Unable to open %s\n", ODU_NAME);
  58.     exit(20);
  59.     }
  60.  
  61.     getcwd(path,128);
  62.  
  63.     if (chdir(GetConfigDir(UUSPOOL))) {
  64.     printf("Couldn't change current working directory to %s\n", GetConfigDir(UUSPOOL));
  65.     exit(1);
  66.     }
  67.  
  68.     read_ctl();
  69.  
  70.     seq = GetSequence(2);
  71.     if (seq < 0) {
  72.     chdir(path);
  73.     exit(1);
  74.     }
  75.  
  76.     from_bang = (int)strchr(from_buf,'!');
  77.     to_bang = (int)strchr(to_buf,'!');
  78.  
  79.     if (from_bang && to_bang) {
  80.     printf("Can not specify a remote system in both arguments.\n");
  81.     chdir(path);
  82.     exit(1);
  83.     }
  84.  
  85.     if (from_bang) {
  86.     BuildReceiveFile();
  87.     }
  88.  
  89.     if (to_bang) {
  90.     BuildSendFile();
  91.     }
  92.  
  93.     chdir(path);
  94.     return(0);
  95. }
  96.  
  97. void
  98. myexit()
  99. {
  100.     UnLockFiles();
  101.  
  102.     if (OwnDevUnitBase) {
  103.     CloseLibrary(OwnDevUnitBase);
  104.     OwnDevUnitBase = NULL;
  105.     }
  106. }
  107.  
  108. void
  109. BuildSendFile()
  110. {
  111.     FILE *fp;
  112.     char system_name[32],dir[256];
  113.     int bang;
  114.  
  115.     strcpy(from_buf,expand_file(from_buf));
  116.  
  117.     bang = (int)strchr(to_buf,'!');
  118.     bang = bang - (int)to_buf;
  119.  
  120.     strncpy(system_name,to_buf,bang);
  121.  
  122.     system_name[bang] = '\0';
  123.  
  124.     if (!is_in_L_sys_file(system_name)) {
  125.     printf("System \"%s\" not in L.sys file.\n", system_name);
  126.     chdir(path);
  127.     exit(1);
  128.     }
  129.  
  130.     system_name[7] = '\0';
  131.     strcpy(dir,MakeConfigPath(UUSPOOL,system_name));  /* hierarch - bruins */
  132.     if(dir==NULL || *dir=='\0'){
  133.     ulog(-1,"Can't create spool for \"%s\".",system_name);
  134.     exit(1);
  135.     }
  136.     if(chdir(dir)){
  137.     ulog(-1,"Can't find spool \"%s\", creating.",dir);
  138.     if(mkdir(dir)){
  139.       ulog(-1,"Failed to make spool: \"%s\".",dir);
  140.       exit(1);
  141.     }
  142.     chdir(dir);
  143.     }
  144.  
  145.     sprintf(command_file,"C.%sA%s", system_name, SeqToName(seq++));
  146.     LockFile(command_file);
  147.  
  148.     fp = fopen(command_file,"w");
  149.  
  150.     if (fp) {   /* srcnam desnam who flags temp mode who */
  151.     fprintf(fp,"S %s %s %s %s %s %s %s\n",
  152.         from_buf,
  153.         to_buf + bang + 1,
  154.         user,
  155.         "-c",
  156.         from_buf,
  157.         "0666",
  158.         user
  159.     );
  160.     fclose(fp);
  161.     } else {
  162.     UnLockFile(command_file);
  163.     perror(command_file);
  164.     chdir(path);
  165.     exit(1);
  166.     }
  167.     UnLockFile(command_file);
  168.     printf("Command queue for transfer to %s.\n", system_name);
  169. }
  170.  
  171. void
  172. BuildReceiveFile()
  173. {
  174.     FILE *fp;
  175.     char system_name[32];
  176.     int bang;
  177.  
  178.     strcpy(to_buf,expand_file(to_buf));
  179.  
  180.     bang = (int)strchr(from_buf,'!');
  181.     bang = bang - (int)from_buf;
  182.  
  183.     strncpy(system_name,from_buf,bang);
  184.  
  185.     system_name[bang] = '\0';
  186.  
  187.     if (!is_in_L_sys_file(system_name)) {
  188.     printf("System \"%s\" not in L.sys file.\n", system_name);
  189.     chdir(path);
  190.     exit(1);
  191.     }
  192.  
  193.     system_name[7] = '\0';
  194.  
  195.     sprintf(command_file,"%s/C.%sA%s",         /* hierarch - bruins */
  196.         system_name,system_name, SeqToName(seq++));
  197.  
  198.     LockFile(command_file);
  199.  
  200.     fp = fopen(command_file,"w");
  201.     if (fp) {
  202.     /* srcnam desnam who flags */
  203.     fprintf(fp,"R %s %s %s %s\n",
  204.         from_buf + bang + 1,
  205.         to_buf,
  206.         user,
  207.         "-c"
  208.     );
  209.     fclose(fp);
  210.     } else {
  211.     UnLockFile(command_file);
  212.     perror(command_file);
  213.     chdir(path);
  214.     exit(1);
  215.     }
  216.     UnLockFile(command_file);
  217.     printf("Command queue for transfer from %s.\n", system_name);
  218. }
  219.  
  220. static char name[128];
  221.  
  222. char *
  223. expand_file(file_name)
  224. char *file_name;
  225. {
  226.     char *colon;
  227.  
  228.     colon = strchr(file_name,':');
  229.     if ((colon != (char*)NULL) && (colon != file_name)) {
  230.     return(file_name);
  231.     } else {
  232.     if (path[strlen(path)-1] != ':') {
  233.         sprintf(name,"%s/%s",path,file_name);
  234.         return name;
  235.     } else {
  236.         sprintf(name,"%s%s",path,file_name);
  237.         return name;
  238.     }
  239.     }
  240. }
  241.  
  242. #define CTL_DELIM " \t\r\n"
  243.  
  244. /*
  245.  * Read the control file and grab a few parameters.
  246.  */
  247.  
  248. void
  249. read_ctl()
  250. {
  251.     FILE  *fd;
  252.     static char  buf[128];
  253.  
  254.     if (! (fd = fopen(MakeConfigPath(UULIB, "Config"), "r"))) {
  255.     printf("Can't Find config file.\n");
  256.     chdir(path);
  257.     exit(3);
  258.     }
  259.  
  260.     while (NULL != fgets(buf, sizeof buf, fd)) {
  261.     if (strncmp(buf, "UserName", 8) == 0)
  262.         strcpy(user, strtok(&buf[9], CTL_DELIM) ) ;
  263.     }
  264.     fclose(fd);
  265. }
  266.  
  267.  
  268.