Use gcc -E instead of cpp to enhance portability for brain
[oweals/busybox.git] / 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 static const char makedevs_usage[] =
20         "makedevs 0.01 -- Create an entire range of device files\n\n"
21         "\tmakedevs /dev/ttyS c 4 64 0 63        (ttyS0-ttyS63)\n"
22
23         "\tmakedevs /dev/hda b 3 0 0 8 s         (hda,hda1-hda8)\n";
24
25 int makedevs_main(int argc, char **argv)
26 {
27
28         const char *basedev = argv[1];
29         const char *type = argv[2];
30         int major = atoi(argv[3]);
31         int Sminor = atoi(argv[4]);
32         int S = atoi(argv[5]);
33         int E = atoi(argv[6]);
34         int sbase = argc == 8 ? 1 : 0;
35
36         mode_t mode = 0;
37         dev_t dev = 0;
38         char devname[255];
39         char buf[255];
40
41         switch (type[0]) {
42         case 'c':
43                 mode = S_IFCHR;
44                 break;
45         case 'b':
46                 mode = S_IFBLK;
47                 break;
48         case 'f':
49                 mode = S_IFIFO;
50                 break;
51         default:
52                 usage(makedevs_usage);
53         }
54         mode |= 0660;
55
56         while (S <= E) {
57
58                 if (type[0] != 'f')
59                         dev = (major << 8) | Sminor;
60                 strcpy(devname, basedev);
61
62                 if (sbase == 0) {
63                         sprintf(buf, "%d", S);
64                         strcat(devname, buf);
65                 } else {
66                         sbase = 0;
67                 }
68
69                 if (mknod(devname, mode, dev))
70                         printf("Failed to create: %s\n", devname);
71
72                 S++;
73                 Sminor++;
74         }
75
76         return 0;
77 }
78
79 /*
80 And this is what this program replaces. The shell is too slow!
81
82 makedev () {
83 local basedev=$1; local S=$2; local E=$3
84 local major=$4; local Sminor=$5; local type=$6
85 local sbase=$7
86
87         if [ ! "$sbase" = "" ]; then
88                 mknod "$basedev" $type $major $Sminor
89                 S=`expr $S + 1`
90                 Sminor=`expr $Sminor + 1`
91         fi
92
93         while [ $S -le $E ]; do
94                 mknod "$basedev$S" $type $major $Sminor
95                 S=`expr $S + 1`
96                 Sminor=`expr $Sminor + 1`
97         done
98 }
99 */