home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Mac⁄gnuucp 6.14 / source / uumisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-22  |  10.0 KB  |  391 lines  |  [TEXT/KAHL]

  1. /*
  2.  * @(#)uumisc.c 1.6 87/09/29    Copyright 1987 Free Software Foundation, Inc.
  3.  *
  4.  * Copying and use of this program are controlled by the terms of the
  5.  * GNU Emacs General Public License.
  6.  *
  7.  * uumisc.c
  8.  *
  9.  * Miscellaneous uucp variables and routines which are used by several
  10.  * uucp main programs.
  11.  */
  12. #include "includes.h"        /* System include files, system dependent */
  13. #include "uucp.h"        /* Uucp definitions and parameters */
  14.  
  15. char    Myname[MAX_HOST];    /* Our uucp hostname, set from usenet.ctl */
  16. char    Mynamealias[MAX_HOST];    /* Our uucp hostname, set from usenet.ctl */
  17. char    Forwarder[MAX_HOST];
  18. char    host_name[MAX_HOST] = "gnuucp";    /* Other guy's host name */
  19. char    *uuControl;        /* Control file name */
  20. char    who[NAMESIZE] = "-";    /* Who sent us a file */
  21. char    *Spool;            /* Spool directory root name */
  22. char    *Alias;            /* Directory where aliases are kept */
  23. char    *Loginfile;        /* File to log calls to us */
  24. char    *Sysfile;        /* L.sys file */
  25. char    *Userfile;        /* USERFILE */
  26. char    *Pubdir;        /* Public Directory */
  27. char    *Logfile;        /* Logging file */
  28. Boolean AppendLogfile = FALSE;  /* Continously add to log file over sessions */
  29. char    *MLogfile;        /* Logging file for mail */
  30. char    *Username;        /* Username for single user machine */
  31. char    *Timezone;        /* Timezone string ("EDT", "PST"....) */
  32. char    *UUCPlogname;    /* Login name for UUCP */
  33. char    *UUCPpasswd;    /* Password for UUCP */
  34. char    *Mail;            /* Directory where mail is delivered */
  35. long    ByteTimeout;    /* Length of time xgetc waits for a char (in secs) */
  36. long    ConnectWait;    /* Max time we wait after dialing */
  37. long    PacketSize = 64;/* Preferred Packet size for this side */
  38. char     *DialPrefix;    /* Normally ATDT, but can be used to set other parameters */
  39. char     *DialPostfix;    /* Normallty ATH, but can be used to reset other parameters */
  40.  
  41. int    debug = -1;        /* -1 indicates ctl file should set it */
  42. int    ourpid = 0;        /* Our process ID */
  43. int    logfd;            /* file desc of uucp logfile */
  44.  
  45. struct port *ports, *porttail;    /* Chain of serial port definitions */
  46.  
  47. #ifdef VMS
  48. void *malloc();
  49. #else
  50. #ifndef MAC
  51. char    *malloc();
  52. #endif
  53. #endif
  54. char    *gimmestring();        /* Forward declaration */
  55.  
  56. /*
  57.  * Read the control file and grab a few parameters.
  58.  */
  59. extern short apref;
  60.  
  61. read_params(ctl)
  62.     char *ctl;
  63. {
  64.     FILE    *fd;
  65.     char    buf[MAX_CTLLINE];
  66.     register struct port *newport;
  67.     register char *cmd;
  68.     register char    *p;
  69.     short        vRefNumTmp;
  70.     GetVol(0L, &vRefNumTmp);
  71.     SetVol(0L, apref);
  72.     if (ctl) {
  73.         /* Remove any possible setuid if user spec'd a control file */
  74.         /* FIXME: this is system dependent....sigh
  75.         setuid(getuid()); */
  76.     } else {
  77.         /* Use the system default control file */
  78.         ctl = sysdep_control;
  79.     }
  80.     uuControl = ctl;
  81.     if (! (fd = fopen(ctl, "r"))) {
  82.         printf("Can't find configuration file %s\n", ctl);
  83.         SetVol(0L, vRefNumTmp);
  84.           return FAIL;
  85.     }
  86.  
  87.     /* find path to inbound news */
  88.     Spool = "";
  89.     Alias = "";
  90.     Mail = "";            
  91.     Sysfile = "";
  92.     Username = "";
  93.     Userfile = "";
  94.     Pubdir = "";
  95.     Logfile = "";
  96.     Loginfile = "";
  97.     MLogfile = "";
  98.     UUCPlogname = "";
  99.     UUCPpasswd = "";
  100.     DialPrefix = "ATV1DT";
  101.     DialPostfix = "+++\d\d\dATH\r\dats0=0\r";
  102.     Timezone = "";
  103.     strcpy(Myname, "");
  104.     strcpy(Mynamealias, "");
  105.     strcpy(Forwarder, "");
  106.     ByteTimeout = BYTE_TIMEOUT;
  107.     ConnectWait = CONNECT_WAIT;
  108.     while ((long)NULL != (long)fgets(buf, sizeof buf, fd)) {
  109.  
  110.         cmd = strtok(buf, CTL_DELIM);
  111.         if (!cmd) continue;        /* Skip blank lines */
  112.         for (p = cmd; *p; p++)
  113.             if (isupper(*p)) *p = tolower(*p);
  114.  
  115.         /* Now figure out which one it is */
  116.  
  117.         if (strcmp(cmd, "alias") == 0) {
  118.             Alias = gimmefilestring();
  119.         } else if (strcmp(cmd, "spool") == 0) {
  120.             Spool = gimmefilestring();
  121.         } else if (strcmp(cmd, "sysfile") == 0) {
  122.             Sysfile = gimmefilestring();
  123.         } else if (strcmp(cmd, "username") == 0) {
  124.             Username = gimmestring();
  125.         } else if (strcmp(cmd, "uucplogname") == 0) {
  126.             UUCPlogname = gimmestring();
  127.         } else if (strcmp(cmd, "uucppasswd") == 0) {
  128.             UUCPpasswd = gimmestring();
  129.         } else if (strcmp(cmd, "mail") == 0) {
  130.             Mail = gimmefilestring();
  131.         } else if (strcmp(cmd, "userfile") == 0) {
  132.             Userfile = gimmefilestring();
  133.         } else if (strcmp(cmd, "pubdir") == 0) {
  134.             Pubdir = gimmefilestring();
  135.         } else if (strcmp(cmd, "loginfile") == 0) {
  136.             Loginfile = gimmefilestring();
  137.         } else if (strcmp(cmd, "logfile") == 0) {
  138.             Logfile = gimmefilestring();
  139.         } else if (strcmp(cmd, "appendlogfile") == 0) {
  140.             {
  141.                 char *applog;
  142.                 applog = gimmestring();
  143.                 {
  144.                     if (applog != NULL && (applog[0] == 'Y' || applog[0] == 'y'))
  145.                         AppendLogfile = TRUE;
  146.                         else
  147.                         AppendLogfile = FALSE;
  148.                 free(applog);
  149.                 }
  150.         }
  151.         } else if (strcmp(cmd, "timezone") == 0) {
  152.             Timezone = gimmestring();
  153.         } else if (strcmp(cmd, "mlogfile") == 0) {
  154.             MLogfile = gimmefilestring();
  155.         } else if (strcmp(cmd, "dialprefix") == 0) {
  156.             DialPrefix = gimmefilestring();
  157.         } else if (strcmp(cmd, "dialpostfix") == 0) {
  158.             DialPostfix = gimmefilestring();
  159.         } else if (strcmp(cmd, "nodename") == 0) {
  160.             strcpy(Myname, strtok((char *)NULL, CTL_DELIM) ) ;
  161.         } else if (strcmp(cmd, "nodenamealias") == 0) {
  162.             strcpy(Mynamealias, strtok((char *)NULL, CTL_DELIM) ) ;
  163.         } else if (strcmp(cmd, "forwarder") == 0) {
  164.             strcpy(Forwarder, strtok((char *)NULL, CTL_DELIM) ) ;
  165.         }
  166.           else if (strcmp(cmd, "debug") == 0) {
  167.             if (debug < 0) {
  168.                 debug = atoi(strtok((char *)NULL, CTL_DELIM));
  169.             }
  170.         }
  171.           else if (strcmp(cmd, "bytetimeout") == 0) {
  172.                 {
  173.                     ByteTimeout = atoi(strtok((char *)NULL, CTL_DELIM));
  174.             }
  175.         }
  176.           else if (strcmp(cmd, "connectwait") == 0) {
  177.                 {
  178.                     ConnectWait = atoi(strtok((char *)NULL, CTL_DELIM));
  179.             }
  180.         }
  181.           else if (strcmp(cmd, "packetsize") == 0) {
  182.                 {
  183.                     PacketSize = atoi(strtok((char *)NULL, CTL_DELIM));
  184.                     if (PacketSize != 64 && PacketSize != 128)
  185.                         {
  186.                             printf("Bad value for packetsize: %ld.\n", PacketSize);
  187.                             printf("Using 64 instead.\n");
  188.                             PacketSize = 64;
  189.                             }
  190.                     switch (PacketSize)
  191.                         {
  192.                             case 64:
  193.                                 segsiz = 2;
  194.                                 break;
  195.                             case 128:
  196.                                 segsiz = 3;
  197.                                 break;
  198.                             default:
  199.                                 segsiz = 2;
  200.                                 break;
  201.                             }
  202.             }
  203.         }
  204.             else if (strcmp(cmd, "port") == 0) {
  205.             newport = (struct port *)malloc(sizeof *newport);
  206.             p = strtok((char *)NULL, CTL_DELIM);
  207.             strcpy(newport->portname, p);
  208.             p = strtok((char *)NULL, CTL_DELIM);
  209.             strcpy(newport->modemname, p);
  210.             p = strtok((char *)NULL, CTL_DELIM);
  211.             strcpy(newport->devname, p);
  212.             p = strtok((char *)NULL, CTL_DELIM);
  213.             newport->baud = atol(p);
  214.             /* Add it to the end of the chain */
  215.             newport->chain = (struct port *)0;
  216.             if (ports)
  217.                 porttail->chain = newport;
  218.             else
  219.                 ports = newport;
  220.             porttail = newport;
  221.         }
  222.     }
  223.  
  224.     fclose(fd);
  225.     SetVol(0L, vRefNumTmp);
  226.     return SUCCESS;
  227. }
  228.  
  229. /* Scan out next arg as a string, return it or NULL if none there. */
  230. char *
  231. gimmestring()
  232. {
  233.     register char *p, *q;
  234.     p = strtok((char *)NULL, CTL_DELIM);
  235.     if (p == NULL)
  236.         return p;
  237.     q = (char *)malloc(strlen(p)+1);
  238.     strcpy(q, p);
  239.     return q;
  240. }
  241.  
  242. char *
  243. gimmefilestring()
  244. {
  245.     register char *p, *q;
  246.     long len;
  247.     p = strtok((char *)NULL, FILE_DELIM);
  248.     if (p == NULL)
  249.         return p;
  250.     for (;*p == ' ';p++); /* Skip whitespace at beginning */
  251.     len = strlen(p);
  252.     for (;p[len-1] == ' ';p[len-1] = '\0'); /* Skip whitespace at end */
  253.     q = (char *)malloc(len+1);
  254.     strcpy(q, p);
  255.     return q;
  256. }
  257.  
  258. /*
  259.  * Scan out an "argv" from a blank-separated string.
  260.  *
  261.  * Result is argc, or -1 if there are too many args.
  262.  */
  263. int
  264. getargs(string, argv, maxargc)
  265.     char    *string;
  266.     char    **argv;
  267.     int    maxargc;
  268. {
  269.     int    argc = 0;
  270.  
  271.     while (*string == ' ') string++;
  272.  
  273.     for (; *string; ) {
  274.         argv[argc++] = string;        /* Record position */
  275.         if (argc >= maxargc) return -1;
  276.         while (*string && *string != ' ') string++;
  277.         if (*string == '\0') break;
  278.         *string++ = '\0';        /* Null terminate it */
  279.         while (*string == ' ') string++;
  280.     }
  281.  
  282.     argv[argc] = (char *)0;            /* Null after last one */
  283.     return argc;
  284. }
  285.  
  286. /*
  287.  * Log file writing subroutine.
  288.  *
  289.  * Makes incredibly ugly log entries that look *just like* Unix uucp's
  290.  * incredibly ugly log entries.
  291.  *
  292.  * Once we don't care about compatability, we should do this much better.
  293.  */
  294. logit(one, two)
  295.     char *one, *two;
  296. {
  297.     char logbuf[(NAMESIZE*4)+SLOP+50];    /* Temp buffer for logs */
  298.     int len;
  299.     int open_modes;
  300.     if (logfd <= 0) {
  301.         /* Open the log file if not already open */
  302.         if (Logfile == 0 || *Logfile == 0)
  303.             return;        /* If none specified, no logging. */
  304.         open_modes = O_CREAT|O_WRONLY|O_TEXT;
  305.         if (AppendLogfile == TRUE)
  306.             open_modes = open_modes | O_APPEND;
  307.             else
  308.             open_modes = open_modes | O_TRUNC;
  309.         logfd = open(Logfile, open_modes);
  310.         if (logfd < 0) {
  311.             if (DEBUG_LEVEL(0)) perror(Logfile);
  312.             exit(EXIT_ERR);
  313.         }
  314.     }
  315.  
  316.     sprintf(logbuf, "%s %s (%s) %s (%s)\n",
  317.         who, host_name, time_and_pid(), one, two);
  318.  
  319.     DEBUG(0, "%s", logbuf);
  320.  
  321.     len = strlen(logbuf);
  322.     if (len != write(logfd, logbuf, len)) {
  323.         if (DEBUG_LEVEL(0)) {
  324.             fprintf(stderr, "Can't write to log, terminating!\n");
  325.             perror(Logfile);
  326.         }
  327.         exit(EXIT_ERR);        /* Terminate if we can't log */
  328.     }
  329.  
  330. #ifdef LOGCLOSE
  331.     if (close(logfd) != 0) {
  332.         if (DEBUG_LEVEL(0)) {
  333.             fprintf(stderr, "Can't close log, terminating!\n");
  334.             perror(Logfile);
  335.         }
  336.         exit(EXIT_ERR);        /* Terminate if we can't log */
  337.     }
  338.     logfd = -1;
  339. #endif
  340. }
  341.  
  342. mlogit(one, two)
  343.     char *one, *two;
  344. {
  345.     char logbuf[(NAMESIZE*4)+SLOP+50];    /* Temp buffer for logs */
  346.     int len;
  347.  
  348.     if (logfd <= 0) {
  349.         /* Open the log file if not already open */
  350.         if (Logfile == 0 || *Logfile == 0)
  351.             return;        /* If none specified, no logging. */
  352.         logfd = open(MLogfile, O_CREAT|O_WRONLY|O_APPEND|O_TEXT);
  353.         if (logfd < 0) {
  354.             if (DEBUG_LEVEL(0)) perror(MLogfile);
  355.             exit(EXIT_ERR);
  356.         }
  357.     }
  358.  
  359.     sprintf(logbuf, "%s %s (%s) %s (%s)\n",
  360.         who, host_name, time_and_pid(), one, two);
  361.  
  362.     DEBUG(0, "%s", logbuf);
  363.  
  364.     len = strlen(logbuf);
  365.     if (len != write(logfd, logbuf, len)) {
  366.         if (DEBUG_LEVEL(0)) {
  367.             fprintf(stderr, "Can't write to log, terminating!\n");
  368.             perror(MLogfile);
  369.         }
  370.         exit(EXIT_ERR);        /* Terminate if we can't log */
  371.     }
  372.  
  373. #ifdef LOGCLOSE
  374.     if (close(logfd) != 0) {
  375.         if (DEBUG_LEVEL(0)) {
  376.             fprintf(stderr, "Can't close log, terminating!\n");
  377.             perror(MLogfile);
  378.         }
  379.         exit(EXIT_ERR);        /* Terminate if we can't log */
  380.     }
  381.     logfd = -1;
  382. #endif
  383. }
  384.  
  385. void
  386. cuserid(str)
  387. char *str;
  388. {
  389.     strcpy(str, Username);
  390.     }
  391.