[BACK]Return to fstab.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Annotation of src/lib/libc/gen/fstab.c, Revision 1.20

1.20    ! lukem       1: /*     $NetBSD: fstab.c,v 1.19 1999/02/23 17:00:53 mrg Exp $   */
1.6       cgd         2:
1.1       cgd         3: /*
1.6       cgd         4:  * Copyright (c) 1980, 1988, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
1.10      christos   36: #include <sys/cdefs.h>
1.1       cgd        37: #if defined(LIBC_SCCS) && !defined(lint)
1.6       cgd        38: #if 0
                     39: static char sccsid[] = "@(#)fstab.c    8.1 (Berkeley) 6/4/93";
                     40: #else
1.20    ! lukem      41: __RCSID("$NetBSD: fstab.c,v 1.19 1999/02/23 17:00:53 mrg Exp $");
1.6       cgd        42: #endif
1.1       cgd        43: #endif /* LIBC_SCCS and not lint */
                     44:
1.15      kleink     45: #include "namespace.h"
1.7       cgd        46: #include <sys/types.h>
1.20    ! lukem      47:
        !            48: #include <assert.h>
1.14      mycroft    49: #include <err.h>
1.6       cgd        50: #include <errno.h>
1.1       cgd        51: #include <fstab.h>
                     52: #include <stdio.h>
                     53: #include <stdlib.h>
                     54: #include <string.h>
1.6       cgd        55: #include <unistd.h>
1.11      jtc        56:
                     57: #ifdef __weak_alias
                     58: __weak_alias(endfsent,_endfsent);
                     59: __weak_alias(getfsent,_getfsent);
                     60: __weak_alias(getfsfile,_getfsfile);
                     61: __weak_alias(getfsspec,_getfsspec);
                     62: __weak_alias(setfsent,_setfsent);
                     63: #endif
1.1       cgd        64:
                     65: static FILE *_fs_fp;
1.17      christos   66: static size_t _fs_lineno = 0;
                     67: static const char *_fs_file = _PATH_FSTAB;
1.1       cgd        68: static struct fstab _fs_fstab;
1.6       cgd        69:
1.8       jtc        70: static int fstabscan __P((void));
1.1       cgd        71:
1.17      christos   72: static __inline char *nextfld __P((char **, const char *));
                     73:
                     74:
                     75: static __inline char *
                     76: nextfld(str, sep)
                     77:        char **str;
                     78:        const char *sep;
                     79: {
                     80:        char *ret;
1.20    ! lukem      81:
        !            82:        _DIAGASSERT(str != NULL);
        !            83:        _DIAGASSERT(sep != NULL);
        !            84:
1.17      christos   85:        while ((ret = strsep(str, sep)) != NULL && *ret == '\0')
                     86:                continue;
                     87:        return ret;
                     88: }
                     89:
                     90:
1.8       jtc        91: static int
1.1       cgd        92: fstabscan()
                     93: {
1.17      christos   94:        char *cp, *lp, *sp;
1.1       cgd        95: #define        MAXLINELENGTH   1024
                     96:        static char line[MAXLINELENGTH];
                     97:        char subline[MAXLINELENGTH];
1.10      christos   98:        static const char sep[] = ":\n";
                     99:        static const char ws[] = " \t\n";
1.17      christos  100:        static char *fstab_type[] = {
1.19      mrg       101:            FSTAB_RW, FSTAB_RQ, FSTAB_RO, FSTAB_SW, FSTAB_DP, FSTAB_XX, NULL
1.17      christos  102:        };
1.1       cgd       103:
                    104:        for (;;) {
1.17      christos  105:                if (!(lp = fgets(line, sizeof(line), _fs_fp)))
                    106:                        return 0;
                    107:                _fs_lineno++;
1.1       cgd       108: /* OLD_STYLE_FSTAB */
1.17      christos  109:                if (!strpbrk(lp, " \t")) {
                    110:                        _fs_fstab.fs_spec = nextfld(&lp, sep);
1.4       pk        111:                        if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
                    112:                                continue;
1.17      christos  113:                        _fs_fstab.fs_file = nextfld(&lp, sep);
                    114:                        _fs_fstab.fs_type = nextfld(&lp, sep);
1.1       cgd       115:                        if (_fs_fstab.fs_type) {
                    116:                                if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
                    117:                                        continue;
                    118:                                _fs_fstab.fs_mntops = _fs_fstab.fs_type;
                    119:                                _fs_fstab.fs_vfstype =
                    120:                                    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
                    121:                                    "ufs" : "swap";
1.17      christos  122:                                if ((cp = nextfld(&lp, sep)) != NULL) {
1.1       cgd       123:                                        _fs_fstab.fs_freq = atoi(cp);
1.17      christos  124:                                        if ((cp = nextfld(&lp, sep)) != NULL) {
1.1       cgd       125:                                                _fs_fstab.fs_passno = atoi(cp);
1.17      christos  126:                                                return 1;
1.1       cgd       127:                                        }
                    128:                                }
                    129:                        }
                    130:                        goto bad;
                    131:                }
                    132: /* OLD_STYLE_FSTAB */
1.17      christos  133:                _fs_fstab.fs_spec = nextfld(&lp, ws);
1.1       cgd       134:                if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
                    135:                        continue;
1.17      christos  136:                _fs_fstab.fs_file = nextfld(&lp, ws);
                    137:                _fs_fstab.fs_vfstype = nextfld(&lp, ws);
                    138:                _fs_fstab.fs_mntops = nextfld(&lp, ws);
1.1       cgd       139:                if (_fs_fstab.fs_mntops == NULL)
                    140:                        goto bad;
                    141:                _fs_fstab.fs_freq = 0;
                    142:                _fs_fstab.fs_passno = 0;
1.17      christos  143:                if ((cp = nextfld(&lp, ws)) != NULL) {
1.1       cgd       144:                        _fs_fstab.fs_freq = atoi(cp);
1.17      christos  145:                        if ((cp = nextfld(&lp, ws)) != NULL)
1.1       cgd       146:                                _fs_fstab.fs_passno = atoi(cp);
                    147:                }
1.17      christos  148:                sp = strncpy(subline, _fs_fstab.fs_mntops, sizeof(subline)-1);
                    149:                while ((cp = nextfld(&sp, ",")) != NULL) {
                    150:                        char **tp;
                    151:
1.1       cgd       152:                        if (strlen(cp) != 2)
                    153:                                continue;
1.17      christos  154:
                    155:                        for (tp = fstab_type; *tp; tp++)
                    156:                                if (strcmp(cp, *tp) == 0) {
                    157:                                        _fs_fstab.fs_type = *tp;
                    158:                                        break;
                    159:                                }
                    160:                        if (*tp)
1.1       cgd       161:                                break;
                    162:                }
1.17      christos  163:                if (strcmp(_fs_fstab.fs_type, FSTAB_XX) == 0)
1.1       cgd       164:                        continue;
                    165:                if (cp != NULL)
1.17      christos  166:                        return 1;
1.1       cgd       167:
1.17      christos  168: bad:
1.18      thorpej   169:                warnx("%s, %lu: Missing fields", _fs_file, (u_long)_fs_lineno);
1.1       cgd       170:        }
                    171:        /* NOTREACHED */
                    172: }
                    173:
                    174: struct fstab *
                    175: getfsent()
                    176: {
1.10      christos  177:        if ((!_fs_fp && !setfsent()) || !fstabscan())
1.17      christos  178:                return NULL;
                    179:        return &_fs_fstab;
1.1       cgd       180: }
                    181:
                    182: struct fstab *
                    183: getfsspec(name)
1.12      perry     184:        const char *name;
1.1       cgd       185: {
1.20    ! lukem     186:
        !           187:        _DIAGASSERT(name != NULL);
        !           188: #ifdef _DIAGNOSTIC
        !           189:        if (name == NULL)
        !           190:                return NULL;
        !           191: #endif
        !           192:
1.1       cgd       193:        if (setfsent())
                    194:                while (fstabscan())
                    195:                        if (!strcmp(_fs_fstab.fs_spec, name))
1.17      christos  196:                                return &_fs_fstab;
                    197:        return NULL;
1.1       cgd       198: }
                    199:
                    200: struct fstab *
                    201: getfsfile(name)
1.12      perry     202:        const char *name;
1.1       cgd       203: {
1.20    ! lukem     204:
        !           205:        _DIAGASSERT(name != NULL);
        !           206: #ifdef _DIAGNOSTIC
        !           207:        if (name == NULL)
        !           208:                return NULL;
        !           209: #endif
        !           210:
1.1       cgd       211:        if (setfsent())
                    212:                while (fstabscan())
                    213:                        if (!strcmp(_fs_fstab.fs_file, name))
1.17      christos  214:                                return &_fs_fstab;
                    215:        return NULL;
1.1       cgd       216: }
                    217:
1.8       jtc       218: int
1.1       cgd       219: setfsent()
                    220: {
1.17      christos  221:        _fs_lineno = 0;
1.1       cgd       222:        if (_fs_fp) {
                    223:                rewind(_fs_fp);
1.17      christos  224:                return 1;
1.1       cgd       225:        }
1.17      christos  226:        if ((_fs_fp = fopen(_PATH_FSTAB, "r")) == NULL) {
                    227:                warn("Cannot open `%s'", _PATH_FSTAB);
                    228:                return 0;
                    229:        }
                    230:        return 1;
1.1       cgd       231: }
                    232:
                    233: void
                    234: endfsent()
                    235: {
                    236:        if (_fs_fp) {
                    237:                (void)fclose(_fs_fp);
                    238:                _fs_fp = NULL;
                    239:        }
                    240: }

CVSweb <webmaster@jp.NetBSD.org>