[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.17

1.17    ! christos    1: /*     $NetBSD: fstab.c,v 1.16 1998/10/16 11:24:30 kleink 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.17    ! christos   41: __RCSID("$NetBSD: fstab.c,v 1.16 1998/10/16 11:24:30 kleink 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.14      mycroft    47: #include <err.h>
1.6       cgd        48: #include <errno.h>
1.1       cgd        49: #include <fstab.h>
                     50: #include <stdio.h>
                     51: #include <stdlib.h>
                     52: #include <string.h>
1.6       cgd        53: #include <unistd.h>
1.11      jtc        54:
                     55: #ifdef __weak_alias
                     56: __weak_alias(endfsent,_endfsent);
                     57: __weak_alias(getfsent,_getfsent);
                     58: __weak_alias(getfsfile,_getfsfile);
                     59: __weak_alias(getfsspec,_getfsspec);
                     60: __weak_alias(setfsent,_setfsent);
                     61: #endif
1.1       cgd        62:
                     63: static FILE *_fs_fp;
1.17    ! christos   64: static size_t _fs_lineno = 0;
        !            65: static const char *_fs_file = _PATH_FSTAB;
1.1       cgd        66: static struct fstab _fs_fstab;
1.6       cgd        67:
1.8       jtc        68: static int fstabscan __P((void));
1.1       cgd        69:
1.17    ! christos   70: static __inline char *nextfld __P((char **, const char *));
        !            71:
        !            72:
        !            73: static __inline char *
        !            74: nextfld(str, sep)
        !            75:        char **str;
        !            76:        const char *sep;
        !            77: {
        !            78:        char *ret;
        !            79:        while ((ret = strsep(str, sep)) != NULL && *ret == '\0')
        !            80:                continue;
        !            81:        return ret;
        !            82: }
        !            83:
        !            84:
1.8       jtc        85: static int
1.1       cgd        86: fstabscan()
                     87: {
1.17    ! christos   88:        char *cp, *lp, *sp;
1.1       cgd        89: #define        MAXLINELENGTH   1024
                     90:        static char line[MAXLINELENGTH];
                     91:        char subline[MAXLINELENGTH];
1.10      christos   92:        static const char sep[] = ":\n";
                     93:        static const char ws[] = " \t\n";
1.17    ! christos   94:        static char *fstab_type[] = {
        !            95:            FSTAB_RW, FSTAB_RQ, FSTAB_RO, FSTAB_SW, FSTAB_XX, NULL
        !            96:        };
1.1       cgd        97:
                     98:        for (;;) {
1.17    ! christos   99:                if (!(lp = fgets(line, sizeof(line), _fs_fp)))
        !           100:                        return 0;
        !           101:                _fs_lineno++;
1.1       cgd       102: /* OLD_STYLE_FSTAB */
1.17    ! christos  103:                if (!strpbrk(lp, " \t")) {
        !           104:                        _fs_fstab.fs_spec = nextfld(&lp, sep);
1.4       pk        105:                        if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
                    106:                                continue;
1.17    ! christos  107:                        _fs_fstab.fs_file = nextfld(&lp, sep);
        !           108:                        _fs_fstab.fs_type = nextfld(&lp, sep);
1.1       cgd       109:                        if (_fs_fstab.fs_type) {
                    110:                                if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
                    111:                                        continue;
                    112:                                _fs_fstab.fs_mntops = _fs_fstab.fs_type;
                    113:                                _fs_fstab.fs_vfstype =
                    114:                                    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
                    115:                                    "ufs" : "swap";
1.17    ! christos  116:                                if ((cp = nextfld(&lp, sep)) != NULL) {
1.1       cgd       117:                                        _fs_fstab.fs_freq = atoi(cp);
1.17    ! christos  118:                                        if ((cp = nextfld(&lp, sep)) != NULL) {
1.1       cgd       119:                                                _fs_fstab.fs_passno = atoi(cp);
1.17    ! christos  120:                                                return 1;
1.1       cgd       121:                                        }
                    122:                                }
                    123:                        }
                    124:                        goto bad;
                    125:                }
                    126: /* OLD_STYLE_FSTAB */
1.17    ! christos  127:                _fs_fstab.fs_spec = nextfld(&lp, ws);
1.1       cgd       128:                if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
                    129:                        continue;
1.17    ! christos  130:                _fs_fstab.fs_file = nextfld(&lp, ws);
        !           131:                _fs_fstab.fs_vfstype = nextfld(&lp, ws);
        !           132:                _fs_fstab.fs_mntops = nextfld(&lp, ws);
1.1       cgd       133:                if (_fs_fstab.fs_mntops == NULL)
                    134:                        goto bad;
                    135:                _fs_fstab.fs_freq = 0;
                    136:                _fs_fstab.fs_passno = 0;
1.17    ! christos  137:                if ((cp = nextfld(&lp, ws)) != NULL) {
1.1       cgd       138:                        _fs_fstab.fs_freq = atoi(cp);
1.17    ! christos  139:                        if ((cp = nextfld(&lp, ws)) != NULL)
1.1       cgd       140:                                _fs_fstab.fs_passno = atoi(cp);
                    141:                }
1.17    ! christos  142:                sp = strncpy(subline, _fs_fstab.fs_mntops, sizeof(subline)-1);
        !           143:                while ((cp = nextfld(&sp, ",")) != NULL) {
        !           144:                        char **tp;
        !           145:
1.1       cgd       146:                        if (strlen(cp) != 2)
                    147:                                continue;
1.17    ! christos  148:
        !           149:                        for (tp = fstab_type; *tp; tp++)
        !           150:                                if (strcmp(cp, *tp) == 0) {
        !           151:                                        _fs_fstab.fs_type = *tp;
        !           152:                                        break;
        !           153:                                }
        !           154:                        if (*tp)
1.1       cgd       155:                                break;
                    156:                }
1.17    ! christos  157:                if (strcmp(_fs_fstab.fs_type, FSTAB_XX) == 0)
1.1       cgd       158:                        continue;
                    159:                if (cp != NULL)
1.17    ! christos  160:                        return 1;
1.1       cgd       161:
1.17    ! christos  162: bad:
        !           163:                warnx("%s, %d: Missing fields", _fs_file, _fs_lineno);
1.1       cgd       164:        }
                    165:        /* NOTREACHED */
                    166: }
                    167:
                    168: struct fstab *
                    169: getfsent()
                    170: {
1.10      christos  171:        if ((!_fs_fp && !setfsent()) || !fstabscan())
1.17    ! christos  172:                return NULL;
        !           173:        return &_fs_fstab;
1.1       cgd       174: }
                    175:
                    176: struct fstab *
                    177: getfsspec(name)
1.12      perry     178:        const char *name;
1.1       cgd       179: {
                    180:        if (setfsent())
                    181:                while (fstabscan())
                    182:                        if (!strcmp(_fs_fstab.fs_spec, name))
1.17    ! christos  183:                                return &_fs_fstab;
        !           184:        return NULL;
1.1       cgd       185: }
                    186:
                    187: struct fstab *
                    188: getfsfile(name)
1.12      perry     189:        const char *name;
1.1       cgd       190: {
                    191:        if (setfsent())
                    192:                while (fstabscan())
                    193:                        if (!strcmp(_fs_fstab.fs_file, name))
1.17    ! christos  194:                                return &_fs_fstab;
        !           195:        return NULL;
1.1       cgd       196: }
                    197:
1.8       jtc       198: int
1.1       cgd       199: setfsent()
                    200: {
1.17    ! christos  201:        _fs_lineno = 0;
1.1       cgd       202:        if (_fs_fp) {
                    203:                rewind(_fs_fp);
1.17    ! christos  204:                return 1;
1.1       cgd       205:        }
1.17    ! christos  206:        if ((_fs_fp = fopen(_PATH_FSTAB, "r")) == NULL) {
        !           207:                warn("Cannot open `%s'", _PATH_FSTAB);
        !           208:                return 0;
        !           209:        }
        !           210:        return 1;
1.1       cgd       211: }
                    212:
                    213: void
                    214: endfsent()
                    215: {
                    216:        if (_fs_fp) {
                    217:                (void)fclose(_fs_fp);
                    218:                _fs_fp = NULL;
                    219:        }
                    220: }

CVSweb <webmaster@jp.NetBSD.org>