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
18int makedevs_main(int argc, char **argv)
19{
20
21	const char *basedev = argv[1];
22	const char *type = argv[2];
23	int major = atoi(argv[3]);
24	int Sminor = atoi(argv[4]);
25	int S = atoi(argv[5]);
26	int E = atoi(argv[6]);
27	int sbase = argc == 8 ? 1 : 0;
28
29	mode_t mode = 0;
30	dev_t dev = 0;
31	char devname[255];
32	char buf[255];
33
34	if (argc < 7 || *argv[1]=='-')
35		show_usage();
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		show_usage();
49	}
50	mode |= 0660;
51
52	while (S <= E) {
53
54		if (type[0] != 'f')
55			dev = (major << 8) | Sminor;
56		strcpy(devname, basedev);
57
58		if (sbase == 0) {
59			sprintf(buf, "%d", S);
60			strcat(devname, buf);
61		} else {
62			sbase = 0;
63		}
64
65		if (mknod(devname, mode, dev))
66			printf("Failed to create: %s\n", devname);
67
68		S++;
69		Sminor++;
70	}
71
72	return 0;
73}
74
75/*
76And this is what this program replaces. The shell is too slow!
77
78makedev () {
79local basedev=$1; local S=$2; local E=$3
80local major=$4; local Sminor=$5; local type=$6
81local sbase=$7
82
83	if [ ! "$sbase" = "" ]; then
84		mknod "$basedev" $type $major $Sminor
85		S=`expr $S + 1`
86		Sminor=`expr $Sminor + 1`
87	fi
88
89	while [ $S -le $E ]; do
90		mknod "$basedev$S" $type $major $Sminor
91		S=`expr $S + 1`
92		Sminor=`expr $Sminor + 1`
93	done
94}
95*/
96