Tizen 2.0 Release
[external/module-init-tools.git] / lsmod.c
1 /* lsmod.c: list the status of kernel modules.
2     Copyright (C) 2002  Rusty Russell, IBM Corporation.
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/mman.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <asm/unistd.h>
29
30 #include "testing.h"
31
32 static void print_usage(const char *progname)
33 {
34         fprintf(stderr, "Usage: %s\n", progname);
35         exit(1);
36 }
37
38 int main(int argc, char *argv[])
39 {
40         char line[4096];
41         FILE *file;
42
43         if (argc != 1)
44                 print_usage("lsmod");
45
46         file = fopen("/proc/modules", "r");
47         if (!file) {
48                 perror("Opening /proc/modules");
49                 exit(1);
50         }
51
52         printf("Module                  Size  Used by\n");
53         while (fgets(line, sizeof(line), file)) {
54                 char *tok;
55
56                 tok = strtok(line, " \t");
57                 printf("%-19s", tok);
58                 tok = strtok(NULL, " \t\n");
59                 printf(" %8s", tok);
60                 tok = strtok(NULL, " \t\n");
61                 /* Null if no module unloading support. */
62                 if (tok) {
63                         printf("  %s", tok);
64                         tok = strtok(NULL, "\n");
65                         if (!tok)
66                                 tok = "";
67                         /* New-style has commas, or -.  If so,
68                            truncate (other fields might follow). */
69                         else if (strchr(tok, ',')) {
70                                 tok = strtok(tok, "\t ");
71                                 /* Strip trailing comma. */
72                                 if (tok[strlen(tok)-1] == ',')
73                                         tok[strlen(tok)-1] = '\0';
74                         } else if (tok[0] == '-'
75                                    && (tok[1] == '\0' || isspace(tok[1])))
76                                 tok = "";
77                         printf(" %s", tok);
78                 }
79                 printf("\n");
80         }
81         fclose(file);
82         exit(0);
83 }