1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000 Silicon Graphics, Inc.
6 /**************************************************************
8 * OS Testing - Silicon Graphics, Inc.
10 * FUNCTION NAME : parse_open_flags
13 * FUNCTION TITLE : converts open flag symbols into bitmask
14 * converts open flag bitmask into symbols
17 * int parse_open_flags(symbols, badname)
21 * char *openflags2symbols(openflags, sep, mode)
26 * AUTHOR : Richard Logan
28 * CO-PILOT(s) : Dean Roehrich
30 * INITIAL RELEASE : UNICOS 8.0
33 * The parse_open_flags function can be used to convert
34 * a list of comma separated open(2) flag symbols (i.e. O_TRUNC)
35 * into the bitmask that can be used by open(2).
36 * If a symbol is unknown and <badname> is not NULL, <badname>
37 * will updated to point that symbol in <string>.
38 * Parse_open_flags will return -1 on this error.
39 * Otherwise parse_open_flags will return the open flag bitmask.
40 * If parse_open_flags returns, <string> will left unchanged.
42 * The openflags2symbols function attempts to convert open flag
43 * bits into human readable symbols (i.e. O_TRUNC). If there
44 * are more than one symbol, the <sep> string will be placed as
45 * a separator between symbols. Commonly used separators would
46 * be a comma "," or pipe "|". If <mode> is one and not all
47 * <openflags> bits can be converted to symbols, the "UNKNOWN"
48 * symbol will be added to return string.
49 * Openflags2symbols will return the indentified symbols.
50 * If no symbols are recognized the return value will be a empty
51 * string or the "UNKNOWN" symbol.
53 * SPECIAL REQUIREMENTS
57 * This should contain the description, author, and date of any
58 * "interesting" modifications (i.e. info should helpful in
59 * maintaining/enhancing this module).
60 * username description
61 * ----------------------------------------------------------------
62 * rrl This code was first created during the beginning
63 * of the SFS testing days. I think that was in 1993.
64 * This code was updated in 05/96.
65 * (05/96) openflags2symbols was written.
68 * Currently (05/96) all known symbols are coded into openflags2symbols.
69 * If new open flags are added this code will have to updated
70 * to know about them or they will not be recognized.
72 **************************************************************/
77 #include <sys/param.h>
78 #include <string.h> /* strcat */
79 #include "open_flags.h"
81 #define UNKNOWN_SYMBOL "UNKNOWN"
83 static char Open_symbols[512]; /* space for openflags2symbols return value */
90 static struct open_flag_t Open_flags[] = {
91 { "O_RDONLY", O_RDONLY },
92 { "O_WRONLY", O_WRONLY },
95 { "O_CREAT", O_CREAT },
96 { "O_TRUNC", O_TRUNC },
98 { "O_APPEND", O_APPEND },
99 { "O_NONBLOCK", O_NONBLOCK },
101 { "O_NOCTTY", O_NOCTTY },
104 { "O_DSYNC", O_DSYNC },
107 { "O_RSYNC", O_RSYNC },
110 { "O_ASYNC", O_ASYNC },
113 { "O_PTYIGN", O_PTYIGN },
116 { "O_NDELAY", O_NDELAY },
128 { "O_PLACE", O_PLACE },
131 { "O_RESTART", O_RESTART },
134 { "O_SFSXOP", O_SFSXOP },
137 { "O_SFS_DEFER_TM", O_SFS_DEFER_TM },
140 { "O_WELLFORMED", O_WELLFORMED },
143 { "O_LDRAW", O_LDRAW },
149 { "O_PARALLEL", O_PARALLEL },
150 { "O_FSA", O_PARALLEL|O_WELLFORMED|O_RAW }, /* short cut */
151 #endif /* O_PARALLEL */
153 { "O_LARGEFILE", O_LARGEFILE },
156 { "O_DIRECT", O_DIRECT },
159 { "O_PRIV", O_PRIV },
165 parse_open_flags(char *string, char **badname)
179 for(; ((*cc != ',') && (*cc != '\0')); cc++);
185 for(ind=0; ind < sizeof(Open_flags)/sizeof(struct open_flag_t); ind++) {
186 if ( strcmp(name, Open_flags[ind].symbol) == 0 ) {
187 bits |= Open_flags[ind].flag;
193 *cc = savecc; /* restore string */
195 if ( found == 0 ) { /* invalid name */
196 if ( badname != NULL )
201 if ( savecc == '\0' )
210 } /* end of parse_open_flags */
214 openflags2symbols(int openflags, char *sep, int mode)
218 int bits = openflags;
221 Open_symbols[0]='\0';
223 size=sizeof(Open_flags)/sizeof(struct open_flag_t);
226 * Deal with special case of O_RDONLY. If O_WRONLY nor O_RDWR
227 * bits are not set, assume O_RDONLY.
230 if ( (bits & (O_WRONLY | O_RDWR)) == 0 ) {
231 strcat(Open_symbols, "O_RDONLY");
236 * Loop through all but O_RDONLY elments of Open_flags
238 for(ind=1; ind < size; ind++) {
240 if ( (bits & Open_flags[ind].flag) == Open_flags[ind].flag ) {
242 strcat(Open_symbols, sep);
244 strcat(Open_symbols, Open_flags[ind].symbol);
247 /* remove flag bits from bits */
248 bits = bits & (~Open_flags[ind].flag);
253 * If not all bits were identified and mode was equal to 1,
254 * added UNKNOWN_SYMBOL to return string
256 if ( bits && mode == 1 ) { /* not all bits were identified */
258 strcat(Open_symbols, sep);
259 strcat(Open_symbols, UNKNOWN_SYMBOL);
264 } /* end of openflags2symbols */
270 * The following code provides a UNIT test main for
271 * parse_open_flags and openflags2symbols functions.
284 printf("Usage: %s openflagsbits\n\t%s symbols\n", argv[0], argv[0]);
288 if ( sscanf(argv[1], "%i", &bits) == 1 ) {
289 printf("openflags2symbols(%#o, \",\", 1) returned %s\n",
290 bits, openflags2symbols(bits, ",", 1));
293 ret=parse_open_flags(argv[1], &err);
295 printf("parse_open_flags(%s, &err) returned -1, err = %s\n",
298 printf("parse_open_flags(%s, &err) returned %#o\n", argv[0], ret);
304 #endif /* end of UNIT_TEST */