chiark / gitweb /
EXTRAS: cleanup and sync all Makefiles
[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  *      This library is free software; you can redistribute it and/or
10  *      modify it under the terms of the GNU Lesser General Public
11  *      License as published by the Free Software Foundation; either
12  *      version 2.1 of the License, or (at your option) any later version.
13  *
14  *      This library is distributed in the hope that it will be useful,
15  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  *      Lesser General Public License for more details.
18  *
19  *      You should have received a copy of the GNU Lesser General Public
20  *      License along with this library; if not, write to the Free Software
21  *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  * Author: Hannes Reinecke <hare@suse.de>
24  *
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <string.h>
34
35 static char *table[] = {
36         "", "d360", "h1200", "u360", "u720", "h360", "h720",
37         "u1440", "u2880", "CompaQ", "h1440", "u1680", "h410",
38         "u820", "h1476", "u1722", "h420", "u830", "h1494", "u1743",
39         "h880", "u1040", "u1120", "h1600", "u1760", "u1920",
40         "u3200", "u3520", "u3840", "u1840", "u800", "u1600",
41         NULL
42 };
43
44 static int t360[] = { 1 };
45 static int t1200[] = { 2, 5, 6, 10, 12, 14, 16, 18, 20, 23 };
46 static int t3in[] = { 8, 9, 26, 27, 28, 7, 11, 15, 19, 24, 25, 29, 31, 3, 4, 13, 17, 21, 22, 30 };
47
48 static int *table_sup[] = { NULL, t360, t1200, t3in + 5 + 8, t3in + 5, t3in, t3in };
49
50 int main(int argc, char **argv)
51 {
52         char *dev;
53         char node[64];
54         int type = 0, i, fdnum, c;
55         int major = 2, minor;
56         int mode = 0;
57         int create_nodes = 0;
58         int print_nodes = 0;
59         int unlink_nodes = 0;
60         int is_err = 0;
61
62         while ((c = getopt(argc, argv, "cdm:M:t:u")) != -1) {
63                 switch (c) {
64                 case 'c':
65                         create_nodes = 1;
66                         unlink_nodes = 0;
67                         break;
68                 case 'd':
69                         print_nodes = 1;
70                         break;
71                 case 'M':
72                         mode = strtol(optarg, NULL, 10);
73                         break;
74                 case 'm':
75                         major = strtol(optarg, NULL, 10);
76                         break;
77                 case 't':
78                         type = strtol(optarg, NULL, 10);
79                         break;
80                 case 'u':
81                         unlink_nodes = 1;
82                         create_nodes = 0;
83                         break;
84                 default:
85                         is_err++;
86                         break;
87                 }
88         }
89
90         if (is_err || optind >= argc) {
91                 fprintf(stderr,"Usage: %s [-d|-c|-u|-m <major>|-t <type>] <device>\n",
92                         argv[0]);
93                 return 1;
94         }
95
96         dev = argv[optind];
97         if (dev[strlen(dev) - 3] != 'f' || dev[strlen(dev) -2 ] != 'd') {
98                 fprintf(stderr,"Device '%s' is not a floppy device\n", dev);
99                 return 1;
100         }
101
102         fdnum = strtol(dev + 2, NULL, 10);
103         if (fdnum < 0 || fdnum > 7) {
104                 fprintf(stderr,"Floppy device number %d out of range (0-7)\n", fdnum);
105                 return 1;
106         }
107         if (fdnum > 3)
108                 fdnum += 128;
109
110         if (major < 1) {
111                 fprintf(stderr,"Invalid major number %d\n", major);
112                 return 1;
113         }
114
115         if (type < 0 || type > (int) sizeof(table)) {
116                 fprintf(stderr,"Invalid CMOS type %d\n", type);
117                 return 1;
118         }
119
120         if (type == 0)
121                 return 0;
122
123         i = 0;
124         while (table_sup[type][i]) {
125                 sprintf(node,"%s%s",dev,table[table_sup[type][i]]);
126                 minor = (table_sup[type][i] << 2) + fdnum;
127                 if (print_nodes)
128                         printf("%s b %d %d %d\n", node, mode, major, minor);
129                 if (create_nodes)
130                         mknod(node, S_IFBLK | mode, makedev(major,minor));
131                 i++;
132         }
133
134         return 0;
135 }
136