home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR41 / CBASE11.ZIP / LSCREATE.C < prev    next >
C/C++ Source or Header  |  1993-01-01  |  3KB  |  134 lines

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