ubase/lsmod.c

68 lines
1.3 KiB
C
Raw Normal View History

2013-08-09 09:23:40 +00:00
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2014-06-30 18:03:41 +00:00
2014-06-14 12:13:10 +00:00
#include "text.h"
2013-08-09 09:23:40 +00:00
#include "util.h"
static void parse_modline(char *buf, char **name, char **size,
char **refcount, char **users);
static void
usage(void)
{
eprintf("usage: %s\n", argv0);
}
int
main(int argc, char *argv[])
{
const char *modfile = "/proc/modules";
FILE *fp;
2014-06-14 12:13:10 +00:00
char *buf = NULL;
2013-08-09 09:23:40 +00:00
char *name, *size, *refcount, *users;
2014-06-14 12:13:10 +00:00
size_t bufsize = 0;
2013-08-09 09:23:40 +00:00
size_t len;
ARGBEGIN {
default:
usage();
} ARGEND;
if (argc > 0)
usage();
fp = fopen(modfile, "r");
if (!fp)
eprintf("fopen %s:", modfile);
printf("%-23s Size Used by\n", "Module");
2014-06-14 12:13:10 +00:00
while (agetline(&buf, &bufsize, fp) != -1) {
2013-08-09 09:23:40 +00:00
parse_modline(buf, &name, &size, &refcount, &users);
if (!name || !size || !refcount || !users)
eprintf("invalid format: %s\n", modfile);
2013-08-09 09:23:40 +00:00
len = strlen(users) - 1;
if (users[len] == ',' || users[len] == '-')
users[len] = '\0';
printf("%-20s%8s%3s %s\n", name, size, refcount,
users);
}
2013-09-05 08:53:39 +00:00
if (ferror(fp))
eprintf("%s: read error:", modfile);
2014-06-14 12:13:10 +00:00
free(buf);
2013-08-09 09:23:40 +00:00
fclose(fp);
2014-10-02 22:45:25 +00:00
return 0;
2013-08-09 09:23:40 +00:00
}
static void
parse_modline(char *buf, char **name, char **size,
char **refcount, char **users)
{
*name = strtok(buf, " ");
*size = strtok(NULL, " ");
*refcount = strtok(NULL, " ");
*users = strtok(NULL, " ");
}