ubase

suckless linux base utils
git clone git://git.suckless.org/ubase
Log | Files | Refs | README | LICENSE

lsmod.c (1357B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 #include "text.h"
      7 #include "util.h"
      8 
      9 static void parse_modline(char *buf, char **name, char **size,
     10 			  char **refcount, char **users);
     11 
     12 static void
     13 usage(void)
     14 {
     15 	eprintf("usage: %s\n", argv0);
     16 }
     17 
     18 int
     19 main(int argc, char *argv[])
     20 {
     21 	const char *modfile = "/proc/modules";
     22 	FILE *fp;
     23 	char *buf = NULL;
     24 	char *name, *size, *refcount, *users;
     25 	size_t bufsize = 0;
     26 	size_t len;
     27 
     28 	ARGBEGIN {
     29 	default:
     30 		usage();
     31 	} ARGEND;
     32 
     33 	if (argc > 0)
     34 		usage();
     35 
     36 	fp = fopen(modfile, "r");
     37 	if (!fp)
     38 		eprintf("fopen %s:", modfile);
     39 
     40 	printf("%-23s Size  Used by\n", "Module");
     41 
     42 	while (agetline(&buf, &bufsize, fp) != -1) {
     43 		parse_modline(buf, &name, &size, &refcount, &users);
     44 		if (!name || !size || !refcount || !users)
     45 			eprintf("invalid format: %s\n", modfile);
     46 		len = strlen(users) - 1;
     47 		if (users[len] == ',' || users[len] == '-')
     48 			users[len] = '\0';
     49 		printf("%-20s%8s%3s %s\n", name, size, refcount,
     50 		       users);
     51 	}
     52 	if (ferror(fp))
     53 		eprintf("%s: read error:", modfile);
     54 	free(buf);
     55 	fclose(fp);
     56 	return 0;
     57 }
     58 
     59 static void
     60 parse_modline(char *buf, char **name, char **size,
     61 	      char **refcount, char **users)
     62 {
     63 	*name = strtok(buf, " ");
     64 	*size = strtok(NULL, " ");
     65 	*refcount = strtok(NULL, " ");
     66 	*users = strtok(NULL, " ");
     67 }