sortix-mirror/utils/kernelinfo.c

80 lines
1.9 KiB
C
Raw Normal View History

/*
* Copyright (c) 2012 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* kernelinfo.c
* Prints a kernel information string.
*/
#include <sys/kernelinfo.h>
2016-10-01 13:47:19 +00:00
#include <err.h>
#include <errno.h>
#include <error.h>
2016-10-01 13:47:19 +00:00
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
for ( int i = 1; i < argc; i++ )
{
const char* arg = argv[i];
if ( arg[0] != '-' || !arg[1] )
continue;
argv[i] = NULL;
if ( !strcmp(arg, "--") )
break;
if ( arg[1] != '-' )
{
2016-02-28 23:40:20 +00:00
char c;
while ( (c = *++arg) ) switch ( c )
{
default:
2016-10-01 13:47:19 +00:00
errx(1, "unknown option -- '%c'\n", c);
}
}
else
2016-10-01 13:47:19 +00:00
errx(1, "unknown option: %s\n", arg);
}
size_t bufsize = 32;
char* buf = (char*) malloc(bufsize);
2015-06-10 21:24:25 +00:00
if ( !buf )
error(1, errno, "malloc");
for ( int i = 1; i < argc; i++ )
{
2016-02-28 23:40:20 +00:00
ssize_t ret;
retry:
2016-02-28 23:40:20 +00:00
ret = kernelinfo(argv[i], buf, bufsize);
if ( ret < 0 )
{
if ( errno == EINVAL )
{
error(1, 0, "%s: No such kernel string", argv[i]);
}
error(1, errno, "kernelinfo(\"%s\")", argv[i]);
}
if ( ret )
{
buf = (char*) realloc(buf, ret);
2015-06-10 21:24:25 +00:00
if ( !buf )
error(1, errno, "realloc");
bufsize = ret;
goto retry;
}
printf("%s\n", buf);
}
return 0;
}