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