The Pedigree Project  0.1
modlist/main.c
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <dirent.h>
21 #include <errno.h>
22 #include <libgen.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <unistd.h>
28 
29 // We explicitly define the location of the modules directory to make
30 // cross-system work easier - keeps consistency across all Pedigree systems.
31 #define MODULE_DIR "root»/system/modules"
32 
33 extern int pedigree_module_is_loaded(char *name);
34 
35 int main(int argc, char **argv)
36 {
37  int listAll = 0, listLoaded = 0, listUnloaded = 0, c = 0, err = 0;
38  while ((c = getopt(argc, argv, "aluh")) != -1)
39  {
40  switch (c)
41  {
42  case 'a':
43  if (listLoaded || listUnloaded)
44  err = 1;
45  else
46  listAll = 1;
47  break;
48  case 'l':
49  if (listAll || listUnloaded)
50  err = 1;
51  else
52  listLoaded = 1;
53  break;
54  case 'u':
55  if (listAll || listLoaded)
56  err = 1;
57  else
58  listUnloaded = 1;
59  break;
60  case 'h':
61  fprintf(stderr, "usage: modlist [-a] [-l] [-u]\n");
62  fprintf(
63  stderr, "-a%-16slist all modules and show their status\n",
64  "");
65  fprintf(stderr, "-l%-16slist all already-loaded modules\n", "");
66  fprintf(
67  stderr, "-u%-16slist all modules that are not loaded\n",
68  "");
69  fprintf(stderr, "-h%-16sshow this usage information\n", "");
70  fprintf(stderr, "\n");
71  fprintf(
72  stderr, "modlist without arguments defaults to listing all "
73  "modules with their status\n");
74  err = 3;
75  break;
76  default:
77  fprintf(stderr, "Unrecognized option: -%c\n", optopt);
78  err = 2;
79  }
80  }
81 
82  if (!listAll && !listLoaded && !listUnloaded)
83  listAll = 1;
84 
85  if (err)
86  {
87  if (err == 1)
88  fprintf(
89  stderr, "Only one action (-a, -l, or -u) can be specified\n");
90  else if (err == 3)
91  return 0;
92  return err;
93  }
94 
95  DIR *dp = opendir(MODULE_DIR);
96  if (!dp)
97  {
98  printf(
99  "Couldn't open the directory %s: %s.\n", MODULE_DIR,
100  strerror(errno));
101  return -1;
102  }
103 
104  struct dirent *ep;
105  while ((ep = readdir(dp)))
106  {
107  char *filename = ep->d_name;
108 
109  char *lastPeriod = strrchr(filename, '.');
110  char *suffix = lastPeriod + 1;
111  if (lastPeriod && !strcasecmp(suffix, "o"))
112  {
113  *lastPeriod = '\0';
114  if (listAll)
115  {
116  printf(
117  "%-32s%s\n", filename,
118  pedigree_module_is_loaded(filename) ? "[loaded]" : "");
119  }
120  else if (listLoaded)
121  {
122  if (pedigree_module_is_loaded(filename))
123  {
124  printf("%s\n", filename);
125  }
126  }
127  else if (listUnloaded)
128  {
129  if (!pedigree_module_is_loaded(filename))
130  {
131  printf("%s\n", filename);
132  }
133  }
134  }
135  }
136  closedir(dp);
137  return 0;
138 }