2 * Copyright (c) 2000 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 /**************************************************************
20 * OS Testing - Silicon Graphics, Inc.
22 * FUNCTION NAME : parse_open_flags
25 * FUNCTION TITLE : converts open flag symbols into bitmask
26 * converts open flag bitmask into symbols
29 * int parse_open_flags(symbols, badname)
33 * char *openflags2symbols(openflags, sep, mode)
38 * AUTHOR : Richard Logan
40 * CO-PILOT(s) : Dean Roehrich
42 * INITIAL RELEASE : UNICOS 8.0
45 * The parse_open_flags function can be used to convert
46 * a list of comma separated open(2) flag symbols (i.e. O_TRUNC)
47 * into the bitmask that can be used by open(2).
48 * If a symbol is unknown and <badname> is not NULL, <badname>
49 * will updated to point that symbol in <string>.
50 * Parse_open_flags will return -1 on this error.
51 * Otherwise parse_open_flags will return the open flag bitmask.
52 * If parse_open_flags returns, <string> will left unchanged.
54 * The openflags2symbols function attempts to convert open flag
55 * bits into human readable symbols (i.e. O_TRUNC). If there
56 * are more than one symbol, the <sep> string will be placed as
57 * a separator between symbols. Commonly used separators would
58 * be a comma "," or pipe "|". If <mode> is one and not all
59 * <openflags> bits can be converted to symbols, the "UNKNOWN"
60 * symbol will be added to return string.
61 * Openflags2symbols will return the indentified symbols.
62 * If no symbols are recognized the return value will be a empty
63 * string or the "UNKNOWN" symbol.
65 * SPECIAL REQUIREMENTS
69 * This should contain the description, author, and date of any
70 * "interesting" modifications (i.e. info should helpful in
71 * maintaining/enhancing this module).
72 * username description
73 * ----------------------------------------------------------------
74 * rrl This code was first created during the beginning
75 * of the SFS testing days. I think that was in 1993.
76 * This code was updated in 05/96.
77 * (05/96) openflags2symbols was written.
80 * Currently (05/96) all known symbols are coded into openflags2symbols.
81 * If new open flags are added this code will have to updated
82 * to know about them or they will not be recognized.
84 **************************************************************/
89 #include <sys/param.h>
90 #include <string.h> /* strcat */
91 #include "open_flags.h"
93 #define UNKNOWN_SYMBOL "UNKNOWN"
95 static char Open_symbols[512]; /* space for openflags2symbols return value */
102 static struct open_flag_t Open_flags[] = {
103 { "O_RDONLY", O_RDONLY },
104 { "O_WRONLY", O_WRONLY },
105 { "O_RDWR", O_RDWR },
106 { "O_SYNC", O_SYNC },
107 { "O_CREAT", O_CREAT },
108 { "O_TRUNC", O_TRUNC },
109 { "O_EXCL", O_EXCL },
110 { "O_APPEND", O_APPEND },
111 { "O_NONBLOCK", O_NONBLOCK },
113 { "O_NOCTTY", O_NOCTTY },
116 { "O_DSYNC", O_DSYNC },
119 { "O_RSYNC", O_RSYNC },
122 { "O_ASYNC", O_ASYNC },
125 { "O_PTYIGN", O_PTYIGN },
128 { "O_NDELAY", O_NDELAY },
140 { "O_PLACE", O_PLACE },
143 { "O_RESTART", O_RESTART },
146 { "O_SFSXOP", O_SFSXOP },
149 { "O_SFS_DEFER_TM", O_SFS_DEFER_TM },
152 { "O_WELLFORMED", O_WELLFORMED },
155 { "O_LDRAW", O_LDRAW },
161 { "O_PARALLEL", O_PARALLEL },
162 { "O_FSA", O_PARALLEL|O_WELLFORMED|O_RAW }, /* short cut */
163 #endif /* O_PARALLEL */
165 { "O_LARGEFILE", O_LARGEFILE },
168 { "O_DIRECT", O_DIRECT },
171 { "O_PRIV", O_PRIV },
177 parse_open_flags(char *string, char **badname)
191 for(; ((*cc != ',') && (*cc != '\0')); cc++);
197 for(ind=0; ind < sizeof(Open_flags)/sizeof(struct open_flag_t); ind++) {
198 if ( strcmp(name, Open_flags[ind].symbol) == 0 ) {
199 bits |= Open_flags[ind].flag;
205 *cc = savecc; /* restore string */
207 if ( found == 0 ) { /* invalid name */
208 if ( badname != NULL )
213 if ( savecc == '\0' )
222 } /* end of parse_open_flags */
226 openflags2symbols(int openflags, char *sep, int mode)
230 int bits = openflags;
233 Open_symbols[0]='\0';
235 size=sizeof(Open_flags)/sizeof(struct open_flag_t);
238 * Deal with special case of O_RDONLY. If O_WRONLY nor O_RDWR
239 * bits are not set, assume O_RDONLY.
242 if ( (bits & (O_WRONLY | O_RDWR)) == 0 ) {
243 strcat(Open_symbols, "O_RDONLY");
248 * Loop through all but O_RDONLY elments of Open_flags
250 for(ind=1; ind < size; ind++) {
252 if ( (bits & Open_flags[ind].flag) == Open_flags[ind].flag ) {
254 strcat(Open_symbols, sep);
256 strcat(Open_symbols, Open_flags[ind].symbol);
259 /* remove flag bits from bits */
260 bits = bits & (~Open_flags[ind].flag);
265 * If not all bits were identified and mode was equal to 1,
266 * added UNKNOWN_SYMBOL to return string
268 if ( bits && mode == 1 ) { /* not all bits were identified */
270 strcat(Open_symbols, sep);
271 strcat(Open_symbols, UNKNOWN_SYMBOL);
276 } /* end of openflags2symbols */
282 * The following code provides a UNIT test main for
283 * parse_open_flags and openflags2symbols functions.
296 printf("Usage: %s openflagsbits\n\t%s symbols\n", argv[0], argv[0]);
300 if ( sscanf(argv[1], "%i", &bits) == 1 ) {
301 printf("openflags2symbols(%#o, \",\", 1) returned %s\n",
302 bits, openflags2symbols(bits, ",", 1));
305 ret=parse_open_flags(argv[1], &err);
307 printf("parse_open_flags(%s, &err) returned -1, err = %s\n",
310 printf("parse_open_flags(%s, &err) returned %#o\n", argv[0], ret);
316 #endif /* end of UNIT_TEST */