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

  1. /*
  2.  *    Copyright (c) 1989-1992 Citadel Software, Inc.
  3.  *    All Rights Reserved
  4.  */
  5.  
  6. /* #ident    "@(#)lsopen.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. /* lseq control structure table definition */
  26. lseq_t lsb[LSOPEN_MAX];
  27.  
  28. /*man---------------------------------------------------------------------------
  29. NAME
  30.      lsopen - open an lseq
  31.  
  32. SYNOPSIS
  33.      lseq_t *lsopen(filename, type)
  34.      const char *filename;
  35.      const char *type;
  36.  
  37. DESCRIPTION
  38.      The lsopen function opens the file named by filename as an lseq.
  39.      A pointer to the lseq_t structure associated with the file is
  40.      returned.
  41.  
  42.      type is a character string having one of the following values:
  43.  
  44.           "r"            open for reading
  45.           "r+"           open for update (reading and writing)
  46.  
  47.      lsopen will fail if one or more of the following is true:
  48.  
  49.      [EINVAL]       filename is the NULL pointer.
  50.      [EINVAL]       type is not "r" or "r+".
  51.      [ENOENT]       The named lseq file does not exist.
  52.      [LSEMFILE]     Too many open lseqs.  The maximum
  53.                     is defined as LSOPEN_MAX in lseq.h.
  54.  
  55. SEE ALSO
  56.      lsclose, lscreate.
  57.  
  58. DIAGNOSTICS
  59.      lsopen returns a NULL pointer on failure, and errno is set to
  60.      indicate the error.
  61.  
  62. ------------------------------------------------------------------------------*/
  63. #ifdef AC_PROTO
  64. lseq_t *lsopen(const char *filename, const char *type)
  65. #else
  66. lseq_t *lsopen(filename, type)
  67. const char *filename;
  68. const char *type;
  69. #endif
  70. {
  71.     lseq_t *    lsp    = NULL;
  72.  
  73.     /* validate input parameters */
  74.     if (filename == NULL || type == NULL) {
  75.         errno = EINVAL;
  76.         return NULL;
  77.     }
  78.  
  79.     /* find free slot in lsb table */
  80.     for (lsp = lsb; lsp < lsb + LSOPEN_MAX; ++lsp) {
  81.         if (!(lsp->flags & LSOPEN)) {
  82.             break;        /* found */
  83.         }
  84.     }
  85.     if (lsp >= lsb + LSOPEN_MAX) {
  86.         errno = LSEMFILE;
  87.         return NULL;        /* no free slots */
  88.     }
  89.  
  90.     /* open file */
  91.     if (strcmp(type, LS_READ) == 0) {
  92.         lsp->flags = LSREAD;
  93.     } else if (strcmp(type, LS_RDWR) == 0) {
  94.         lsp->flags = LSREAD | LSWRITE;
  95.     } else {
  96.         errno = EINVAL;
  97.         return NULL;
  98.     }
  99.     lsp->bp = bopen(filename, type, sizeof(lshdr_t), (size_t)1, (size_t)0);
  100.     if (lsp->bp == NULL) {
  101. #ifdef DEBUG
  102.         if (errno != EACCES && errno != ENOENT) LSERRLOG;
  103. #endif
  104.         memset(lsp, 0, sizeof(*lsp));
  105.         lsp->flags = 0;
  106.         return NULL;
  107.     }
  108.  
  109.     /* load lseq_t structure */
  110.     memset(&lsp->lshdr, 0, sizeof(lsp->lshdr));    /* header */
  111.     lsp->clspos = NIL;            /* cursor */
  112.     lsp->clsrp = NULL;            /* current record pointer */
  113.  
  114.     return lsp;
  115. }
  116.