home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit5a188 / ckustr.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  5KB  |  270 lines

  1. #include <stdio.h>
  2. #include <sysexits.h>
  3.  
  4. /* string extraction/restoration routines */
  5.  
  6. /*
  7.   STR_FILE must be defined as a quoted string on the cc command line,
  8.   for example:
  9.  
  10.       -DSTR_FILE=\\\"/usr/local/lib/kermit5.sr\\\"
  11.  
  12.   This is the file where the strings go, and where C-Kermit looks for them
  13.   at runtime.
  14. */
  15. #ifdef STR_FILE
  16. char    *StringFile = STR_FILE;
  17. #else
  18. char    *StringFile = "/usr/local/lib/kermit5.sr";
  19. #endif /* STR_FILE */
  20.  
  21. #ifdef STR_CTIMED
  22. char    *Ctimed = STR_CTIMED;
  23. #else
  24. char    *Ctimed = "/usr/lib/ctimed";
  25. #endif
  26.  
  27. extern int errno;
  28. static int strfile = -1, ourpid = 0;
  29.  
  30. #define BUFLEN 256
  31.  
  32. errprep(offset, buf)
  33. unsigned short offset;
  34. char *buf;
  35. {
  36. register int pid = getpid();
  37.  
  38.     if (pid != ourpid) {
  39.         ourpid = pid;
  40.         if (strfile >= 0) {
  41.             close(strfile);
  42.             strfile = -1;
  43.         }
  44.     }
  45.     if (strfile < 0) {
  46.             char *p, *getenv();
  47.         if (p = getenv("KSTR"))
  48.           if (strlen(p))
  49.             StringFile = p;
  50.         strfile = open(StringFile, 0);
  51.         if (strfile < 0) {
  52. oops:
  53.             fprintf(stderr, "Cannot find %s\r\n", StringFile);
  54.             exit(EX_OSFILE);
  55.         }
  56.     }
  57.     if (lseek(strfile, (long) offset, 0) < 0
  58.             || read(strfile, buf, BUFLEN) <= 0)
  59.         goto oops;
  60. }
  61.  
  62. /* extracted string front end for printf() */
  63. /*VARARGS1*/
  64. strprerror(fmt, a, b, c, d, e, f, g, h, i, j, k, l)
  65.     int fmt;
  66. {
  67.     char buf[BUFLEN];
  68.  
  69.     errprep(fmt, buf);
  70.     printf(buf, a, b, c, d, e, f, g, h, i ,j, k, l);
  71. }
  72.  
  73. /* extracted string front end for sprintf() */
  74. /*VARARGS1*/
  75. strsrerror(fmt, obuf, a, b, c, d, e, f, g, h, i, j, k, l)
  76.     int fmt;
  77.     char *obuf;
  78. {
  79.     char buf[BUFLEN];
  80.  
  81.     errprep(fmt, buf);
  82.     sprintf(obuf, buf, a, b, c, d, e, f, g, h, i, j, k, l);
  83. }
  84.  
  85. /* extracted string front end for fprintf() */
  86. /*VARARGS1*/
  87. strfrerror(fmt, fd, a, b, c, d, e, f, g, h, i, j, k, l)
  88.     int fmt, fd;
  89. {
  90.     char buf[BUFLEN];
  91.  
  92.     errprep(fmt, buf);
  93.     fprintf(fd, buf, a, b, c, d, e, f, g, h, i, j, k, l);
  94. }
  95.  
  96. /* extracted string front end for perror() */
  97. strperror(fmt)
  98.     int fmt;
  99. {
  100.     char buf[BUFLEN];
  101.     register int saverr = errno;
  102.  
  103.     errprep(fmt, buf);
  104.     errno = saverr;
  105.     perror(buf);
  106. }
  107.  
  108. perror(str)
  109.     char    *str;
  110.     {
  111.  
  112.     printf("%s: errno %d\n", str, errno);
  113.     }
  114.  
  115. #include    <sys/types.h>
  116. #include    <sys/time.h>
  117.  
  118. #define    SEND_FD    W[1]
  119. #define    RECV_FD    R[0]
  120.  
  121. #define    CTIME    1
  122. #define    ASCTIME    2
  123. #define    TZSET    3
  124. #define    LOCALTIME 4
  125. #define    GMTIME    5
  126. #define    OFFTIME    6
  127.  
  128.     static    int    R[2], W[2], inited;
  129.     static    char    result[26];
  130.     static    struct    tm    tmtmp;
  131.  
  132. char    *
  133. ctime(t)
  134.     time_t    *t;
  135.     {
  136.     u_char    fnc = CTIME;
  137.  
  138.     sewer();
  139.     write(SEND_FD, fnc, sizeof fnc);
  140.     write(SEND_FD, t, sizeof (*t));
  141.     getb(RECV_FD, result, 26);
  142.     return(result);
  143.     }
  144.  
  145. char    *
  146. asctime(tp)
  147.     struct    tm    *tp;
  148.     {
  149.     u_char    fnc = ASCTIME;
  150.  
  151.     sewer();
  152.     write(SEND_FD, &fnc, sizeof fnc);
  153.     write(SEND_FD, tp, sizeof (*tp));
  154.     getb(RECV_FD, result, 26);
  155.     return(result);
  156.     }
  157.  
  158. void
  159. tzset()
  160.     {
  161.     u_char    fnc = TZSET;
  162.  
  163.     sewer();
  164.     write(SEND_FD, &fnc, sizeof fnc);
  165.     }
  166.  
  167. struct    tm *
  168. localtime(tp)
  169.     time_t    *tp;
  170.     {
  171.     u_char    fnc = LOCALTIME;
  172.  
  173.     sewer();
  174.     write(SEND_FD, &fnc, sizeof fnc);
  175.     write(SEND_FD, tp, sizeof (*tp));
  176.     getb(RECV_FD, &tmtmp, sizeof tmtmp);
  177.     getb(RECV_FD, result, 24);
  178.     tmtmp.tm_zone = result;
  179.     return(&tmtmp);
  180.     }
  181.  
  182. struct    tm *
  183. gmtime(tp)
  184.     time_t    *tp;
  185.     {
  186.     u_char    fnc = GMTIME;
  187.  
  188.     sewer();
  189.     write(SEND_FD, &fnc, sizeof fnc);
  190.     write(SEND_FD, tp, sizeof (*tp));
  191.     getb(RECV_FD, &tmtmp, sizeof tmtmp);
  192.     getb(RECV_FD, result, 24);
  193.     tmtmp.tm_zone = result;
  194.     return(&tmtmp);
  195.     }
  196.  
  197. struct    tm *
  198. offtime(clock, offset)
  199.     time_t    *clock;
  200.     long    offset;
  201.     {
  202.     u_char    fnc = OFFTIME;
  203.  
  204.     sewer();
  205.     write(SEND_FD, &fnc, sizeof fnc);
  206.     write(SEND_FD, clock, sizeof (*clock));
  207.     write(SEND_FD, &offset, sizeof offset);
  208.     getb(RECV_FD, &tmtmp, sizeof tmtmp);
  209.     tmtmp.tm_zone = "";
  210.     return(&tmtmp);
  211.     }
  212.  
  213. getb(f, p, n)
  214.     register int f, n;
  215.     register char *p;
  216.     {
  217.     int    i;
  218.  
  219.     while    (n)
  220.         {
  221.         i = read(f, p, n);
  222.         if    (i <= 0)
  223.             return;
  224.         p += i;
  225.         n -= i;
  226.         }
  227.     }
  228.  
  229. sewer()
  230.     {
  231.     register int    pid, ourpid = getpid();
  232.  
  233.     if    (inited == ourpid)
  234.         return;
  235.     if    (inited)
  236.         {
  237.         close(SEND_FD);
  238.         close(RECV_FD);
  239.         }
  240.     pipe(W);
  241.     pipe(R);
  242.     pid = vfork();
  243.     if    (pid == 0)
  244.         {            /* child */
  245.         alarm(0);        /* cancel alarms */
  246.         dup2(W[0], 0);        /* parent write side to our stdin */
  247.         dup2(R[1], 1);        /* parent read side to our stdout */
  248.         close(SEND_FD);        /* copies made, close the... */
  249.         close(RECV_FD);        /* originals now */
  250.         execl(Ctimed, "ctimed", 0);
  251.         _exit(EX_OSFILE);
  252.         }
  253.     if    (pid == -1)
  254.         abort();        /* nothing else really to do */
  255.     close(W[0]);            /* close read side of SEND channel */
  256.     close(R[1]);            /* close write side of RECV channel */
  257.     inited = ourpid;        /* don't do this again in this proc */
  258.     }
  259.  
  260. XXctime()
  261.     {
  262.  
  263.     if    (SEND_FD)
  264.         close(SEND_FD);
  265.     if    (RECV_FD)
  266.         close(RECV_FD);
  267.     SEND_FD = RECV_FD = 0;
  268.     inited = 0;
  269.     }
  270.