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

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

CVSweb <webmaster@jp.NetBSD.org>