Extract usage information into a separate file.
[platform/upstream/busybox.git] / miscutils / makedevs.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
4  * 
5  * makedevs
6  * Make ranges of device files quickly. 
7  * known bugs: can't deal with alpha ranges
8  */
9
10 #include "internal.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18
19 int makedevs_main(int argc, char **argv)
20 {
21
22         const char *basedev = argv[1];
23         const char *type = argv[2];
24         int major = atoi(argv[3]);
25         int Sminor = atoi(argv[4]);
26         int S = atoi(argv[5]);
27         int E = atoi(argv[6]);
28         int sbase = argc == 8 ? 1 : 0;
29
30         mode_t mode = 0;
31         dev_t dev = 0;
32         char devname[255];
33         char buf[255];
34
35         if (argc < 7 || *argv[1]=='-')
36                 usage(makedevs_usage);
37
38         switch (type[0]) {
39         case 'c':
40                 mode = S_IFCHR;
41                 break;
42         case 'b':
43                 mode = S_IFBLK;
44                 break;
45         case 'f':
46                 mode = S_IFIFO;
47                 break;
48         default:
49                 usage(makedevs_usage);
50         }
51         mode |= 0660;
52
53         while (S <= E) {
54
55                 if (type[0] != 'f')
56                         dev = (major << 8) | Sminor;
57                 strcpy(devname, basedev);
58
59                 if (sbase == 0) {
60                         sprintf(buf, "%d", S);
61                         strcat(devname, buf);
62                 } else {
63                         sbase = 0;
64                 }
65
66                 if (mknod(devname, mode, dev))
67                         printf("Failed to create: %s\n", devname);
68
69                 S++;
70                 Sminor++;
71         }
72
73         return 0;
74 }
75
76 /*
77 And this is what this program replaces. The shell is too slow!
78
79 makedev () {
80 local basedev=$1; local S=$2; local E=$3
81 local major=$4; local Sminor=$5; local type=$6
82 local sbase=$7
83
84         if [ ! "$sbase" = "" ]; then
85                 mknod "$basedev" $type $major $Sminor
86                 S=`expr $S + 1`
87                 Sminor=`expr $Sminor + 1`
88         fi
89
90         while [ $S -le $E ]; do
91                 mknod "$basedev$S" $type $major $Sminor
92                 S=`expr $S + 1`
93                 Sminor=`expr $Sminor + 1`
94         done
95 }
96 */