e4233330a2b6b80da2a33a8ba9f3378aed743e49
[oweals/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 <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/sysmacros.h>     /* major() and minor() */
17 #include "busybox.h"
18
19 int makedevs_main(int argc, char **argv)
20 {
21         mode_t mode;
22         char *basedev, *type, *nodname, buf[255];
23         int Smajor, Sminor, S, E;
24
25         if (argc < 7 || *argv[1]=='-')
26                 bb_show_usage();
27
28         basedev = argv[1];
29         type = argv[2];
30         Smajor = major(atoi(argv[3]));
31         Sminor = minor(atoi(argv[4]));
32         S = atoi(argv[5]);
33         E = atoi(argv[6]);
34         nodname = argc == 8 ? basedev : buf;
35
36         mode = 0660;
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                 bb_show_usage();
50         }
51
52         while (S <= E) {
53                 int sz;
54
55                 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
56                 if(sz<0 || sz>=sizeof(buf))  /* libc different */
57                         bb_error_msg_and_die("%s too large", basedev);
58
59         /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
60
61                 if (mknod(nodname, mode, Smajor | Sminor))
62                         bb_error_msg("Failed to create: %s", nodname);
63
64                 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
65                         nodname = buf;
66                 S++;
67                 Sminor++;
68         }
69
70         return 0;
71 }
72
73 /*
74 And this is what this program replaces. The shell is too slow!
75
76 makedev () {
77 local basedev=$1; local S=$2; local E=$3
78 local major=$4; local Sminor=$5; local type=$6
79 local sbase=$7
80
81         if [ ! "$sbase" = "" ]; then
82                 mknod "$basedev" $type $major $Sminor
83                 S=`expr $S + 1`
84                 Sminor=`expr $Sminor + 1`
85         fi
86
87         while [ $S -le $E ]; do
88                 mknod "$basedev$S" $type $major $Sminor
89                 S=`expr $S + 1`
90                 Sminor=`expr $Sminor + 1`
91         done
92 }
93 */