home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Berk / Berklib.c next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  4.2 KB  |  252 lines

  1. /* $XConsortium: Berklib.c,v 1.15 91/09/10 08:50:04 rws Exp $ */
  2.  
  3. /*
  4.  * These are routines found in BSD but not on all other systems.  The core
  5.  * MIT distribution does not use them except for ffs in the server, unless
  6.  * the system is seriously deficient.  You should enable only the ones that
  7.  * you need for your system.  Use Xfuncs.h in clients to avoid using the
  8.  * slow versions of bcopy, bcmp, and bzero provided here.
  9.  */
  10.  
  11. #include <sys/types.h>
  12.  
  13. #ifdef hpux
  14. #define WANT_BFUNCS
  15. #define WANT_FFS
  16. #define WANT_RANDOM
  17. #endif
  18.  
  19. #ifdef macII
  20. /* silly bcopy in A/UX 2.0.1 does not handle overlaps */
  21. #define WANT_BFUNCS
  22. #define NO_BZERO
  23. #endif
  24.  
  25. #ifdef SVR4
  26. #define WANT_BFUNCS
  27. #define WANT_FFS
  28. #define WANT_RANDOM
  29. #endif
  30.  
  31. #ifdef hcx
  32. #define WANT_FFS
  33. #endif
  34.  
  35. #ifdef SYSV386
  36. #ifdef SYSV
  37. #define WANT_FFS
  38. #endif
  39. #endif
  40.  
  41. /* you should use Xfuncs.h in code instead of relying on Berklib */
  42. #ifdef WANT_BFUNCS
  43.  
  44. #include <X11/Xosdefs.h>
  45.  
  46. #if (__STDC__ && defined(X_NOT_STDC_ENV)) || defined(SVR4) || defined(hpux)
  47.  
  48. #include <string.h>
  49.  
  50. void bcopy (b1, b2, length)
  51.     register char *b1, *b2;
  52.     register int length;
  53. {
  54.     memmove(b2, b1, (size_t)length);
  55. }
  56.  
  57. int bcmp (b1, b2, length)
  58.     register char *b1, *b2;
  59.     register int length;
  60. {
  61.     return memcmp(b1, b2, (size_t)length);
  62. }
  63.  
  64. void bzero (b, length)
  65.     register char *b;
  66.     register int length;
  67. {
  68.     memset(b, 0, (size_t)length);
  69. }
  70.  
  71. #else
  72.  
  73. void bcopy (b1, b2, length)
  74.     register char *b1, *b2;
  75.     register int length;
  76. {
  77.     if (b1 < b2) {
  78.     b2 += length;
  79.     b1 += length;
  80.     while (length--)
  81.         *--b2 = *--b1;
  82.     } else {
  83.     while (length--)
  84.         *b2++ = *b1++;
  85.     }
  86. }
  87.  
  88. #if defined(SYSV)
  89.  
  90. #include <memory.h>
  91.  
  92. int bcmp (b1, b2, length)
  93.     register char *b1, *b2;
  94.     register int length;
  95. {
  96.     return memcmp(b1, b2, length);
  97. }
  98.  
  99. #ifndef NO_BZERO
  100.  
  101. bzero (b, length)
  102.     register char *b;
  103.     register int length;
  104. {
  105.     memset(b, 0, length);
  106. }
  107.  
  108. #endif
  109.  
  110. #else
  111.  
  112. int bcmp (b1, b2, length)
  113.     register char *b1, *b2;
  114.     register int length;
  115. {
  116.     while (length--) {
  117.     if (*b1++ != *b2++) return 1;
  118.     }
  119.     return 0;
  120. }
  121.  
  122. void bzero (b, length)
  123.     register char *b;
  124.     register int length;
  125. {
  126.     while (length--)
  127.     *b++ = '\0';
  128. }
  129.  
  130. #endif
  131. #endif
  132. #endif /* WANT_BFUNCS */
  133.  
  134. #ifdef WANT_FFS
  135. int
  136. ffs(mask)
  137. unsigned int    mask;
  138. {
  139.     register i;
  140.  
  141.     if ( ! mask ) return 0;
  142.     i = 1;
  143.     while (! (mask & 1)) {
  144.     i++;
  145.     mask = mask >> 1;
  146.     }
  147.     return i;
  148. }
  149. #endif
  150.  
  151. #ifdef WANT_RANDOM
  152. #if defined(SYSV) || defined(SVR4)
  153.  
  154. long lrand48();
  155.  
  156. long random()
  157. {
  158.    return (lrand48());
  159. }
  160.  
  161. void srandom(seed)
  162.     int seed;
  163. {
  164.    srand48(seed);
  165. }
  166.  
  167. #else
  168.  
  169. long random()
  170. {
  171.    return (rand());
  172. }
  173.  
  174. void srandom(seed)
  175.     int seed;
  176. {
  177.    srand(seed);
  178. }
  179.  
  180. #endif
  181. #endif /* WANT_RANDOM */
  182.  
  183. /*
  184.  * insque, remque - insert/remove element from a queue
  185.  *
  186.  * DESCRIPTION
  187.  *      Insque and remque manipulate queues built from doubly linked
  188.  *      lists.  Each element in the queue must in the form of
  189.  *      ``struct qelem''.  Insque inserts elem in a queue immedi-
  190.  *      ately after pred; remque removes an entry elem from a queue.
  191.  *
  192.  * SEE ALSO
  193.  *      ``VAX Architecture Handbook'', pp. 228-235.
  194.  */
  195.  
  196. #ifdef WANT_QUE
  197. struct qelem {
  198.     struct    qelem *q_forw;
  199.     struct    qelem *q_back;
  200.     char *q_data;
  201.     };
  202.  
  203. insque(elem, pred)
  204. register struct qelem *elem, *pred;
  205. {
  206.     register struct qelem *q;
  207.     /* Insert locking code here */
  208.     if ( elem->q_forw = q = (pred ? pred->q_forw : pred) )
  209.     q->q_back = elem;
  210.     if ( elem->q_back = pred )
  211.     pred->q_forw = elem;
  212.     /* Insert unlocking code here */
  213. }
  214.  
  215. remque(elem)
  216. register struct qelem *elem;
  217. {
  218.     register struct qelem *q;
  219.     if ( ! elem ) return;
  220.     /* Insert locking code here */
  221.  
  222.     if ( q = elem->q_back ) q->q_forw = elem->q_forw;
  223.     if ( q = elem->q_forw ) q->q_back = elem->q_back;
  224.  
  225.     /* insert unlocking code here */
  226. }
  227. #endif /* WANT_QUE */
  228.  
  229. /*
  230.  * gettimeofday emulation
  231.  * Caution -- emulation is incomplete
  232.  *  - has only second, not microsecond, resolution.
  233.  *  - does not return timezone info.
  234.  */
  235.  
  236. #if WANT_GTOD
  237. int gettimeofday (tvp, tzp)
  238.     struct timeval *tvp;
  239.     struct timezone *tzp;
  240. {
  241.     time (&tvp->tv_sec);
  242.     tvp->tv_usec = 0L;
  243.  
  244.     if (tzp) {
  245.     fprintf( stderr,
  246.          "Warning: gettimeofday() emulation does not return timezone\n"
  247.         );
  248.     }
  249. }
  250. #endif /* WANT_GTOD */
  251.  
  252.