char *
openflags2symbols(openflags, sep, mode)
int openflags;
char *sep;
int mode;
The openflags2symbols function attempts to convert open flag bits into human readable symbols (i.e. O_TRUNC). If there are more than one symbol, the sep string will be placed as a separator between symbols. Commonly used separators would be a comma "," or pipe "|". If mode is one and not all openflags bits can be converted to symbols, the UNKNOWN symbol will be added to return string. Openflags2symbols will return the identified symbols. If no symbols are recognized the return value will be a empty string or the UNKNOWN symbol.
If the O_WRONLY and O_RDWR bits are not set, openflags2symbols assumes that O_RDONLY symbol is wanted.
/*
* The following code provides a UNIT test main for
* parse_open_flags and openflags2symbols functions.
*/
#include <stdio.h>
main(argc, argv)
int argc;
char **argv;
{
int bits;
int ret;
char *err;
if (argc == 1 ) {
printf("Usage: %s openflagsbitst%s symbols, argv[0], argv[0]);
exit(1);
}
if ( sscanf(argv[1], "%i", &bits) == 1 ) {
printf("openflags2symbols(%#o, bits, openflags2symbols(bits, ",", 1));
} else {
ret=parse_open_flags(argv[1], &err);
if ( ret == -1 )
printf("parse_open_flags(%s, &err) returned -1, err = %s,
argv[0], err);
else
printf("parse_open_flags(%s, &err) returned %#o, argv[0], ret);
}
exit(0);
}
The static space where openflags2symbols stores open flag symbols with callers separators is limited to 512 characters. This should be large enough for most open flags symbols as long as the separator is kept short.