chiark / gitweb /
91d3a581953c8e05521b2b33da6cb137bdaa9521
[elogind.git] / extras / floppy / create_floppy_devices.c
1 /*
2  * create_floppy_devices
3  *
4  * Create all possible floppy device based on the CMOS type.
5  * Based upon code from drivers/block/floppy.c
6  *
7  * Copyright(C) 2005, SUSE Linux Products GmbH
8  *
9  * Author:
10  *      Hannes Reinecke <hare@suse.de>
11  *
12  *      This program is free software; you can redistribute it and/or modify it
13  *      under the terms of the GNU General Public License as published by the
14  *      Free Software Foundation version 2 of the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <string.h>
24
25 static char *table[] = {
26         "", "d360", "h1200", "u360", "u720", "h360", "h720",
27         "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
28         "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
29         "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
30         "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
31         NULL
32 };
33
34 static int t360[] = { 1, 0 };
35 static int t1200[] = { 2, 5, 6, 10, 12, 14, 16, 18, 20, 23, 0 };
36 static int t3in[] = { 8, 9, 26, 27, 28, 7, 11, 15, 19, 24, 25, 29, 31, 3, 4, 13, 17, 21, 22, 30, 0 };
37
38 static int *table_sup[] = { NULL, t360, t1200, t3in + 5 + 8, t3in + 5, t3in, t3in };
39
40 int main(int argc, char **argv)
41 {
42         char *dev;
43         char node[64];
44         int type = 0, i, fdnum, c;
45         int major = 2, minor;
46         int mode = 0;
47         int create_nodes = 0;
48         int print_nodes = 0;
49         int unlink_nodes = 0;
50         int is_err = 0;
51
52         while ((c = getopt(argc, argv, "cdm:M:t:u")) != -1) {
53                 switch (c) {
54                 case 'c':
55                         create_nodes = 1;
56                         unlink_nodes = 0;
57                         break;
58                 case 'd':
59                         print_nodes = 1;
60                         break;
61                 case 'M':
62                         mode = strtol(optarg, NULL, 10);
63                         break;
64                 case 'm':
65                         major = strtol(optarg, NULL, 10);
66                         break;
67                 case 't':
68                         type = strtol(optarg, NULL, 10);
69                         break;
70                 case 'u':
71                         unlink_nodes = 1;
72                         create_nodes = 0;
73                         break;
74                 default:
75                         is_err++;
76                         break;
77                 }
78         }
79
80         if (is_err || optind >= argc) {
81                 fprintf(stderr,"Usage: %s [-d|-c|-u|-m <major>|-t <type>] <device>\n",
82                         argv[0]);
83                 return 1;
84         }
85
86         dev = argv[optind];
87         if (dev[strlen(dev) - 3] != 'f' || dev[strlen(dev) -2 ] != 'd') {
88                 fprintf(stderr,"Device '%s' is not a floppy device\n", dev);
89                 return 1;
90         }
91
92         fdnum = strtol(dev + 2, NULL, 10);
93         if (fdnum < 0 || fdnum > 7) {
94                 fprintf(stderr,"Floppy device number %d out of range (0-7)\n", fdnum);
95                 return 1;
96         }
97         if (fdnum > 3)
98                 fdnum += 128;
99
100         if (major < 1) {
101                 fprintf(stderr,"Invalid major number %d\n", major);
102                 return 1;
103         }
104
105         if (type < 0 || type > (int) sizeof(table)) {
106                 fprintf(stderr,"Invalid CMOS type %d\n", type);
107                 return 1;
108         }
109
110         if (type == 0)
111                 return 0;
112
113         i = 0;
114         while (table_sup[type][i]) {
115                 sprintf(node, "%s%s",dev, table[table_sup[type][i]]);
116                 minor = (table_sup[type][i] << 2) + fdnum;
117                 if (print_nodes)
118                         printf("%s b %d %d %d\n", node, mode, major, minor);
119                 if (create_nodes)
120                         mknod(node, S_IFBLK | mode, makedev(major,minor));
121                 i++;
122         }
123
124         return 0;
125 }
126