?login_element?

Subversion Repositories NedoOS

Rev

Rev 630 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.         STDLIB3.C -- for BDS C v1.6 --  1/86
  3.         Copyright (c) 1982, 1986 by BD Software, Inc.
  4.  
  5.         The files STDLIB1.C, STDLIB2.C and STDLIB3.C contain the source
  6.         listings for all functions present in the DEFF.CRL library object
  7.         file. (Note: DEFF2.CRL contains the .CSM-coded portions of the
  8.         library.)
  9.  
  10.         STDLIB3.C contains mainly string, character, and storage allocation
  11.         functions:
  12.  
  13.         strcat  strcmp  strcpy  strlen
  14.         isalpha isupper islower isdigit isspace toupper tolower
  15.         atoi
  16.         qsort
  17.         initw   initb   initptr getval
  18.         alloc   free
  19.         swapin
  20.         abs     max     min
  21. */
  22.  
  23.  
  24. #include <stdio.h>
  25.  
  26. #define MAX_QSORT_WIDTH 513     /* Largest sized object "qsort" can sort  */
  27.  
  28. /*
  29.         String functions:
  30. */
  31.  
  32. char *strcat(s1,s2)
  33. char *s1, *s2;
  34. {
  35.         char *temp; temp=s1;
  36.         while(*s1) s1++;
  37.         do *s1++ = *s2; while (*s2++);
  38.         return temp;
  39. }
  40.  
  41.  
  42. int strcmp(s1, s2)
  43. char *s1, *s2;
  44. {
  45.         while (*s1 == *s2++)
  46.                 if (*s1++ == '\0')
  47.                         return 0;
  48.         return (*s1 - *--s2);
  49. }
  50.  
  51.  
  52. char *strcpy(s1,s2)
  53. char *s1, *s2;
  54. {
  55.         char *temp; temp=s1;
  56.         while (*s1++ = *s2++);
  57.         return temp;
  58. }
  59.  
  60.  
  61. int strlen(s)
  62. char *s;
  63. {
  64.         int len;
  65.         len=0;
  66.         while (*s++) len++;
  67.         return len;
  68. }
  69.  
  70.  
  71. /*
  72.         Character diddling functions:
  73. */
  74.  
  75. int isalpha(c)
  76. char c;
  77. {
  78.         return isupper(c) || islower(c);
  79. }
  80.  
  81.  
  82. int isupper(c)
  83. char c;
  84. {
  85.         return c>='A' && c<='Z';
  86. }
  87.  
  88.  
  89. int islower(c)
  90. char c;
  91. {
  92.         return c>='a' && c<='z';
  93. }
  94.  
  95.  
  96. int isdigit(c)
  97. char c;
  98. {
  99.         return c>='0' && c<='9';
  100. }
  101.  
  102.  
  103. int isspace(c)
  104. char c;
  105. {
  106.         return c==' ' || c=='\t' || c=='\n';
  107. }
  108.  
  109.  
  110. char toupper(c)
  111. char c;
  112. {
  113.         return islower(c) ? c-32 : c;
  114. }
  115.  
  116.  
  117. char tolower(c)
  118. char c;
  119. {
  120.         return isupper(c) ? c+32 : c;
  121. }
  122.  
  123.  
  124. int atoi(n)
  125. char *n;
  126. {
  127.         int val;
  128.         char c;
  129.         int sign;
  130.         val=0;
  131.         sign=1;
  132.         while ((c = *n) == '\t' || c== ' ') ++n;
  133.         if (c== '-') {sign = -1; n++;}
  134.         while (  isdigit(c = *n++)) val = val * 10 + c - '0';
  135.         return sign*val;
  136. }
  137.  
  138. /*
  139.         Generalized Sort function:
  140. */
  141.  
  142. qsort(base, nel, width, compar)
  143. char *base; int (*compar)();
  144. unsigned width,nel;
  145. {       int i, j;
  146.         unsigned gap, ngap, t1;
  147.         int jd, t2;
  148.  
  149.         t1 = nel * width;
  150.         for (ngap = nel / 2; ngap > 0; ngap /= 2) {
  151.            gap = ngap * width;
  152.            t2 = gap + width;
  153.            jd = base + gap;
  154.            for (i = t2; i <= t1; i += width)
  155.               for (j =  i - t2; j >= 0; j -= gap) {
  156.                 if ((*compar)(base+j, jd+j) <=0) break;
  157.                          _swp(width, base+j, jd+j);
  158.               }
  159.         }
  160. }
  161.  
  162. _swp(w,a,b)
  163. char *a,*b;
  164. unsigned w;
  165. {
  166.         char swapbuf[MAX_QSORT_WIDTH];
  167.         movmem(a,swapbuf,w);
  168.         movmem(b,a,w);
  169.         movmem(swapbuf,b,w);
  170. }
  171.  
  172.  
  173. /*
  174.         Initialization functions
  175. */
  176.  
  177.  
  178. initw(int_ptr,string)
  179. int *int_ptr;
  180. char *string;
  181. {
  182.         int n;
  183.         while ((n = getval(&string)) != -32760)
  184.                 *int_ptr++ = n;
  185. }
  186.  
  187. initb(char_ptr, string)
  188. char *char_ptr, *string;
  189. {
  190.         int n;
  191.         while ((n = getval(&string)) != -32760) *char_ptr++ = n;
  192. }
  193.  
  194. initptr(str_ptr, first_arg)
  195. char **str_ptr, **first_arg;
  196. {
  197.         char **arg_ptr;
  198.  
  199.         for (arg_ptr = &first_arg; *arg_ptr; arg_ptr++)
  200.                 *str_ptr++ = *arg_ptr;
  201. }
  202.  
  203.  
  204. int getval(strptr)
  205. char **strptr;
  206. {
  207.         int n;
  208.         if (!**strptr) return -32760;
  209.         n = atoi(*strptr);
  210.         while (**strptr && *(*strptr)++ != ',');
  211.         return n;
  212. }
  213.  
  214.  
  215. /*
  216.         Storage allocation functions:
  217. */
  218.  
  219. char *alloc(nbytes)
  220. unsigned nbytes;
  221. {
  222.         struct _header *p, *q, *cp;
  223.         int nunits;
  224.         nunits = 1 + (nbytes + (sizeof (_base) - 1)) / sizeof (_base);
  225.         if ((q = _allocp) == NULL) {
  226.                 _base._ptr = _allocp = q = &_base;
  227.                 _base._size = 0;
  228.          }
  229.         for (p = q -> _ptr; ; q = p, p = p -> _ptr) {
  230.                 if (p -> _size >= nunits) {
  231.                         _allocp = q;
  232.                         if (p -> _size == nunits)
  233.                                 _allocp->_ptr = p->_ptr;
  234.                         else {
  235.                                 q = _allocp->_ptr = p + nunits;
  236.                                 q->_ptr = p->_ptr;
  237.                                 q->_size = p->_size - nunits;
  238.                                 p -> _size = nunits;
  239.                          }
  240.                         return p + 1;
  241.                  }
  242.                 if (p == _allocp) {
  243.                         if ((cp = sbrk(nunits *  sizeof (_base))) == ERROR)
  244.                                 return NULL;
  245.                         cp -> _size = nunits;
  246.                         free(cp+1);     /* remember: pointer arithmetic! */
  247.                         p = _allocp;
  248.                 }
  249.          }
  250. }
  251.  
  252.  
  253. free(ap)
  254. struct _header *ap;
  255. {
  256.         struct _header *p, *q;
  257.  
  258.         p = ap - 1;     /* No need for the cast when "ap" is a struct ptr */
  259.  
  260.         for (q = &_base; q->_ptr != &_base; q = q -> _ptr)
  261.                 if (p > q && p < q -> _ptr)
  262.                         break;
  263.  
  264.         if (p + p -> _size == q -> _ptr) {
  265.                 p -> _size += q -> _ptr -> _size;
  266.                 p -> _ptr = q -> _ptr -> _ptr;
  267.          }
  268.         else p -> _ptr = q -> _ptr;
  269.  
  270.         if (q + q -> _size == p) {
  271.                 q -> _size += p -> _size;
  272.                 q -> _ptr = p -> _ptr;
  273.          }
  274.         else q -> _ptr = p;
  275.  
  276.         _allocp = q;
  277. }
  278.  
  279.  
  280. /* Load a disk file into memory (typically an overlay segment): */
  281.  
  282. swapin(name,addr)
  283. char *name;
  284. {
  285.         int fd;
  286.         if (( fd = open(name,0)) == ERROR)
  287.                 return ERROR;
  288.  
  289.         if ((read(fd,addr,512)) < 0) {
  290.                 close(fd);
  291.                 return ERROR;
  292.         }
  293.  
  294.         bdos(26, 0x80); /* reset DMA address for benefit of certain systems */
  295.  
  296.         close(fd);
  297.         return OK;
  298. }
  299.  
  300.  
  301. /*
  302.         Now some really hairy functions to wrap things up:
  303. */
  304.  
  305. int abs(n)
  306. {
  307.         return (n<0) ? -n : n;
  308. }
  309.  
  310. int max(a,b)
  311. {
  312.         return (a > b) ? a : b;
  313. }
  314.  
  315. int min(a,b)
  316. {
  317.         return (a <= b) ? a : b;
  318. }
  319.