home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CBASE101.ZIP / LSEQ101.ZIP / LSOPEN.C < prev    next >
Text File  |  1990-06-20  |  3KB  |  141 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)lsopen.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. /* lseq control structure table definition */
  18. lseq_t lsb[LSOPEN_MAX];
  19.  
  20. /*man---------------------------------------------------------------------------
  21. NAME
  22.      lsopen - open an lseq
  23.  
  24. SYNOPSIS
  25.      lseq_t *lsopen(filename, type)
  26.      const char *filename;
  27.      const char *type;
  28.  
  29. DESCRIPTION
  30.      The lsopen function opens the file named by filename as an lseq.
  31.      A pointer to the lseq_t structure associated with the file is
  32.      returned.
  33.  
  34.      type is a character string having one of the following values:
  35.  
  36.           "r"            open for reading
  37.           "r+"           open for update (reading and writing)
  38.  
  39.      lsopen will fail if one or more of the following is true:
  40.  
  41.      [EINVAL]       filename is the NULL pointer.
  42.      [EINVAL]       type is not "r" or "r+".
  43.      [ENOENT]       The named lseq file does not exist.
  44.      [LSECORRUPT]   The named file is corrupt.
  45.      [LSEEOF]       No file header.
  46.      [LSEMFILE]     Too many open lseqs.  The maximum
  47.                     is defined as LSOPEN_MAX in lseq.h.
  48.  
  49. SEE ALSO
  50.      lsclose, lscreate.
  51.  
  52. DIAGNOSTICS
  53.      lsopen returns a NULL pointer on failure, and errno set to
  54.      indicate the error.
  55.  
  56. ------------------------------------------------------------------------------*/
  57. lseq_t *lsopen(filename, type)
  58. const char *filename;
  59. const char *type;
  60. {
  61.     int terrno = 0;
  62.     lseq_t *lsp = NULL;
  63.  
  64.     /* validate input parameters */
  65.     if (filename == NULL || type == NULL) {
  66.         errno = EINVAL;
  67.         return NULL;
  68.     }
  69.  
  70.     /* find free slot in lsb table */
  71.     for (lsp = lsb; lsp < lsb + LSOPEN_MAX; ++lsp) {
  72.         if (!(lsp->flags & LSOPEN)) {
  73.             break;        /* found */
  74.         }
  75.     }
  76.     if (lsp >= lsb + LSOPEN_MAX) {
  77.         errno = LSEMFILE;
  78.         return NULL;        /* no free slots */
  79.     }
  80.  
  81.     /* open file */
  82.     if (strcmp(type, LS_READ) == 0) {
  83.         lsp->flags = LSREAD;
  84.     } else if (strcmp(type, LS_RDWR) == 0) {
  85.         lsp->flags = LSREAD | LSWRITE;
  86.     } else {
  87.         errno = EINVAL;
  88.         return NULL;
  89.     }
  90.     lsp->bp = bopen(filename, type, sizeof(lshdr_t), (size_t)1, (size_t)0);
  91.     if (lsp->bp == NULL) {
  92.         if ((errno != EACCES) && (errno != ENOENT)) LSEPRINT;
  93.         memset(lsp, 0, sizeof(*lsb));
  94.         lsp->flags = 0;
  95.         return NULL;
  96.     }
  97.  
  98.     /* load lseq_t structure */
  99.     if (bgeth(lsp->bp, &lsp->lshdr) == -1) {    /* header */
  100.         LSEPRINT;
  101.         terrno = errno;
  102.         bclose(lsp->bp);
  103.         memset(lsp, 0, sizeof(*lsb));
  104.         lsp->flags = 0;
  105.         errno = terrno;
  106.         return NULL;
  107.     }
  108.     if (lsp->lshdr.flags & LSHMOD) {        /* corrupt file */
  109.         bclose(lsp->bp);
  110.         memset(lsp, 0, sizeof(*lsb));
  111.         lsp->flags = 0;
  112.         errno = LSECORRUPT;
  113.         return NULL;
  114.     }
  115.     lsp->clspos = NIL;                /* cursor */
  116.     if (ls_alloc(lsp) == -1) {
  117.         LSEPRINT;
  118.         terrno = errno;
  119.         bclose(lsp->bp);
  120.         memset(lsp, 0, sizeof(*lsb));
  121.         lsp->flags = 0;
  122.         errno = terrno;
  123.         return NULL;
  124.     }
  125.  
  126.     /* set up buffering */
  127.     if (bsetvbuf(lsp->bp, NULL, ls_blksize(lsp), LSBUFCNT) == -1) {
  128.         LSEPRINT;
  129.         terrno = errno;
  130.         ls_free(lsp);
  131.         bclose(lsp->bp);
  132.         memset(lsp, 0, sizeof(*lsb));
  133.         lsp->flags = 0;
  134.         errno = terrno;
  135.         return NULL;
  136.     }
  137.  
  138.     errno = 0;
  139.     return lsp;
  140. }
  141.