1 /* mkmisc - make misc device nodes with dynamic minor number
3 * Copyright (C) 2011 Antonio Ospite <ospite@studenti.unina.it>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* A misc device is a character device node which can have a dynamic minor
21 * http://www.linux.it/~rubini/docs/misc/misc.html
28 #include <sys/types.h>
34 static int verbose = 0;
36 static unsigned int mode = 0664;
38 static int misc_get_major(void)
45 fp = fopen("/proc/devices", "r");
47 perror("/proc/devices");
51 while (fgets(line, sizeof(line) - 1, fp)) {
52 if(sscanf(line, "%d %64s", &major, name) == 2)
53 if (strcmp(name, "misc") == 0) {
63 static int misc_get_minor(char *node)
69 fp = fopen("/proc/misc", "r");
75 while (fscanf(fp, "%d %64s", &minor, name) == 2) {
76 if (strcmp(name, node) == 0) {
86 static int mkmisc(char *device)
95 major = misc_get_major();
97 fprintf(stderr, "Cannot get misc major number\n");
101 path = strdup(device);
102 node = basename(path);
104 minor = misc_get_minor(node);
106 fprintf(stderr, "Cannot get misc minor for %s\n", device);
113 printf("Creating device: %s, major: %d, minor: %d\n",
114 device, major, minor);
117 ret = unlink(device);
126 dev = makedev(major, minor);
128 ret = mknod(device, mode, dev);
137 static void usage(void)
139 printf("Usage: mkmisc [OPTION]... NAME\n");
140 printf("Create the special file NAME as a misc device with a dynamic minor number.\n");
142 printf("OPTION can be any of:\n");
143 printf(" -f force creation even if the file already exists\n");
144 printf(" -m MODE set file permission bits to MODE, not a=rw - umask\n");
145 printf(" -v verbose output\n");
146 printf(" -h display this help and exit\n");
149 static void parse_options(int argc, char *argv[])
154 while ((c = getopt (argc, argv, "fm:vh")) != -1)
160 ret = sscanf(optarg, "0%o", &mode);
162 fprintf(stderr, "mode must be octal (e.g 0664)\n");
173 /* skip known options with missing arguments */
177 "Unknown option `-%c'.\n",
181 "Unknown option character `\\x%x'.\n",
190 int main(int argc, char *argv[])
195 parse_options(argc, argv);
197 if (optind >= argc) {
198 fprintf(stderr, "Missing argument NAME\n\n");
201 } else if ((argc - optind) > 1) {
202 fprintf(stderr, "Too many arguments\n\n");
207 device = strdup(argv[optind]);
209 ret = mkmisc(device);
211 fprintf(stderr, "Cannot create %s\n", device);