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

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

CVSweb <webmaster@jp.NetBSD.org>