home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / LIBSRC.ZOO / libsrc / gen / fstab.c < prev    next >
C/C++ Source or Header  |  1992-01-31  |  5KB  |  200 lines

  1. /*
  2.  * Copyright (c) 1980, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)fstab.c    5.15 (Berkeley) 2/23/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/errno.h>
  39. #include <fstab.h>
  40. #include <unistd.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44.  
  45. static FILE *_fs_fp;
  46. static struct fstab _fs_fstab;
  47. static error();
  48.  
  49. static
  50. fstabscan()
  51. {
  52.     register char *cp;
  53. #define    MAXLINELENGTH    1024
  54.     static char line[MAXLINELENGTH];
  55.     char subline[MAXLINELENGTH];
  56.     int typexx;
  57.  
  58.     for (;;) {
  59.         if (!(cp = fgets(line, sizeof(line), _fs_fp)))
  60.             return(0);
  61. /* OLD_STYLE_FSTAB */
  62.         if (!strpbrk(cp, " \t")) {
  63.             _fs_fstab.fs_spec = strtok(cp, ":\n");
  64.             _fs_fstab.fs_file = strtok((char *)NULL, ":\n");
  65.             _fs_fstab.fs_type = strtok((char *)NULL, ":\n");
  66.             if (_fs_fstab.fs_type) {
  67.                 if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
  68.                     continue;
  69.                 _fs_fstab.fs_mntops = _fs_fstab.fs_type;
  70.                 _fs_fstab.fs_vfstype =
  71.                     strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
  72.                     "ufs" : "swap";
  73.                 if (cp = strtok((char *)NULL, ":\n")) {
  74.                     _fs_fstab.fs_freq = atoi(cp);
  75.                     if (cp = strtok((char *)NULL, ":\n")) {
  76.                         _fs_fstab.fs_passno = atoi(cp);
  77.                         return(1);
  78.                     }
  79.                 }
  80.             }
  81.             goto bad;
  82.         }
  83. /* OLD_STYLE_FSTAB */
  84.         _fs_fstab.fs_spec = strtok(cp, " \t\n");
  85.         if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
  86.             continue;
  87.         _fs_fstab.fs_file = strtok((char *)NULL, " \t\n");
  88.         _fs_fstab.fs_vfstype = strtok((char *)NULL, " \t\n");
  89.         _fs_fstab.fs_mntops = strtok((char *)NULL, " \t\n");
  90.         if (_fs_fstab.fs_mntops == NULL)
  91.             goto bad;
  92.         _fs_fstab.fs_freq = 0;
  93.         _fs_fstab.fs_passno = 0;
  94.         if ((cp = strtok((char *)NULL, " \t\n")) != NULL) {
  95.             _fs_fstab.fs_freq = atoi(cp);
  96.             if ((cp = strtok((char *)NULL, " \t\n")) != NULL)
  97.                 _fs_fstab.fs_passno = atoi(cp);
  98.         }
  99.         strcpy(subline, _fs_fstab.fs_mntops);
  100.         for (typexx = 0, cp = strtok(subline, ","); cp;
  101.              cp = strtok((char *)NULL, ",")) {
  102.             if (strlen(cp) != 2)
  103.                 continue;
  104.             if (!strcmp(cp, FSTAB_RW)) {
  105.                 _fs_fstab.fs_type = FSTAB_RW;
  106.                 break;
  107.             }
  108.             if (!strcmp(cp, FSTAB_RQ)) {
  109.                 _fs_fstab.fs_type = FSTAB_RQ;
  110.                 break;
  111.             }
  112.             if (!strcmp(cp, FSTAB_RO)) {
  113.                 _fs_fstab.fs_type = FSTAB_RO;
  114.                 break;
  115.             }
  116.             if (!strcmp(cp, FSTAB_SW)) {
  117.                 _fs_fstab.fs_type = FSTAB_SW;
  118.                 break;
  119.             }
  120.             if (!strcmp(cp, FSTAB_XX)) {
  121.                 _fs_fstab.fs_type = FSTAB_XX;
  122.                 typexx++;
  123.                 break;
  124.             }
  125.         }
  126.         if (typexx)
  127.             continue;
  128.         if (cp != NULL)
  129.             return(1);
  130.  
  131. bad:        /* no way to distinguish between EOF and syntax error */
  132.         error(EFTYPE);
  133.     }
  134.     /* NOTREACHED */
  135. }
  136.  
  137. struct fstab *
  138. getfsent()
  139. {
  140.     if (!_fs_fp && !setfsent() || !fstabscan())
  141.         return((struct fstab *)NULL);
  142.     return(&_fs_fstab);
  143. }
  144.  
  145. struct fstab *
  146. getfsspec(name)
  147.     register const char *name;
  148. {
  149.     if (setfsent())
  150.         while (fstabscan())
  151.             if (!strcmp(_fs_fstab.fs_spec, name))
  152.                 return(&_fs_fstab);
  153.     return((struct fstab *)NULL);
  154. }
  155.  
  156. struct fstab *
  157. getfsfile(name)
  158.     register const char *name;
  159. {
  160.     if (setfsent())
  161.         while (fstabscan())
  162.             if (!strcmp(_fs_fstab.fs_file, name))
  163.                 return(&_fs_fstab);
  164.     return((struct fstab *)NULL);
  165. }
  166.  
  167. setfsent()
  168. {
  169.     if (_fs_fp) {
  170.         rewind(_fs_fp);
  171.         return(1);
  172.     }
  173.     if (_fs_fp = fopen(_PATH_FSTAB, "r"))
  174.         return(1);
  175.     error(errno);
  176.     return(0);
  177. }
  178.  
  179. void
  180. endfsent()
  181. {
  182.     if (_fs_fp) {
  183.         (void)fclose(_fs_fp);
  184.         _fs_fp = NULL;
  185.     }
  186. }
  187.  
  188. static
  189. error(err)
  190.     int err;
  191. {
  192.     char *p;
  193.  
  194.     (void)write(STDERR_FILENO, "fstab: ", 7);
  195.     (void)write(STDERR_FILENO, _PATH_FSTAB, sizeof(_PATH_FSTAB) - 1);
  196.     p = strerror(err);
  197.     (void)write(STDERR_FILENO, p, strlen(p));
  198.     (void)write(STDERR_FILENO, "\n", 1);
  199. }
  200.