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