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

1.14    ! mycroft     1: /*     $NetBSD: fstab.c,v 1.13 1998/07/26 13:47:20 mycroft 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.14    ! mycroft    41: __RCSID("$NetBSD: fstab.c,v 1.13 1998/07/26 13:47:20 mycroft Exp $");
1.6       cgd        42: #endif
1.1       cgd        43: #endif /* LIBC_SCCS and not lint */
                     44:
1.7       cgd        45: #include <sys/types.h>
                     46: #include <sys/uio.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;
                     64: static struct fstab _fs_fstab;
1.6       cgd        65:
1.8       jtc        66: static void error __P((int));
                     67: static int fstabscan __P((void));
1.1       cgd        68:
1.8       jtc        69: static int
1.1       cgd        70: fstabscan()
                     71: {
1.12      perry      72:        char *cp;
1.1       cgd        73: #define        MAXLINELENGTH   1024
                     74:        static char line[MAXLINELENGTH];
                     75:        char subline[MAXLINELENGTH];
                     76:        int typexx;
1.10      christos   77:        static const char sep[] = ":\n";
                     78:        static const char ws[] = " \t\n";
1.1       cgd        79:
                     80:        for (;;) {
                     81:                if (!(cp = fgets(line, sizeof(line), _fs_fp)))
                     82:                        return(0);
                     83: /* OLD_STYLE_FSTAB */
                     84:                if (!strpbrk(cp, " \t")) {
1.10      christos   85:                        _fs_fstab.fs_spec = strtok(cp, sep);
1.4       pk         86:                        if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
                     87:                                continue;
1.10      christos   88:                        _fs_fstab.fs_file = strtok(NULL, sep);
                     89:                        _fs_fstab.fs_type = strtok(NULL, sep);
1.1       cgd        90:                        if (_fs_fstab.fs_type) {
                     91:                                if (!strcmp(_fs_fstab.fs_type, FSTAB_XX))
                     92:                                        continue;
                     93:                                _fs_fstab.fs_mntops = _fs_fstab.fs_type;
                     94:                                _fs_fstab.fs_vfstype =
                     95:                                    strcmp(_fs_fstab.fs_type, FSTAB_SW) ?
                     96:                                    "ufs" : "swap";
1.10      christos   97:                                if ((cp = strtok(NULL, sep)) != NULL) {
1.1       cgd        98:                                        _fs_fstab.fs_freq = atoi(cp);
1.10      christos   99:                                        if ((cp = strtok(NULL, sep)) != NULL) {
1.1       cgd       100:                                                _fs_fstab.fs_passno = atoi(cp);
                    101:                                                return(1);
                    102:                                        }
                    103:                                }
                    104:                        }
                    105:                        goto bad;
                    106:                }
                    107: /* OLD_STYLE_FSTAB */
1.10      christos  108:                _fs_fstab.fs_spec = strtok(cp, ws);
1.1       cgd       109:                if (!_fs_fstab.fs_spec || *_fs_fstab.fs_spec == '#')
                    110:                        continue;
1.10      christos  111:                _fs_fstab.fs_file = strtok(NULL, ws);
                    112:                _fs_fstab.fs_vfstype = strtok(NULL, ws);
                    113:                _fs_fstab.fs_mntops = strtok(NULL, ws);
1.1       cgd       114:                if (_fs_fstab.fs_mntops == NULL)
                    115:                        goto bad;
                    116:                _fs_fstab.fs_freq = 0;
                    117:                _fs_fstab.fs_passno = 0;
1.10      christos  118:                if ((cp = strtok(NULL, ws)) != NULL) {
1.1       cgd       119:                        _fs_fstab.fs_freq = atoi(cp);
1.10      christos  120:                        if ((cp = strtok(NULL, ws)) != NULL)
1.1       cgd       121:                                _fs_fstab.fs_passno = atoi(cp);
                    122:                }
1.9       mrg       123:                (void)strncpy(subline, _fs_fstab.fs_mntops, sizeof(subline)-1);
1.1       cgd       124:                for (typexx = 0, cp = strtok(subline, ","); cp;
                    125:                     cp = strtok((char *)NULL, ",")) {
                    126:                        if (strlen(cp) != 2)
                    127:                                continue;
                    128:                        if (!strcmp(cp, FSTAB_RW)) {
                    129:                                _fs_fstab.fs_type = FSTAB_RW;
                    130:                                break;
                    131:                        }
                    132:                        if (!strcmp(cp, FSTAB_RQ)) {
                    133:                                _fs_fstab.fs_type = FSTAB_RQ;
                    134:                                break;
                    135:                        }
                    136:                        if (!strcmp(cp, FSTAB_RO)) {
                    137:                                _fs_fstab.fs_type = FSTAB_RO;
                    138:                                break;
                    139:                        }
                    140:                        if (!strcmp(cp, FSTAB_SW)) {
                    141:                                _fs_fstab.fs_type = FSTAB_SW;
                    142:                                break;
                    143:                        }
                    144:                        if (!strcmp(cp, FSTAB_XX)) {
                    145:                                _fs_fstab.fs_type = FSTAB_XX;
                    146:                                typexx++;
                    147:                                break;
                    148:                        }
                    149:                }
                    150:                if (typexx)
                    151:                        continue;
                    152:                if (cp != NULL)
                    153:                        return(1);
                    154:
                    155: bad:           /* no way to distinguish between EOF and syntax error */
                    156:                error(EFTYPE);
                    157:        }
                    158:        /* NOTREACHED */
                    159: }
                    160:
                    161: struct fstab *
                    162: getfsent()
                    163: {
1.10      christos  164:        if ((!_fs_fp && !setfsent()) || !fstabscan())
1.1       cgd       165:                return((struct fstab *)NULL);
                    166:        return(&_fs_fstab);
                    167: }
                    168:
                    169: struct fstab *
                    170: getfsspec(name)
1.12      perry     171:        const char *name;
1.1       cgd       172: {
                    173:        if (setfsent())
                    174:                while (fstabscan())
                    175:                        if (!strcmp(_fs_fstab.fs_spec, name))
                    176:                                return(&_fs_fstab);
                    177:        return((struct fstab *)NULL);
                    178: }
                    179:
                    180: struct fstab *
                    181: getfsfile(name)
1.12      perry     182:        const char *name;
1.1       cgd       183: {
                    184:        if (setfsent())
                    185:                while (fstabscan())
                    186:                        if (!strcmp(_fs_fstab.fs_file, name))
                    187:                                return(&_fs_fstab);
                    188:        return((struct fstab *)NULL);
                    189: }
                    190:
1.8       jtc       191: int
1.1       cgd       192: setfsent()
                    193: {
                    194:        if (_fs_fp) {
                    195:                rewind(_fs_fp);
                    196:                return(1);
                    197:        }
1.10      christos  198:        if ((_fs_fp = fopen(_PATH_FSTAB, "r")) != NULL)
1.1       cgd       199:                return(1);
                    200:        error(errno);
                    201:        return(0);
                    202: }
                    203:
                    204: void
                    205: endfsent()
                    206: {
                    207:        if (_fs_fp) {
                    208:                (void)fclose(_fs_fp);
                    209:                _fs_fp = NULL;
                    210:        }
                    211: }
                    212:
1.8       jtc       213: static void
1.1       cgd       214: error(err)
                    215:        int err;
                    216: {
                    217:
1.14    ! mycroft   218:        errno = err;
        !           219:        warn(_PATH_FSTAB);
1.1       cgd       220: }

CVSweb <webmaster@jp.NetBSD.org>