home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / c / cbase.zoo / lseq101.zoo / lscreate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-20  |  2.6 KB  |  121 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lscreate.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. /*#include <string.h>*/
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13.  
  14. /* local headers */
  15. #include "lseq_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      lscreate - create an lseq
  20.  
  21. SYNOPSIS
  22.      int lscreate(filename, recsize)
  23.      const char *filename;
  24.      size_t recsize;
  25.  
  26. DESCRIPTION
  27.      The lscreate function creates the file named by filename as an
  28.      lseq.
  29.  
  30.      recsize is the size of the records in the lseq.
  31.  
  32.      lscreate will fail if one or more of the following is true:
  33.  
  34.      [EINVAL]       filename is the NULL pointer.
  35.      [EINVAL]       recsize is less than 1.
  36.      [LSEMFILE]     Too many open lseqs.  The maximum
  37.                     is defined as LSOPEN_MAX in lseq.h.
  38.  
  39. SEE ALSO
  40.      lsopen.
  41.  
  42. DIAGNOSTICS
  43.      Upon successful completion, a value of 0 is returned.  Otherwise,
  44.      a value of -1 is returned, and errno set to indicate the error.
  45.  
  46. ------------------------------------------------------------------------------*/
  47. int lscreate(filename, recsize)
  48. const char * filename;
  49. size_t recsize;
  50. {
  51.     int terrno = 0;
  52.     lseq_t *lsp = NULL;
  53.  
  54.     /* validate input parameters */
  55.     if (filename == NULL || recsize < sizeof(lspos_t)) {
  56.         errno = EINVAL;
  57.         return -1;
  58.     }
  59.  
  60.     /* find free slot in lsb table */
  61.     for (lsp = lsb; lsp < (lsb + LSOPEN_MAX); lsp++) {
  62.         if (!(lsp->flags & LSOPEN)) {
  63.             break;        /* found */
  64.         }
  65.     }
  66.     if (lsp >= lsb + LSOPEN_MAX) {
  67.         errno = LSEMFILE;
  68.         return -1;        /* no free slots */
  69.     }
  70.  
  71.     /* load lseq_t structure */
  72.     lsp->lshdr.flh = NIL;
  73.     lsp->lshdr.recsize = recsize;
  74.     lsp->lshdr.flags = 0;
  75.     lsp->lshdr.first = NIL;
  76.     lsp->lshdr.last = NIL;
  77.     lsp->lshdr.reccnt = 0;
  78.     lsp->bp = NULL;
  79.     lsp->flags = LSREAD | LSWRITE;
  80.     lsp->clspos = NIL;
  81.     lsp->clsrp = NULL;
  82.     if (ls_alloc(lsp) == -1) {
  83.         LSEPRINT;
  84.         memset(lsp, 0, sizeof(*lsb));
  85.         lsp->flags = 0;
  86.         return -1;
  87.     }
  88.  
  89.     /* create file */
  90.     lsp->bp = bopen(filename, "c", sizeof(lshdr_t), (size_t)1, (size_t)0);
  91.     if (lsp->bp == NULL) {
  92.         if (errno != EEXIST) LSEPRINT;
  93.         ls_free(lsp);
  94.         memset(lsp, 0, sizeof(*lsb));
  95.         lsp->flags = 0;
  96.         return -1;
  97.     }
  98.  
  99.     /* write header to file */
  100.     if (bputh(lsp->bp, &lsp->lshdr) == -1) {
  101.         LSEPRINT;
  102.         terrno = errno;
  103.         bclose(lsp->bp);
  104.         ls_free(lsp);
  105.         memset(lsp, 0, sizeof(*lsb));
  106.         lsp->flags = 0;
  107.         errno = terrno;
  108.         return -1;
  109.     }
  110.  
  111.     /* close lsp */
  112.     if (lsclose(lsp) == -1) {
  113.         LSEPRINT;
  114.         ls_free(lsp);
  115.         return -1;
  116.     }
  117.  
  118.     errno = 0;
  119.     return 0;
  120. }
  121.