home *** CD-ROM | disk | FTP | other *** search
/ ftp.eri.u-tokyo.ac.jp / 2014.03.ftp.eri.u-tokyo.ac.jp.zip / ftp.eri.u-tokyo.ac.jp / pub / DOS / tools / kmtar.lzh / MISC.C < prev    next >
C/C++ Source or Header  |  1991-02-17  |  5KB  |  324 lines

  1. /*
  2.  * misc - miscellaneous functions
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <time.h>
  9. #include <ctype.h>
  10.  
  11. #include "defs.h"
  12.  
  13. #include "main.h"
  14. #include "misc.h"
  15.  
  16.  
  17.  
  18. /*
  19.  * Report error, s: object msg: reason
  20.  */
  21. global void error(char *s, char *msg)
  22. {
  23.     fprintf(stderr, "%s: ", Progname);
  24.     if (msg == NULL) {
  25.         if (errno == 0)
  26.             fprintf(stderr, "%s: Device not ready\n", s);
  27.         else
  28.             perror(s);
  29.     } else {
  30.         if (s)
  31.             fprintf(stderr, "%s: ", s);
  32.         fprintf(stderr, "%s\n", msg);
  33.     }
  34.     Exitcode = 1;
  35. }
  36.  
  37.  
  38.  
  39. /*
  40.  * Report error and terminate program
  41.  */
  42. global void fatal(char *s, char *msg)
  43. {
  44.     error(s, msg);
  45.     exit(1);
  46. }
  47.  
  48.  
  49.  
  50.  
  51. /*
  52.  * Return true if string t's head is same as string p
  53.  */
  54. global int strmatch(char *t, char *p)
  55. {
  56.     return (strncmp(t, p, strlen(p)) == 0);
  57. }
  58.  
  59.  
  60.  
  61. int    strcmpl(char *s, char *t)
  62. {
  63.     for (; *s && tolower(*s) == tolower(*t); s++, t++)
  64.         ;
  65.     return (*s - *t);    
  66. }
  67.  
  68.  
  69.  
  70. FILE *fopenpath(char *file)
  71. {
  72.     FILE *fp;
  73.     char *p, *d;
  74.     char buf[100];
  75.  
  76.     if ((p = getenv("TAR")) != NULL) {
  77.         strcat(addsl(strcpy(buf, p)), file);
  78.         if ((fp = fopen(buf, "r")) != NULL)
  79.             return (fp);
  80.     }
  81.     if ((fp = fopen(file, "r")) != NULL)
  82.         return (fp);
  83.     if ((p = getenv("PATH")) != NULL) {
  84.         while (*p) {
  85.             d = buf;
  86.             while (*p && *p != ';')
  87.                 *d++ = *p++;
  88.             *d = '\0';
  89.             strcat(addsl(buf), file);
  90.             if ((fp = fopen(buf, "r")) != NULL)
  91.                 return (fp);
  92.             if (*p == ';')
  93.                 p++;
  94.         }
  95.     }
  96.     return (NULL);
  97. }
  98.  
  99.  
  100.  
  101. /*
  102.  * Return device-description string
  103.  */
  104. global char *get_dev_alias(char *device)
  105. {
  106.     FILE *fp;
  107.     char *p, *s;
  108.     char line[201];
  109.  
  110.     if ((fp = fopenpath("_tar")) == NULL)
  111.         return (NULL);
  112.     while (fgets(line, 200, fp)) {
  113.         for (p = line; isspace(*p); p++)
  114.             ;
  115.         if (*p == '#' || *p == '\0')
  116.             continue;
  117.         for (s = p; *s && !isspace(*s); s++)
  118.             ;
  119.         if (*s == '\0')
  120.             continue;
  121.         *s++ = '\0';
  122.         if (device == NULL || strcmpl(p, device) == 0) {
  123.             while (isspace(*s))
  124.                 s++;
  125.             for (p = s; *p && !isspace(*p); p++)
  126.                 ;
  127.             *p = '\0';
  128.             if ((p = malloc(strlen(s) + 1)) == NULL)
  129.                 fatal(NULL, "Out of memory");
  130.             strcpy(p, s);
  131.             fclose(fp);
  132.             return (p);
  133.         }
  134.     }
  135.     fclose(fp);
  136.     return (NULL);
  137. }
  138.  
  139.  
  140.  
  141. /*
  142.  * Append '/' to string buf
  143.  */
  144. global char *addsl(char *buf)
  145. {
  146.     if (buf[0]) {
  147.         switch(buf[strlen(buf) - 1]) {
  148.         case '/': case '\\': case ':':
  149.             break;
  150.         default:
  151.             strcat(buf, "/");
  152.             break;
  153.         }
  154.     }
  155.     return (buf);
  156. }
  157.  
  158.  
  159.  
  160. /*
  161.  * Read with ASCII conversion
  162.  */
  163. global int read_a(int fd, char *buff, int size)
  164. {
  165.     int n;
  166.     char *p, *q, *t;
  167.  
  168.     do {
  169.         n = read(fd, buff, size);
  170.         if (n == 0 || !Aflag)
  171.             break;
  172.         t = buff + n;
  173.         for (q = p = buff; p < t; ) {
  174.             char *s;
  175.  
  176.             if (*p == '\r')
  177.                 p++;
  178.             s = memchr(p, '\r', t - p);
  179.             if (s == NULL)
  180.                 s = t;
  181.             if (p != q)
  182.                 memcpy(q, p, s - p);
  183.             q += s - p;
  184.             p = s;
  185.         }
  186.         n = q - buff;
  187.     } while (n == 0);
  188.     return (n);
  189. }
  190.  
  191.  
  192.  
  193. /*
  194.  * Write with ASCII conversion
  195.  */
  196. global int write_a(int fd, char *buff, int size)
  197. {
  198.     char *q, *p, *s, *t;
  199.     int n, m;
  200.     char buff2[512];
  201.  
  202.     if (!Aflag)
  203.         return (write(fd, buff, size));
  204.     m = 0;
  205.     t = buff + size;
  206.     q = buff2;
  207.     for (s = p = buff; p < t; ) {
  208.         s = memchr(s, '\n', t - p);
  209.         if (s == NULL)
  210.             s = t;
  211.         do {
  212.             n = s - p;
  213.             if (q + n > buff2 + sizeof buff2)
  214.                 n = buff2 + sizeof buff2 - q;
  215.             memcpy(q, p, n);
  216.             q += n;
  217.             p += n;
  218.             if (q >= buff2 + sizeof buff2) {
  219.                 m += write(fd, buff2, sizeof buff2);
  220.                 q = buff2;
  221.             }
  222.         } while (p < s);
  223.         if (s < t)
  224.             *q++ = '\r';
  225.         s++;
  226.     }
  227.     if (q > buff2)
  228.         m += write(fd, buff2, q - buff2);
  229.     return (m);
  230. }
  231.  
  232.  
  233.  
  234. /*
  235.  * Return 1 if contents of file fd and *buff are different
  236.  */
  237. global int compare_a(int fd, char *buff, int size)
  238. {
  239.     char buff2[512];
  240.  
  241.     while (size > 0) {
  242.         int n = sizeof buff2;
  243.         if (n > size)
  244.             n = size;
  245.         n = read_a(fd, buff2, n);
  246.         if (n == 0 || memcmp(buff, buff2, n) != 0)
  247.             return 1;
  248.         size -= n;
  249.         buff += n;
  250.     }
  251.     return 0;
  252. }
  253.  
  254.  
  255.  
  256. /*
  257.  * Return real size of file fd
  258.  */
  259. global long realsize(int fd)
  260. {
  261.     char buff[512];
  262.     int n;
  263.     long size;
  264.     char *p, *t;
  265.  
  266.     size = 0;
  267.     while ((n = read(fd, buff, sizeof buff)) > 0) {
  268.         t = buff + n;
  269.         for (p = buff; p = memchr(p, '\r', t - p); p++)
  270.             n--;
  271.         size += n;
  272.     }
  273.     lseek(fd, 0L, 0);
  274.     return (size);
  275. }
  276.  
  277.  
  278.  
  279. /*
  280.  * Convert date-string s (mm/dd[/yyyy] [hh:mm[:ss]]) to time_t type
  281.  */
  282. global time_t getdate(char *s)
  283. {
  284.     time_t now;
  285.     struct tm t, *nowp;
  286.  
  287.     memset(&t, 0, sizeof t);
  288.     now = time(NULL);
  289.     nowp = localtime(&now);
  290.     t.tm_mon = strtol(s, &s, 10) - 1;
  291.     if (*s++ != '/')
  292.         return (-1L);
  293.     t.tm_mday = strtol(s, &s, 10);
  294.     if (*s == '/') {
  295.         s++;
  296.         t.tm_year = strtol(s, &s, 10);
  297.         if (t.tm_year >= 1900)
  298.             t.tm_year -= 1900;
  299.     } else {
  300.         t.tm_year = nowp->tm_year;
  301.         if (t.tm_mon > nowp->tm_mon
  302.            || t.tm_mon == nowp->tm_mon && t.tm_mday > nowp->tm_mday)
  303.             t.tm_year--;
  304.     }
  305.     while (isspace(*s))
  306.         s++;
  307.     if (*s) {
  308.         t.tm_hour = strtol(s, &s, 10);
  309.         if (*s != ':')
  310.             return (-1L);
  311.         s++;
  312.         t.tm_min = strtol(s, &s, 10);
  313.         if (*s == ':') {
  314.             s++;
  315.             t.tm_sec = strtol(s, &s, 10);
  316.         }
  317.     }
  318.     while (isspace(*s))
  319.         s++;
  320.     if (*s)
  321.         return (-1L);
  322.     return (mktime(&t));
  323. }
  324.