1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved. 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify it 5f08c3bdfSopenharmony_ci * under the terms of version 2 of the GNU General Public License as 6f08c3bdfSopenharmony_ci * published by the Free Software Foundation. 7f08c3bdfSopenharmony_ci * 8f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, but 9f08c3bdfSopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of 10f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 11f08c3bdfSopenharmony_ci * 12f08c3bdfSopenharmony_ci * Further, this software is distributed without any warranty that it is 13f08c3bdfSopenharmony_ci * free of the rightful claim of any third person regarding infringement 14f08c3bdfSopenharmony_ci * or the like. Any license provided herein, whether implied or 15f08c3bdfSopenharmony_ci * otherwise, applies only to this software file. Patent licenses, if 16f08c3bdfSopenharmony_ci * any, provided herein do not apply to combinations of this program with 17f08c3bdfSopenharmony_ci * other software, or any other product whatsoever. 18f08c3bdfSopenharmony_ci * 19f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License along 20f08c3bdfSopenharmony_ci * with this program; if not, write the Free Software Foundation, Inc., 21f08c3bdfSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, 24f08c3bdfSopenharmony_ci * Mountain View, CA 94043, or: 25f08c3bdfSopenharmony_ci * 26f08c3bdfSopenharmony_ci * http://www.sgi.com 27f08c3bdfSopenharmony_ci * 28f08c3bdfSopenharmony_ci * For further information regarding this notice, see: 29f08c3bdfSopenharmony_ci * 30f08c3bdfSopenharmony_ci * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ 31f08c3bdfSopenharmony_ci */ 32f08c3bdfSopenharmony_ci/************************************************************** 33f08c3bdfSopenharmony_ci * 34f08c3bdfSopenharmony_ci * OS Testing - Silicon Graphics, Inc. 35f08c3bdfSopenharmony_ci * 36f08c3bdfSopenharmony_ci * FUNCTION NAME : parse_open_flags 37f08c3bdfSopenharmony_ci * openflags2symbols 38f08c3bdfSopenharmony_ci * 39f08c3bdfSopenharmony_ci * FUNCTION TITLE : converts open flag symbols into bitmask 40f08c3bdfSopenharmony_ci * converts open flag bitmask into symbols 41f08c3bdfSopenharmony_ci * 42f08c3bdfSopenharmony_ci * SYNOPSIS: 43f08c3bdfSopenharmony_ci * int parse_open_flags(symbols, badname) 44f08c3bdfSopenharmony_ci * char *symbols; 45f08c3bdfSopenharmony_ci * char **badname; 46f08c3bdfSopenharmony_ci * 47f08c3bdfSopenharmony_ci * char *openflags2symbols(openflags, sep, mode) 48f08c3bdfSopenharmony_ci * int openflags; 49f08c3bdfSopenharmony_ci * char *sep; 50f08c3bdfSopenharmony_ci * int mode; 51f08c3bdfSopenharmony_ci * 52f08c3bdfSopenharmony_ci * AUTHOR : Richard Logan 53f08c3bdfSopenharmony_ci * 54f08c3bdfSopenharmony_ci * CO-PILOT(s) : Dean Roehrich 55f08c3bdfSopenharmony_ci * 56f08c3bdfSopenharmony_ci * INITIAL RELEASE : UNICOS 8.0 57f08c3bdfSopenharmony_ci * 58f08c3bdfSopenharmony_ci * DESIGN DESCRIPTION 59f08c3bdfSopenharmony_ci * The parse_open_flags function can be used to convert 60f08c3bdfSopenharmony_ci * a list of comma separated open(2) flag symbols (i.e. O_TRUNC) 61f08c3bdfSopenharmony_ci * into the bitmask that can be used by open(2). 62f08c3bdfSopenharmony_ci * If a symbol is unknown and <badname> is not NULL, <badname> 63f08c3bdfSopenharmony_ci * will updated to point that symbol in <string>. 64f08c3bdfSopenharmony_ci * Parse_open_flags will return -1 on this error. 65f08c3bdfSopenharmony_ci * Otherwise parse_open_flags will return the open flag bitmask. 66f08c3bdfSopenharmony_ci * If parse_open_flags returns, <string> will left unchanged. 67f08c3bdfSopenharmony_ci * 68f08c3bdfSopenharmony_ci * The openflags2symbols function attempts to convert open flag 69f08c3bdfSopenharmony_ci * bits into human readable symbols (i.e. O_TRUNC). If there 70f08c3bdfSopenharmony_ci * are more than one symbol, the <sep> string will be placed as 71f08c3bdfSopenharmony_ci * a separator between symbols. Commonly used separators would 72f08c3bdfSopenharmony_ci * be a comma "," or pipe "|". If <mode> is one and not all 73f08c3bdfSopenharmony_ci * <openflags> bits can be converted to symbols, the "UNKNOWN" 74f08c3bdfSopenharmony_ci * symbol will be added to return string. 75f08c3bdfSopenharmony_ci * Openflags2symbols will return the indentified symbols. 76f08c3bdfSopenharmony_ci * If no symbols are recognized the return value will be a empty 77f08c3bdfSopenharmony_ci * string or the "UNKNOWN" symbol. 78f08c3bdfSopenharmony_ci * 79f08c3bdfSopenharmony_ci * SPECIAL REQUIREMENTS 80f08c3bdfSopenharmony_ci * None. 81f08c3bdfSopenharmony_ci * 82f08c3bdfSopenharmony_ci * UPDATE HISTORY 83f08c3bdfSopenharmony_ci * This should contain the description, author, and date of any 84f08c3bdfSopenharmony_ci * "interesting" modifications (i.e. info should helpful in 85f08c3bdfSopenharmony_ci * maintaining/enhancing this module). 86f08c3bdfSopenharmony_ci * username description 87f08c3bdfSopenharmony_ci * ---------------------------------------------------------------- 88f08c3bdfSopenharmony_ci * rrl This code was first created during the beginning 89f08c3bdfSopenharmony_ci * of the SFS testing days. I think that was in 1993. 90f08c3bdfSopenharmony_ci * This code was updated in 05/96. 91f08c3bdfSopenharmony_ci * (05/96) openflags2symbols was written. 92f08c3bdfSopenharmony_ci * 93f08c3bdfSopenharmony_ci * BUGS/LIMITATIONS 94f08c3bdfSopenharmony_ci * Currently (05/96) all known symbols are coded into openflags2symbols. 95f08c3bdfSopenharmony_ci * If new open flags are added this code will have to updated 96f08c3bdfSopenharmony_ci * to know about them or they will not be recognized. 97f08c3bdfSopenharmony_ci * 98f08c3bdfSopenharmony_ci **************************************************************/ 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_ci#include <stdio.h> 101f08c3bdfSopenharmony_ci#include <unistd.h> 102f08c3bdfSopenharmony_ci#include <fcntl.h> 103f08c3bdfSopenharmony_ci#include <sys/param.h> 104f08c3bdfSopenharmony_ci#include <string.h> /* strcat */ 105f08c3bdfSopenharmony_ci#include "open_flags.h" 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci#define UNKNOWN_SYMBOL "UNKNOWN" 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_cistatic char Open_symbols[512]; /* space for openflags2symbols return value */ 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_cistruct open_flag_t { 112f08c3bdfSopenharmony_ci char *symbol; 113f08c3bdfSopenharmony_ci int flag; 114f08c3bdfSopenharmony_ci}; 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_cistatic struct open_flag_t Open_flags[] = { 117f08c3bdfSopenharmony_ci {"O_RDONLY", O_RDONLY}, 118f08c3bdfSopenharmony_ci {"O_WRONLY", O_WRONLY}, 119f08c3bdfSopenharmony_ci {"O_RDWR", O_RDWR}, 120f08c3bdfSopenharmony_ci {"O_SYNC", O_SYNC}, 121f08c3bdfSopenharmony_ci {"O_CREAT", O_CREAT}, 122f08c3bdfSopenharmony_ci {"O_TRUNC", O_TRUNC}, 123f08c3bdfSopenharmony_ci {"O_EXCL", O_EXCL}, 124f08c3bdfSopenharmony_ci {"O_APPEND", O_APPEND}, 125f08c3bdfSopenharmony_ci {"O_NONBLOCK", O_NONBLOCK}, 126f08c3bdfSopenharmony_ci#if O_NOCTTY 127f08c3bdfSopenharmony_ci {"O_NOCTTY", O_NOCTTY}, 128f08c3bdfSopenharmony_ci#endif 129f08c3bdfSopenharmony_ci#if O_DSYNC 130f08c3bdfSopenharmony_ci {"O_DSYNC", O_DSYNC}, 131f08c3bdfSopenharmony_ci#endif 132f08c3bdfSopenharmony_ci#if O_RSYNC 133f08c3bdfSopenharmony_ci {"O_RSYNC", O_RSYNC}, 134f08c3bdfSopenharmony_ci#endif 135f08c3bdfSopenharmony_ci#if O_ASYNC 136f08c3bdfSopenharmony_ci {"O_ASYNC", O_ASYNC}, 137f08c3bdfSopenharmony_ci#endif 138f08c3bdfSopenharmony_ci#if O_PTYIGN 139f08c3bdfSopenharmony_ci {"O_PTYIGN", O_PTYIGN}, 140f08c3bdfSopenharmony_ci#endif 141f08c3bdfSopenharmony_ci#if O_NDELAY 142f08c3bdfSopenharmony_ci {"O_NDELAY", O_NDELAY}, 143f08c3bdfSopenharmony_ci#endif 144f08c3bdfSopenharmony_ci#if O_RAW 145f08c3bdfSopenharmony_ci {"O_RAW", O_RAW}, 146f08c3bdfSopenharmony_ci#endif 147f08c3bdfSopenharmony_ci#ifdef O_SSD 148f08c3bdfSopenharmony_ci {"O_SSD", O_SSD}, 149f08c3bdfSopenharmony_ci#endif 150f08c3bdfSopenharmony_ci#if O_BIG 151f08c3bdfSopenharmony_ci {"O_BIG", O_BIG}, 152f08c3bdfSopenharmony_ci#endif 153f08c3bdfSopenharmony_ci#if O_PLACE 154f08c3bdfSopenharmony_ci {"O_PLACE", O_PLACE}, 155f08c3bdfSopenharmony_ci#endif 156f08c3bdfSopenharmony_ci#if O_RESTART 157f08c3bdfSopenharmony_ci {"O_RESTART", O_RESTART}, 158f08c3bdfSopenharmony_ci#endif 159f08c3bdfSopenharmony_ci#if O_SFSXOP 160f08c3bdfSopenharmony_ci {"O_SFSXOP", O_SFSXOP}, 161f08c3bdfSopenharmony_ci#endif 162f08c3bdfSopenharmony_ci#if O_SFS_DEFER_TM 163f08c3bdfSopenharmony_ci {"O_SFS_DEFER_TM", O_SFS_DEFER_TM}, 164f08c3bdfSopenharmony_ci#endif 165f08c3bdfSopenharmony_ci#if O_WELLFORMED 166f08c3bdfSopenharmony_ci {"O_WELLFORMED", O_WELLFORMED}, 167f08c3bdfSopenharmony_ci#endif 168f08c3bdfSopenharmony_ci#if O_LDRAW 169f08c3bdfSopenharmony_ci {"O_LDRAW", O_LDRAW}, 170f08c3bdfSopenharmony_ci#endif 171f08c3bdfSopenharmony_ci#if O_T3D 172f08c3bdfSopenharmony_ci {"O_T3D", O_T3D}, 173f08c3bdfSopenharmony_ci#endif /* O_T3D */ 174f08c3bdfSopenharmony_ci#if O_PARALLEL 175f08c3bdfSopenharmony_ci {"O_PARALLEL", O_PARALLEL}, 176f08c3bdfSopenharmony_ci {"O_FSA", O_PARALLEL | O_WELLFORMED | O_RAW}, /* short cut */ 177f08c3bdfSopenharmony_ci#endif /* O_PARALLEL */ 178f08c3bdfSopenharmony_ci#ifdef O_LARGEFILE 179f08c3bdfSopenharmony_ci {"O_LARGEFILE", O_LARGEFILE}, 180f08c3bdfSopenharmony_ci#endif 181f08c3bdfSopenharmony_ci#ifdef O_DIRECT 182f08c3bdfSopenharmony_ci {"O_DIRECT", O_DIRECT}, 183f08c3bdfSopenharmony_ci#endif 184f08c3bdfSopenharmony_ci#ifdef O_PRIV 185f08c3bdfSopenharmony_ci {"O_PRIV", O_PRIV}, 186f08c3bdfSopenharmony_ci#endif 187f08c3bdfSopenharmony_ci 188f08c3bdfSopenharmony_ci}; 189f08c3bdfSopenharmony_ci 190f08c3bdfSopenharmony_ciint parse_open_flags(char *string, char **badname) 191f08c3bdfSopenharmony_ci{ 192f08c3bdfSopenharmony_ci int bits = 0; 193f08c3bdfSopenharmony_ci char *name; 194f08c3bdfSopenharmony_ci char *cc; 195f08c3bdfSopenharmony_ci char savecc; 196f08c3bdfSopenharmony_ci int found; 197f08c3bdfSopenharmony_ci unsigned int ind; 198f08c3bdfSopenharmony_ci 199f08c3bdfSopenharmony_ci name = string; 200f08c3bdfSopenharmony_ci cc = name; 201f08c3bdfSopenharmony_ci 202f08c3bdfSopenharmony_ci while (1) { 203f08c3bdfSopenharmony_ci 204f08c3bdfSopenharmony_ci for (; ((*cc != ',') && (*cc != '\0')); cc++) ; 205f08c3bdfSopenharmony_ci savecc = *cc; 206f08c3bdfSopenharmony_ci *cc = '\0'; 207f08c3bdfSopenharmony_ci 208f08c3bdfSopenharmony_ci found = 0; 209f08c3bdfSopenharmony_ci 210f08c3bdfSopenharmony_ci for (ind = 0; 211f08c3bdfSopenharmony_ci ind < sizeof(Open_flags) / sizeof(struct open_flag_t); 212f08c3bdfSopenharmony_ci ind++) { 213f08c3bdfSopenharmony_ci if (strcmp(name, Open_flags[ind].symbol) == 0) { 214f08c3bdfSopenharmony_ci bits |= Open_flags[ind].flag; 215f08c3bdfSopenharmony_ci found = 1; 216f08c3bdfSopenharmony_ci break; 217f08c3bdfSopenharmony_ci } 218f08c3bdfSopenharmony_ci } 219f08c3bdfSopenharmony_ci 220f08c3bdfSopenharmony_ci *cc = savecc; /* restore string */ 221f08c3bdfSopenharmony_ci 222f08c3bdfSopenharmony_ci if (found == 0) { /* invalid name */ 223f08c3bdfSopenharmony_ci if (badname != NULL) 224f08c3bdfSopenharmony_ci *badname = name; 225f08c3bdfSopenharmony_ci return -1; 226f08c3bdfSopenharmony_ci } 227f08c3bdfSopenharmony_ci 228f08c3bdfSopenharmony_ci if (savecc == '\0') 229f08c3bdfSopenharmony_ci break; 230f08c3bdfSopenharmony_ci 231f08c3bdfSopenharmony_ci name = ++cc; 232f08c3bdfSopenharmony_ci 233f08c3bdfSopenharmony_ci } /* end while */ 234f08c3bdfSopenharmony_ci 235f08c3bdfSopenharmony_ci return bits; 236f08c3bdfSopenharmony_ci 237f08c3bdfSopenharmony_ci} /* end of parse_open_flags */ 238f08c3bdfSopenharmony_ci 239f08c3bdfSopenharmony_cichar *openflags2symbols(int openflags, char *sep, int mode) 240f08c3bdfSopenharmony_ci{ 241f08c3bdfSopenharmony_ci int ind; 242f08c3bdfSopenharmony_ci int size; 243f08c3bdfSopenharmony_ci int bits = openflags; 244f08c3bdfSopenharmony_ci int havesome = 0; 245f08c3bdfSopenharmony_ci 246f08c3bdfSopenharmony_ci Open_symbols[0] = '\0'; 247f08c3bdfSopenharmony_ci 248f08c3bdfSopenharmony_ci size = sizeof(Open_flags) / sizeof(struct open_flag_t); 249f08c3bdfSopenharmony_ci 250f08c3bdfSopenharmony_ci /* 251f08c3bdfSopenharmony_ci * Deal with special case of O_RDONLY. If O_WRONLY nor O_RDWR 252f08c3bdfSopenharmony_ci * bits are not set, assume O_RDONLY. 253f08c3bdfSopenharmony_ci */ 254f08c3bdfSopenharmony_ci 255f08c3bdfSopenharmony_ci if ((bits & (O_WRONLY | O_RDWR)) == 0) { 256f08c3bdfSopenharmony_ci strcat(Open_symbols, "O_RDONLY"); 257f08c3bdfSopenharmony_ci havesome = 1; 258f08c3bdfSopenharmony_ci } 259f08c3bdfSopenharmony_ci 260f08c3bdfSopenharmony_ci /* 261f08c3bdfSopenharmony_ci * Loop through all but O_RDONLY elments of Open_flags 262f08c3bdfSopenharmony_ci */ 263f08c3bdfSopenharmony_ci for (ind = 1; ind < size; ind++) { 264f08c3bdfSopenharmony_ci 265f08c3bdfSopenharmony_ci if ((bits & Open_flags[ind].flag) == Open_flags[ind].flag) { 266f08c3bdfSopenharmony_ci if (havesome) 267f08c3bdfSopenharmony_ci strcat(Open_symbols, sep); 268f08c3bdfSopenharmony_ci 269f08c3bdfSopenharmony_ci strcat(Open_symbols, Open_flags[ind].symbol); 270f08c3bdfSopenharmony_ci havesome++; 271f08c3bdfSopenharmony_ci 272f08c3bdfSopenharmony_ci /* remove flag bits from bits */ 273f08c3bdfSopenharmony_ci bits = bits & (~Open_flags[ind].flag); 274f08c3bdfSopenharmony_ci } 275f08c3bdfSopenharmony_ci } 276f08c3bdfSopenharmony_ci 277f08c3bdfSopenharmony_ci /* 278f08c3bdfSopenharmony_ci * If not all bits were identified and mode was equal to 1, 279f08c3bdfSopenharmony_ci * added UNKNOWN_SYMBOL to return string 280f08c3bdfSopenharmony_ci */ 281f08c3bdfSopenharmony_ci if (bits && mode == 1) { /* not all bits were identified */ 282f08c3bdfSopenharmony_ci if (havesome) 283f08c3bdfSopenharmony_ci strcat(Open_symbols, sep); 284f08c3bdfSopenharmony_ci strcat(Open_symbols, UNKNOWN_SYMBOL); 285f08c3bdfSopenharmony_ci } 286f08c3bdfSopenharmony_ci 287f08c3bdfSopenharmony_ci return Open_symbols; 288f08c3bdfSopenharmony_ci 289f08c3bdfSopenharmony_ci} /* end of openflags2symbols */ 290f08c3bdfSopenharmony_ci 291f08c3bdfSopenharmony_ci#ifdef UNIT_TEST 292f08c3bdfSopenharmony_ci 293f08c3bdfSopenharmony_ci/* 294f08c3bdfSopenharmony_ci * The following code provides a UNIT test main for 295f08c3bdfSopenharmony_ci * parse_open_flags and openflags2symbols functions. 296f08c3bdfSopenharmony_ci */ 297f08c3bdfSopenharmony_ci 298f08c3bdfSopenharmony_ciint main(argc, argv) 299f08c3bdfSopenharmony_ciint argc; 300f08c3bdfSopenharmony_cichar **argv; 301f08c3bdfSopenharmony_ci{ 302f08c3bdfSopenharmony_ci int bits; 303f08c3bdfSopenharmony_ci int ret; 304f08c3bdfSopenharmony_ci char *err; 305f08c3bdfSopenharmony_ci 306f08c3bdfSopenharmony_ci if (argc == 1) { 307f08c3bdfSopenharmony_ci printf("Usage: %s openflagsbits\n\t%s symbols\n", argv[0], 308f08c3bdfSopenharmony_ci argv[0]); 309f08c3bdfSopenharmony_ci exit(1); 310f08c3bdfSopenharmony_ci } 311f08c3bdfSopenharmony_ci 312f08c3bdfSopenharmony_ci if (sscanf(argv[1], "%i", &bits) == 1) { 313f08c3bdfSopenharmony_ci printf("openflags2symbols(%#o, \",\", 1) returned %s\n", 314f08c3bdfSopenharmony_ci bits, openflags2symbols(bits, ",", 1)); 315f08c3bdfSopenharmony_ci 316f08c3bdfSopenharmony_ci } else { 317f08c3bdfSopenharmony_ci ret = parse_open_flags(argv[1], &err); 318f08c3bdfSopenharmony_ci if (ret == -1) 319f08c3bdfSopenharmony_ci printf 320f08c3bdfSopenharmony_ci ("parse_open_flags(%s, &err) returned -1, err = %s\n", 321f08c3bdfSopenharmony_ci argv[0], err); 322f08c3bdfSopenharmony_ci else 323f08c3bdfSopenharmony_ci printf("parse_open_flags(%s, &err) returned %#o\n", 324f08c3bdfSopenharmony_ci argv[0], ret); 325f08c3bdfSopenharmony_ci } 326f08c3bdfSopenharmony_ci 327f08c3bdfSopenharmony_ci exit(0); 328f08c3bdfSopenharmony_ci} 329f08c3bdfSopenharmony_ci 330f08c3bdfSopenharmony_ci#endif /* end of UNIT_TEST */ 331