home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / xmit / llist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  1.2 KB  |  67 lines

  1. /*
  2. ** Routines for fiddlin' linked lists
  3. ** Erik E. Fair <fair@ucbarpa.berkeley.edu>
  4. */
  5.  
  6. #include "../common/conf.h"
  7. #include <sys/types.h>
  8. #include "llist.h"
  9.  
  10. #ifdef USG
  11. #define bcopy(a,b,c)  memcpy(b,a,c)
  12. #endif
  13.  
  14. extern free();
  15. extern caddr_t    malloc();
  16.  
  17. /*
  18. ** a little wasteful for some cases, but it works, and I don't mind
  19. ** the extra padding - think of it as insurance.
  20. */
  21. #define    ALIGN(x)    ((x) + (sizeof(long) - (x) % sizeof(long)))
  22.  
  23. /*
  24. ** recursively free a linked list
  25. */
  26. void
  27. l_free(lp)
  28. register ll_t *lp;
  29. {
  30.     if (lp->l_next == (ll_t *)NULL)
  31.         return;
  32.     l_free(lp->l_next);
  33.     (void) free(lp->l_item);
  34. }
  35.  
  36. /*
  37. ** allocate a new element in a linked list, along with enough space
  38. ** at the end of the item for the next list element header.
  39. */
  40. ll_t *
  41. l_alloc(lp, s, len)
  42. register ll_t    *lp;
  43. caddr_t    s;
  44. register int len;
  45. {
  46.     if (s == (caddr_t)NULL || lp == (ll_t *)NULL || len <= 0)
  47.         return((ll_t *)NULL);
  48.  
  49.     lp->l_len = len;
  50.     len = ALIGN(len);
  51.  
  52.     if ((lp->l_item = malloc((unsigned)len + sizeof(ll_t))) == (caddr_t)NULL)
  53.         return((ll_t *)NULL);
  54.  
  55.     bcopy(s, lp->l_item, lp->l_len);
  56.     lp->l_next = (ll_t *)(&lp->l_item[len]);
  57.  
  58.     /*
  59.     ** set up next list entry
  60.     */
  61.     lp = lp->l_next;
  62.     lp->l_next = (ll_t *)NULL;
  63.     lp->l_item = (caddr_t)NULL;
  64.     lp->l_len = 0;
  65.     return(lp);
  66. }
  67.