blob: 5948bacc84ad177ba06650ada752c5cbb63804a1 [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[] =
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"
Eric Andersencc8ed391999-10-05 16:24:54 +000022
Erik Andersene49d5ec2000-02-08 19:58:47 +000023 "\tmakedevs /dev/hda b 3 0 0 8 s (hda,hda1-hda8)\n";
24
25int makedevs_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000026{
27
Erik Andersene49d5ec2000-02-08 19:58:47 +000028 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;
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 mode_t mode = 0;
37 dev_t dev = 0;
38 char devname[255];
39 char buf[255];
Eric Andersencc8ed391999-10-05 16:24:54 +000040
41 switch (type[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 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;
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 while (S <= E) {
57
Eric Andersencc8ed391999-10-05 16:24:54 +000058 if (type[0] != 'f')
59 dev = (major << 8) | Sminor;
60 strcpy(devname, basedev);
Erik Andersene49d5ec2000-02-08 19:58:47 +000061
Eric Andersencc8ed391999-10-05 16:24:54 +000062 if (sbase == 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 sprintf(buf, "%d", S);
Eric Andersencc8ed391999-10-05 16:24:54 +000064 strcat(devname, buf);
65 } else {
66 sbase = 0;
67 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000068
69 if (mknod(devname, mode, dev))
70 printf("Failed to create: %s\n", devname);
71
72 S++;
73 Sminor++;
Eric Andersencc8ed391999-10-05 16:24:54 +000074 }
75
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000077}
78
79/*
80And this is what this program replaces. The shell is too slow!
81
82makedev () {
83local basedev=$1; local S=$2; local E=$3
84local major=$4; local Sminor=$5; local type=$6
85local 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*/