[BACK]Return to stat_flags.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / othersrc / fsu_compat

Annotation of othersrc/fsu_compat/stat_flags.c, Revision 1.2

1.2     ! stacktic    1: /*     $NetBSD: stat_flags.c,v 1.1 2009/11/05 14:39:16 stacktic Exp $  */
1.1       stacktic    2:
                      3: /*-
                      4:  * Copyright (c) 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      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. Neither the name of the University nor the names of its contributors
                     16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #if HAVE_NBTOOL_CONFIG_H
                     33: #include "nbtool_config.h"
                     34: #else
                     35: #define HAVE_STRUCT_STAT_ST_FLAGS 1
                     36: #endif
1.2     ! stacktic   37: #if HAVE_NBCOMPAT_H
        !            38: #include <nbcompat.h>
        !            39: #endif
1.1       stacktic   40:
                     41: #include <sys/cdefs.h>
                     42: #if !defined(lint)
                     43: #if 0
                     44: static char sccsid[] = "@(#)stat_flags.c       8.2 (Berkeley) 7/28/94";
                     45: #else
1.2     ! stacktic   46: __RCSID("$NetBSD: stat_flags.c,v 1.1 2009/11/05 14:39:16 stacktic Exp $");
1.1       stacktic   47: #endif
                     48: #endif /* not lint */
                     49:
                     50: #include <sys/types.h>
                     51: #include <sys/stat.h>
                     52: #include <stddef.h>
                     53: #include <string.h>
                     54: #include <stdlib.h>
                     55:
                     56: #include "nbsysstat.h"
                     57: #include "fsu_compat.h"
                     58:
                     59: #define        SAPPEND(s) do {                                                 \
                     60:        if (prefix != NULL)                                             \
                     61:                (void)strlcat(string, prefix, sizeof(string));          \
                     62:        (void)strlcat(string, s, sizeof(string));                       \
                     63:        prefix = ",";                                                   \
                     64: } while (/* CONSTCOND */ 0)
                     65:
                     66: /*
                     67:  * flags_to_string --
                     68:  *     Convert stat flags to a comma-separated string.  If no flags
                     69:  *     are set, return the default string.
                     70:  */
                     71: char *
                     72: flags_to_string(u_long flags, const char *def)
                     73: {
                     74:        char string[128];
                     75:        const char *prefix;
                     76:
                     77:        string[0] = '\0';
                     78:        prefix = NULL;
                     79: #if HAVE_STRUCT_STAT_ST_FLAGS
                     80:        if (flags & UF_APPEND)
                     81:                SAPPEND("uappnd");
                     82:        if (flags & UF_IMMUTABLE)
                     83:                SAPPEND("uchg");
                     84:        if (flags & UF_NODUMP)
                     85:                SAPPEND("nodump");
                     86:        if (flags & UF_OPAQUE)
                     87:                SAPPEND("opaque");
                     88:        if (flags & SF_APPEND)
                     89:                SAPPEND("sappnd");
                     90:        if (flags & SF_ARCHIVED)
                     91:                SAPPEND("arch");
                     92:        if (flags & SF_IMMUTABLE)
                     93:                SAPPEND("schg");
                     94: #ifdef SF_SNAPSHOT
                     95:        if (flags & SF_SNAPSHOT)
                     96:                SAPPEND("snap");
                     97: #endif
                     98: #endif
                     99:        if (prefix != NULL)
                    100:                return strdup(string);
                    101:        return strdup(def);
                    102: }
                    103:
                    104: #define        TEST(a, b, f) {                                                 \
                    105:        if (!strcmp(a, b)) {                                            \
                    106:                if (clear) {                                            \
                    107:                        if (clrp)                                       \
                    108:                                *clrp |= (f);                           \
                    109:                        if (setp)                                       \
                    110:                                *setp &= ~(f);                          \
                    111:                } else {                                                \
                    112:                        if (setp)                                       \
                    113:                                *setp |= (f);                           \
                    114:                        if (clrp)                                       \
                    115:                                *clrp &= ~(f);                          \
                    116:                }                                                       \
                    117:                break;                                                  \
                    118:        }                                                               \
                    119: }
                    120:
                    121: /*
                    122:  * string_to_flags --
                    123:  *     Take string of arguments and return stat flags.  Return 0 on
                    124:  *     success, 1 on failure.  On failure, stringp is set to point
                    125:  *     to the offending token.
                    126:  */
                    127: int
                    128: string_to_flags(char **stringp, u_long *setp, u_long *clrp)
                    129: {
                    130:        int clear;
                    131:        char *string, *p;
                    132:
                    133:        if (setp)
                    134:                *setp = 0;
                    135:        if (clrp)
                    136:                *clrp = 0;
                    137:
                    138: #if HAVE_STRUCT_STAT_ST_FLAGS
                    139:        string = *stringp;
                    140:        while ((p = strsep(&string, "\t ,")) != NULL) {
                    141:                clear = 0;
                    142:                *stringp = p;
                    143:                if (*p == '\0')
                    144:                        continue;
                    145:                if (p[0] == 'n' && p[1] == 'o') {
                    146:                        clear = 1;
                    147:                        p += 2;
                    148:                }
                    149:                switch (p[0]) {
                    150:                case 'a':
                    151:                        TEST(p, "arch", SF_ARCHIVED);
                    152:                        TEST(p, "archived", SF_ARCHIVED);
                    153:                        return (1);
                    154:                case 'd':
                    155:                        clear = !clear;
                    156:                        TEST(p, "dump", UF_NODUMP);
                    157:                        return (1);
                    158:                case 'n':
                    159:                                /*
                    160:                                 * Support `nonodump'. Note that
                    161:                                 * the state of clear is not changed.
                    162:                                 */
                    163:                        TEST(p, "nodump", UF_NODUMP);
                    164:                        return (1);
                    165:                case 'o':
                    166:                        TEST(p, "opaque", UF_OPAQUE);
                    167:                        return (1);
                    168:                case 's':
                    169:                        TEST(p, "sappnd", SF_APPEND);
                    170:                        TEST(p, "sappend", SF_APPEND);
                    171:                        TEST(p, "schg", SF_IMMUTABLE);
                    172:                        TEST(p, "schange", SF_IMMUTABLE);
                    173:                        TEST(p, "simmutable", SF_IMMUTABLE);
                    174:                        return (1);
                    175:                case 'u':
                    176:                        TEST(p, "uappnd", UF_APPEND);
                    177:                        TEST(p, "uappend", UF_APPEND);
                    178:                        TEST(p, "uchg", UF_IMMUTABLE);
                    179:                        TEST(p, "uchange", UF_IMMUTABLE);
                    180:                        TEST(p, "uimmutable", UF_IMMUTABLE);
                    181:                        return (1);
                    182:                default:
                    183:                        return (1);
                    184:                }
                    185:        }
                    186: #endif
                    187:
                    188:        return (0);
                    189: }

CVSweb <webmaster@jp.NetBSD.org>