Mail Archives: djgpp-workers/2001/06/11/23:40:14
Hello,
getconf is a tool I could make use of, so I implemented it. Below is my first
version.
/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#define constant_var 0
#define confstr_var 1
#define pathconf_var 2
#define sysconf_var 3
struct variable
{
const char *name;
long value;
unsigned int type;
};
const struct variable var_list[] =
{
{ "ARG_MAX", _SC_ARG_MAX, sysconf_var },
{ "CHILD_MAX", _SC_CHILD_MAX, sysconf_var },
{ "CLK_TCK", _SC_CLK_TCK, sysconf_var },
{ "LINK_MAX", _PC_LINK_MAX, pathconf_var },
{ "MAX_CANON", _PC_MAX_CANON, pathconf_var },
{ "MAX_INPUT", _PC_MAX_INPUT, pathconf_var },
{ "NAME_MAX", _PC_NAME_MAX, pathconf_var },
{ "NGROUPS_MAX", _SC_NGROUPS_MAX, sysconf_var },
{ "OPEN_MAX", _SC_OPEN_MAX, sysconf_var },
#ifdef _CS_PATH
{ "PATH", _CS_PATH, confstr_var },
#endif
{ "PATH_MAX", _PC_PATH_MAX, pathconf_var },
{ "PIPE_BUF", _PC_PIPE_BUF, pathconf_var },
{ "STREAM_MAX", _SC_STREAM_MAX, sysconf_var },
#ifdef _PC_SYMLINK_MAX
{ "SYMLINK_MAX", _PC_SYMLINK_MAX, pathconf_var },
#endif
{ "TZNAME_MAX", _SC_TZNAME_MAX, sysconf_var },
{ "_POSIX_ARG_MAX", _POSIX_ARG_MAX, constant_var },
{ "_POSIX_CHILD_MAX", _POSIX_CHILD_MAX, constant_var },
{ "_POSIX_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED, pathconf_var },
{ "_POSIX_LINK_MAX", _POSIX_LINK_MAX, constant_var },
{ "_POSIX_MAX_CANON", _POSIX_MAX_CANON, constant_var },
{ "_POSIX_MAX_INPUT", _POSIX_MAX_INPUT, constant_var },
{ "_POSIX_NAME_MAX", _POSIX_NAME_MAX, constant_var },
{ "_POSIX_NGROUPS_MAX", _POSIX_NGROUPS_MAX, constant_var },
{ "_POSIX_NO_TRUNC", _PC_NO_TRUNC, pathconf_var },
{ "_POSIX_JOB_CONTROL", _SC_JOB_CONTROL, sysconf_var },
{ "_POSIX_OPEN_MAX", _POSIX_OPEN_MAX, constant_var },
{ "_POSIX_PATH_MAX", _POSIX_PATH_MAX, constant_var },
{ "_POSIX_PIPE_BUF", _POSIX_PIPE_BUF, constant_var },
{ "_POSIX_SAVED_IDS", _SC_SAVED_IDS, sysconf_var },
{ "_POSIX_SSIZE_MAX", _POSIX_SSIZE_MAX, constant_var },
{ "_POSIX_STREAM_MAX", _POSIX_STREAM_MAX, constant_var },
{ "_POSIX_TZNAME_MAX", _POSIX_TZNAME_MAX, constant_var },
{ "_POSIX_VERSION", _SC_VERSION, sysconf_var },
{ "_POSIX_VDISABLE", _PC_VDISABLE, pathconf_var },
};
static void usage(void);
static void help(void);
int main(int argc, char *argv[])
{
char c;
const char *spec;
const char *var_name;
const struct variable *var;
int print_usage;
int i, cmp;
int beg, end;
if (argc == 1)
usage();
beg = 0;
end = sizeof(var_list) / sizeof(var_list[0]);
/* For now, any option will trigger the usage text. */
while ((c = getopt(argc, argv, "h")) != -1)
{
switch (c)
{
case 'h':
{
help();
break;
}
default:
{
usage();
break;
}
}
}
var_name = argv[optind];
while (beg <= end)
{
i = (end + beg) / 2;
cmp = strcmp (var_name, var_list[i].name);
if (cmp == 0)
{
beg = end = i;
break;
}
if (cmp > 0)
{
beg = i + 1;
}
else
{
end = i - 1;
}
}
if (beg != end)
{
fprintf(stderr, "undefined\n");
return 1;
}
var = var_list + beg;
if ((argc - optind) != ((var->type == pathconf_var) ? 2 : 1))
usage();
switch (var->type)
{
case constant_var:
{
printf("%l\n", var->value);
break;
}
case confstr_var:
{
size_t buf_len, ret;
char *buf;
buf_len = confstr(var->value, 0, 0);
buf = alloca(buf_len);
ret = confstr(var->value, buf, buf_len);
printf("%s\n", buf);
break;
}
case sysconf_var:
{
long val;
val = sysconf(var->value);
if (val != -1)
printf("%d\n", val);
else
printf("undefined\n");
break;
}
case pathconf_var:
{
long val;
val = pathconf(argv[optind + 1], var->value);
if (val != -1)
printf("%d\n", val);
else
printf("undefined\n");
break;
}
}
return 0;
}
static void
usage(void)
{
fprintf(stderr, "Usage: getopt [-h] [var] [pathvar path]\n");
exit(1);
}
static void
help(void)
{
int i;
fprintf(stderr, "Usage: getopt var\n");
for (i = 0; i < sizeof(var_list) / sizeof(var_list[0]); ++i)
{
if (var_list[0].type != pathconf_var)
fprintf(stderr, "%s\n", var_list[i].name);
}
fprintf(stderr, "\nUsage: getopt pathvar path\n");
for (i = 0; i < sizeof(var_list) / sizeof(var_list[0]); ++i)
{
if (var_list[i].type == pathconf_var)
fprintf(stderr, "%s\n", var_list[i].name);
}
}
- Raw text -