home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / OLS / Os2 / LHA2P205 / LHA2P205.LZH / lha2-2.05pre / source.lzh / src / util.c < prev   
C/C++ Source or Header  |  1996-02-25  |  4KB  |  283 lines

  1. /*
  2.  * util.c --- utility routines
  3.  *   Copyright (C) 1988-1992, Haruyasu YOSHIZAKI
  4.  *   Copyright (C) 1991-1996, Satoshi HIRAMATSU (OS/2 HPFS version)
  5.  *
  6.  * $Log$
  7.  */
  8.  
  9.  
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <stdio.h>
  13. #include <io.h>
  14. #include <string.h>
  15. #include <conio.h>
  16. #include <ctype.h>
  17. #include <time.h>
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. #include <fcntl.h>
  21. #include "port2.h"
  22. #include "typedef.h"
  23. #include "lh.h"
  24. #include "intrface.h"
  25. #include "slidehuf.h"
  26. #include "errmes.h"
  27. #include "disp.h"
  28.  
  29.  
  30. uchar pathdelim = '\\';
  31. uchar swchar = '/';
  32.  
  33.  
  34. /*
  35.  * convert path delimiter
  36.  *
  37.  * returns *filename
  38.  */
  39. char *
  40. convdelim(char *path, char delim)
  41. {
  42.   uchar c;
  43.   uchar *p;
  44.   int kflg;
  45.  
  46.   kflg = 0;
  47.   for(p = path; (c = *p) != 0; p++)
  48.     {
  49.       if(kflg)
  50.     kflg = 0;
  51.       else if(iskanji(c))
  52.     kflg = 1;
  53.       else if(c == '\\' || c == DELIM || c == DELIM2)
  54.     {
  55.       *p = delim;
  56.       path = p + 1;
  57.     }
  58.     }
  59.  
  60.   return path;
  61. }
  62.  
  63.  
  64. char *
  65. getfilename(char *path)
  66. {
  67.   char *q;
  68.  
  69.   if((q = strrchr(path, DELIM)) != NULL || (q = strchr(path, ':')) != NULL)
  70.     return q + 1;
  71.   else
  72.     return path;
  73. }
  74.  
  75.  
  76. /* get back to parent directory */
  77. char *
  78. backpath(char *p)
  79. {
  80.   char *q;
  81.  
  82.   q = getfilename(p);
  83.   *q = '\0';
  84.  
  85.   return q;
  86. }
  87.  
  88.  
  89. /* ask 'Y' or 'N' */
  90. int
  91. getyn(void)
  92. {
  93.   int yn;
  94.  
  95.   do
  96.     {
  97.       yn = getch();
  98.       yn = toupper(yn);
  99.       if(yn == '\x03')
  100.     error(CTRLBRK, NULL);
  101.     }
  102.   while(yn != 'Y' && yn != 'N');
  103.   eprintf("%c\n", yn);
  104.  
  105.   return yn;
  106. }
  107.  
  108.  
  109. void
  110. getswchar(void)
  111. {
  112.   swchar = '-';
  113.   pathdelim = '/';
  114. }
  115.  
  116.  
  117. time_t
  118. dos2unix(struct ftime *ft)
  119. {
  120.   struct tm tm;
  121.  
  122.   tm.tm_sec = ft -> ft_tsec * 2;
  123.   tm.tm_min = ft -> ft_min;
  124.   tm.tm_hour = ft -> ft_hour;
  125.   tm.tm_mday = ft -> ft_day;
  126.   tm.tm_mon = ft -> ft_month - 1;
  127.   tm.tm_year = ft -> ft_year + 80;
  128. #ifdef __API16__
  129.   tm.tm_isdst = timezone;
  130. #else
  131.   tm.tm_isdst = _timezone;
  132. #endif
  133.  
  134.   return mktime(&tm);
  135. }
  136.  
  137.  
  138. struct ftime *
  139. unix2dos(time_t utc)
  140. {
  141.   static struct ftime ft;
  142.   struct tm *tm;
  143.  
  144.   tm = localtime(&utc);
  145.   ft.ft_tsec = tm -> tm_sec / 2;
  146.   ft.ft_min = tm -> tm_min ;
  147.   ft.ft_hour = tm -> tm_hour;
  148.   ft.ft_day = tm -> tm_mday;
  149.   ft.ft_month = tm -> tm_mon + 1;
  150.   ft.ft_year = tm -> tm_year - 80;
  151.  
  152.   return &ft;
  153. }
  154.  
  155.  
  156. /* ratio * 1000 */
  157. int
  158. ratio(ulong a, ulong b, int ord)
  159. {
  160.   int i;
  161.  
  162.   for(i = 0; i < ord && a < 0x19999999; i++)
  163.     a *= 10;            /* while not overflow */
  164.   /* upto 10^ord times */
  165.  
  166.   for(; i < ord; i++)        /* the case of overflow */
  167.     b /= 10;
  168.  
  169.   if(b == 0)            /* if diviser == 0 */
  170.     return 0;
  171.   a += b / 2;            /* for round up */
  172.  
  173.   return (a / b);        /* return (a * 1000 / b) */
  174. }
  175.  
  176.  
  177. hword
  178. copyfile(FILE *f1, FILE *f2, long size, int crc_flg)
  179. {
  180.   int xsize;
  181.   int dicsiz;
  182.   hword crc = 0;
  183.   int read;
  184.  
  185.   if(f2)
  186.     fflush(f2);
  187.   dicsiz = 1 << interface.dicbit;
  188.   dispflg = crc_flg;
  189.   while(size > 0)
  190.     {
  191.       xsize = (size > TEXT_SIZE) ? TEXT_SIZE : size;
  192.       if((read = fread(text, 1, xsize, f1)) != xsize)
  193.     fileerror(RDERR, f1);
  194.       fwrite_crc(text, xsize, f2, &crc);
  195.       size -= xsize;
  196.       if(crc_flg)
  197.     {
  198.       while(xsize > 0)
  199.         {
  200.           if(!flg_n)
  201.         dispmark('c');
  202.           xsize -= dicsiz;
  203.         }
  204.     }
  205.     }
  206.  
  207.   return crc;
  208. }
  209.  
  210.  
  211. #ifndef __DEBUG__
  212. void *
  213. e_malloc(size_t size)
  214. {
  215.   void *p;
  216.  
  217.   if((p = malloc(size)) == NULL)
  218.     error(MEMOVRERR, NULL);
  219.  
  220.   return p;
  221. }
  222.  
  223.  
  224. void *
  225. e_realloc(void *buf, size_t size)
  226. {
  227.   void *p;
  228.  
  229.   if((p = realloc(buf, size)) == NULL)
  230.     error(MEMOVRERR, NULL);
  231.  
  232.   return p;
  233. }
  234. #endif
  235.  
  236.  
  237. FILE *
  238. myeopen(const char *path, const char *mode, char *errmes)
  239. {
  240.   FILE *f;
  241.   int fd;
  242.  
  243.   f = NULL;
  244.   if(*mode != 'w')
  245.     f = fopen(path, mode);
  246.   else
  247.     {
  248.       fd = open(path, O_CREAT|O_TRUNC|O_BINARY|O_RDWR, S_IREAD|S_IWRITE);
  249.       if(fd >= 0)
  250.     f = fdopen(fd, mode);
  251.     }
  252.   if(f == NULL && errmes != NULL)
  253.     error(errmes, path);
  254.  
  255.   return f;
  256. }
  257.  
  258.  
  259. FILE *
  260. mywopen(const char *path, char *errmes)
  261. {
  262.   return myeopen(path, "wb", errmes);
  263. }
  264.  
  265.  
  266. FILE *
  267. myropen(const char *path)
  268. {
  269.   return myeopen(path, "rb", NOFILEERR);
  270. }
  271.  
  272.  
  273. void
  274. eprintf(char *fmt, ...)
  275. {
  276.   va_list p;
  277.  
  278.   va_start(p, fmt);
  279.   vfprintf(stderr, fmt, p);
  280.   va_end(p);
  281.   fflush(stderr);
  282. }
  283.