blob: 5072095b532cab61e67ed556ed428e17c3283996 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
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 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00009
Eric Andersencc8ed391999-10-05 16:24:54 +000010#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
Erik Andersene49d5ec2000-02-08 19:58:47 +000019static const char makedevs_usage[] =
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000020 "makedevs NAME TYPE MAJOR MINOR FIRST LAST [s]\n"
21#ifndef BB_FEATURE_TRIVIAL_HELP
22 "\nCreates a range of block or character special files\n\n"
Erik Andersen5e1189e2000-04-15 16:34:54 +000023 "TYPEs include:\n"
24 "\tb:\tMake a block (buffered) device.\n"
25 "\tc or u:\tMake a character (un-buffered) device.\n"
26 "\tp:\tMake a named pipe. MAJOR and MINOR are ignored for named pipes.\n\n"
27 "FIRST specifies the number appended to NAME to create the first device.\n"
28 "LAST specifies the number of the last item that should be created.\n"
29 "If 's' is the last argument, the base device is created as well.\n\n"
30 "For example:\n"
31 "\tmakedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000032 "\tmakedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8\n"
33#endif
34 ;
Erik Andersene49d5ec2000-02-08 19:58:47 +000035
36int makedevs_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000037{
38
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 const char *basedev = argv[1];
40 const char *type = argv[2];
41 int major = atoi(argv[3]);
42 int Sminor = atoi(argv[4]);
43 int S = atoi(argv[5]);
44 int E = atoi(argv[6]);
45 int sbase = argc == 8 ? 1 : 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000046
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 mode_t mode = 0;
48 dev_t dev = 0;
49 char devname[255];
50 char buf[255];
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Erik Andersen5e1189e2000-04-15 16:34:54 +000052 if (argc < 7 || *argv[1]=='-')
53 usage(makedevs_usage);
54
Eric Andersencc8ed391999-10-05 16:24:54 +000055 switch (type[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 case 'c':
57 mode = S_IFCHR;
58 break;
59 case 'b':
60 mode = S_IFBLK;
61 break;
62 case 'f':
63 mode = S_IFIFO;
64 break;
65 default:
66 usage(makedevs_usage);
67 }
68 mode |= 0660;
Eric Andersencc8ed391999-10-05 16:24:54 +000069
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 while (S <= E) {
71
Eric Andersencc8ed391999-10-05 16:24:54 +000072 if (type[0] != 'f')
73 dev = (major << 8) | Sminor;
74 strcpy(devname, basedev);
Erik Andersene49d5ec2000-02-08 19:58:47 +000075
Eric Andersencc8ed391999-10-05 16:24:54 +000076 if (sbase == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 sprintf(buf, "%d", S);
Eric Andersencc8ed391999-10-05 16:24:54 +000078 strcat(devname, buf);
79 } else {
80 sbase = 0;
81 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000082
83 if (mknod(devname, mode, dev))
84 printf("Failed to create: %s\n", devname);
85
86 S++;
87 Sminor++;
Eric Andersencc8ed391999-10-05 16:24:54 +000088 }
89
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000091}
92
93/*
94And this is what this program replaces. The shell is too slow!
95
96makedev () {
97local basedev=$1; local S=$2; local E=$3
98local major=$4; local Sminor=$5; local type=$6
99local sbase=$7
100
101 if [ ! "$sbase" = "" ]; then
102 mknod "$basedev" $type $major $Sminor
103 S=`expr $S + 1`
104 Sminor=`expr $Sminor + 1`
105 fi
106
107 while [ $S -le $E ]; do
108 mknod "$basedev$S" $type $major $Sminor
109 S=`expr $S + 1`
110 Sminor=`expr $Sminor + 1`
111 done
112}
113*/