Initial revision
[platform/upstream/busybox.git] / makedevs.c
1 /*
2  * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
3  * 
4  * makedevs
5  * Make ranges of device files quickly. 
6  * known bugs: can't deal with alpha ranges
7  */
8  
9 #include "internal.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17
18 const char makedevs_usage[] = 
19 "makedevs 0.01 -- Create an entire range of device files\n\n"
20 "\tmakedevs /dev/ttyS c 4 64 0 63        (ttyS0-ttyS63)\n"
21 "\tmakedevs /dev/hda b 3 0 0 8 s         (hda,hda1-hda8)\n";
22
23 int
24 makedevs_main(struct FileInfo * i, int argc, char * * argv)
25 {
26
27 const char *basedev = argv[1];
28 const char *type = argv[2];
29 int major = atoi(argv[3]);
30 int Sminor = atoi(argv[4]);
31 int S = atoi(argv[5]);
32 int E = atoi(argv[6]);
33 int sbase = argc == 8 ? 1 : 0;
34
35 mode_t mode = 0;
36 dev_t dev = 0;
37 char devname[255];
38 char buf[255];
39
40         switch (type[0]) {
41                 case 'c':
42                         mode = S_IFCHR; break;
43                 case 'b':
44                         mode = S_IFBLK; break;
45                 case 'f':
46                         mode = S_IFIFO; break;
47                 default:
48                         usage(makedevs_usage);
49                         return 2;
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++; Sminor++;
70         }
71
72 return 0;
73 }
74
75 /*
76 And this is what this program replaces. The shell is too slow!
77
78 makedev () {
79 local basedev=$1; local S=$2; local E=$3
80 local major=$4; local Sminor=$5; local type=$6
81 local sbase=$7
82
83         if [ ! "$sbase" = "" ]; then
84                 mknod "$basedev" $type $major $Sminor
85                 S=`expr $S + 1`
86                 Sminor=`expr $Sminor + 1`
87         fi
88
89         while [ $S -le $E ]; do
90                 mknod "$basedev$S" $type $major $Sminor
91                 S=`expr $S + 1`
92                 Sminor=`expr $Sminor + 1`
93         done
94 }
95 */