blob: c8206e020e5d3f8208b375402fe50ea32280fe07 [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 Andersen5e1189e2000-04-15 16:34:54 +000020 "makedevs NAME TYPE MAJOR MINOR FIRST LAST [s]\n\n"
21 "Creates a range of block or character special files\n\n"
22 "TYPEs include:\n"
23 "\tb:\tMake a block (buffered) device.\n"
24 "\tc or u:\tMake a character (un-buffered) device.\n"
25 "\tp:\tMake a named pipe. MAJOR and MINOR are ignored for named pipes.\n\n"
26 "FIRST specifies the number appended to NAME to create the first device.\n"
27 "LAST specifies the number of the last item that should be created.\n"
28 "If 's' is the last argument, the base device is created as well.\n\n"
29 "For example:\n"
30 "\tmakedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63\n"
31 "\tmakedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8\n";
Erik Andersene49d5ec2000-02-08 19:58:47 +000032
33int makedevs_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000034{
35
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 const char *basedev = argv[1];
37 const char *type = argv[2];
38 int major = atoi(argv[3]);
39 int Sminor = atoi(argv[4]);
40 int S = atoi(argv[5]);
41 int E = atoi(argv[6]);
42 int sbase = argc == 8 ? 1 : 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000043
Erik Andersene49d5ec2000-02-08 19:58:47 +000044 mode_t mode = 0;
45 dev_t dev = 0;
46 char devname[255];
47 char buf[255];
Eric Andersencc8ed391999-10-05 16:24:54 +000048
Erik Andersen5e1189e2000-04-15 16:34:54 +000049 if (argc < 7 || *argv[1]=='-')
50 usage(makedevs_usage);
51
Eric Andersencc8ed391999-10-05 16:24:54 +000052 switch (type[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 case 'c':
54 mode = S_IFCHR;
55 break;
56 case 'b':
57 mode = S_IFBLK;
58 break;
59 case 'f':
60 mode = S_IFIFO;
61 break;
62 default:
63 usage(makedevs_usage);
64 }
65 mode |= 0660;
Eric Andersencc8ed391999-10-05 16:24:54 +000066
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 while (S <= E) {
68
Eric Andersencc8ed391999-10-05 16:24:54 +000069 if (type[0] != 'f')
70 dev = (major << 8) | Sminor;
71 strcpy(devname, basedev);
Erik Andersene49d5ec2000-02-08 19:58:47 +000072
Eric Andersencc8ed391999-10-05 16:24:54 +000073 if (sbase == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 sprintf(buf, "%d", S);
Eric Andersencc8ed391999-10-05 16:24:54 +000075 strcat(devname, buf);
76 } else {
77 sbase = 0;
78 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000079
80 if (mknod(devname, mode, dev))
81 printf("Failed to create: %s\n", devname);
82
83 S++;
84 Sminor++;
Eric Andersencc8ed391999-10-05 16:24:54 +000085 }
86
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000088}
89
90/*
91And this is what this program replaces. The shell is too slow!
92
93makedev () {
94local basedev=$1; local S=$2; local E=$3
95local major=$4; local Sminor=$5; local type=$6
96local sbase=$7
97
98 if [ ! "$sbase" = "" ]; then
99 mknod "$basedev" $type $major $Sminor
100 S=`expr $S + 1`
101 Sminor=`expr $Sminor + 1`
102 fi
103
104 while [ $S -le $E ]; do
105 mknod "$basedev$S" $type $major $Sminor
106 S=`expr $S + 1`
107 Sminor=`expr $Sminor + 1`
108 done
109}
110*/