home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / unix / dgrep.arc / CLIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-16  |  5.4 KB  |  243 lines

  1. /************************************************************************\
  2.  *
  3.  *    CLIB.C
  4.  *
  5.  * This file defines some ANSI C and SYSTEM V compatible library
  6.  * routines for BSD UNIX, input buffer allocating and some system
  7.  * dependant routines.
  8. \************************************************************************/
  9.  
  10. #include "system.h"
  11.  
  12. unsigned    maxbuf;
  13. uchar*        buffer;
  14.  
  15. /**********************************************************************
  16.  *
  17.  *    d_free
  18.  */
  19. void d_free(void* ptr)
  20. {
  21.     REG1 void** p = (void**)ptr;
  22.  
  23.     if (*p != NULL) {
  24.         free(*p);
  25.         *p = NULL;
  26.     }
  27. }
  28.  
  29. /**********************************************************************
  30.  *
  31.  *    alloc_buffer
  32.  *
  33.  * Allocate I/O-buffer.
  34.  */
  35. void alloc_buffer(void)
  36. #if defined(__TURBOC__) || defined(M_I86)
  37. {
  38.     buffer = sbrk(0);
  39.     if ((int)buffer == -1)
  40.         error("Input buffer allocation failed", 3);
  41.     /* Calculate largest input buffer that doesn't cause pointer
  42.        wraparound in boyer-moore. */
  43.     maxbuf = ((unsigned)(-((int)buffer)) >> 1) - MAXREGMUST;
  44.     /* Starting from maxbuf, try to allocate input buffer. If allocation
  45.        fails, cut the buffer size to half until we reach minimum
  46.        buffer size. */
  47.     while (maxbuf >= 2*MAXREGMUST) {
  48.         if (buffer == sbrk(maxbuf + 1))
  49.             return;
  50.         maxbuf /= 2;
  51.     }
  52.     error("Out of memory when allocating input buffer", 3);
  53.  
  54. }
  55. #else
  56. {
  57.     maxbuf = MAXBUF;
  58.     buffer = malloc(MAXBUF);
  59.     if (buffer == NULL)
  60.         error("Out of memory when allocating input buffer", 3);
  61. }
  62. #endif    /* defined(__TURBOC__) || defined(M_I86) */
  63.  
  64. #if defined(__TURBOC__)
  65.  
  66. #include    <dos.h>
  67.  
  68. /* time structure for a pseudo-random number */
  69. static struct time    tc_time;
  70.  
  71. /**********************************************************************
  72.  *
  73.  *    get_sec
  74.  */
  75. unsigned get_sec(void)
  76. {
  77.     gettime(&tc_time);
  78.     return (unsigned)tc_time.ti_sec;
  79. }
  80.  
  81. /**********************************************************************
  82.  *
  83.  *    putch
  84.  */
  85. #define putch(c) \
  86. { \
  87.     _DL = (c); \
  88.     _AH = 2; \
  89.     geninterrupt(0x21); \
  90. }
  91.  
  92. /* Dummy function to save little code size */
  93. void cdecl _setenvp (void)
  94. {
  95. }
  96. #endif    /* defined(__TURBOC__) */
  97.  
  98. #if defined(__TURBOC__) || (defined(M_I86) && defined(MSDOS))
  99. /**********************************************************************
  100.  *
  101.  *    putstr
  102.  */
  103. void putstr(REG1 uchar* str)
  104. {
  105.     REG2 int    c;
  106.     
  107.     while ((c = *str++) != '\0') {
  108.         if (c == '\n')
  109.             putch('\r');
  110.         putch(c);
  111.     }
  112. }
  113. #endif    /* defined(__TURBOC__) || (defined(M_I86) && defined(MSDOS)) */
  114.  
  115. /**********************************************************************
  116.  *
  117.  *    error
  118.  */
  119. void error(char* errmsg, int exit_value)
  120. {
  121.     fputs("Error: ", stderr);
  122.     fputs(errmsg, stderr);
  123.     fputs("\n", stderr);
  124.     exit(exit_value);
  125. }
  126.  
  127. /**********************************************************************
  128.  *
  129.  *    assertion_failure
  130.  *
  131.  * Own assert exit routine because many system asserts will link
  132.  * fprintf. We don't want fprintf bacause we have worked hard to keep
  133.  * the program as small as possible.
  134.  */
  135. void assertion_failure(char* file, int line)
  136. {
  137.     char buf[10];
  138.  
  139.     fputs("Internal error: ", stderr);
  140.     fputs(file, stderr);
  141.     fputs(", ", stderr);
  142.     fputs(ultoa((ulong)line, buf, 10), stderr);
  143.     fputs("\n", stderr);
  144.     exit(3);
  145. }
  146.  
  147. #ifdef TOUCH
  148. /**********************************************************************
  149.  *
  150.  *    set_file_time
  151.  */
  152. #if defined(__TURBOC__)
  153. bool set_file_time(char* fname, int h)
  154. {
  155.     struct date        d;
  156.     static struct ftime    ft;
  157.     static int        first_time = TRUE;
  158.     
  159.     if (first_time) {
  160.         first_time = FALSE;
  161.         getdate(&d);
  162.         gettime(&tc_time);
  163.         ft.ft_tsec = tc_time.ti_sec;
  164.         ft.ft_min = tc_time.ti_min;
  165.         ft.ft_hour = tc_time.ti_hour;
  166.         ft.ft_day = d.da_day;
  167.         ft.ft_month = d.da_mon;
  168.         ft.ft_year = d.da_year - 1980;
  169.     }
  170.     return setftime(h, &ft) != -1;
  171. }
  172. #else
  173. bool set_file_time(char* fname, int h)
  174. {
  175.     static int    first_time = TRUE;
  176.     static utime_t    u;
  177.  
  178.     if (first_time) {
  179.         first_time = FALSE;
  180.         u.modtime = time(NULL);
  181.         u.actime = u.modtime;
  182.         }
  183.     return utime(fname, &u) == 0;
  184. }
  185. #endif
  186. #endif    /* TOUCH */
  187.  
  188. #ifdef BSD    /* BSD UNIX lacks mem(set|chr) */
  189. /**********************************************************************
  190.  *
  191.  *    memset
  192.  */
  193. void* memset(void* b, REG3 char ch, REG2 unsigned n)
  194. {
  195.     REG1 uchar* s = (uchar*)b;
  196.     
  197.     while (n--)
  198.         *s++ = ch;
  199.     return b;
  200. }
  201.  
  202. /************************************************************************
  203.  *
  204.  *    memchr
  205.  */
  206. void* memchr(void* b, REG3 char ch, REG2 unsigned n)
  207. {
  208.     REG1 uchar* s = (uchar*)b;
  209.     
  210.     while (n--)
  211.         if (*s++ == ch)
  212.             return s - 1;
  213.     return NULL;
  214. }
  215. #endif /* BSD */
  216.  
  217. /************************************************************************
  218.  *    ultoa
  219.  *
  220.  * Even if the ultoa() were already defined in the library, redefinition
  221.  * does not matter, because this version is excatly compatible with
  222.  * it and this is also well fast enough.
  223.  */
  224. char*    ultoa(ulong n, char* s, int radix)
  225. {
  226.     static char    itoc_xlate[] = {
  227.         '0','1','2','3','4','5','6','7','8','9','A','B',
  228.         'C','D','E','F','G','H','I','J','K','L','M','N',
  229.         'O','P','Q','R','S','T','U','V','W','X','Y','Z'
  230.     };
  231.     register char*    s1;
  232.     register char*    s2;
  233.     
  234.     s1 = s2 = s;
  235.     if ((unsigned)(radix - 2) <= (unsigned)(sizeof(itoc_xlate) - 2))
  236.         do {
  237.             *s1++ = itoc_xlate[n % (ulong)radix];
  238.         } while (n /= (unsigned)radix);
  239.     for (*s1 = '\0'; --s1 > s2; ++s2)
  240.         *s1 ^= *s2 ^= *s1 ^= *s2;    /* what on earth ??? */
  241.     return (s);
  242. }
  243.