home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc190.zip / ckustr.c < prev    next >
C/C++ Source or Header  |  1994-04-23  |  5KB  |  283 lines

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