blob: 54a2e000a94443e5f10a419c0c7577aa8b804691 [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>
Eric Andersenc7bda1c2004-03-15 08:29:22 +00004 *
Eric Andersencc8ed391999-10-05 16:24:54 +00005 * makedevs
Eric Andersenc7bda1c2004-03-15 08:29:22 +00006 * Make ranges of device files quickly.
Eric Andersencc8ed391999-10-05 16:24:54 +00007 * known bugs: can't deal with alpha ranges
8 */
Glenn L McGrath07e1ccd2004-06-05 07:58:18 +00009
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>
Eric Andersen4f807a82004-07-26 09:11:12 +000016#include <sys/sysmacros.h> /* major() and minor() */
Glenn L McGrath07e1ccd2004-06-05 07:58:18 +000017#include "busybox.h"
18
Erik Andersene49d5ec2000-02-08 19:58:47 +000019int makedevs_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000020{
Eric Andersen36006022002-06-08 12:44:17 +000021 mode_t mode;
22 char *basedev, *type, *nodname, buf[255];
Eric Andersen4f807a82004-07-26 09:11:12 +000023 int Smajor, Sminor, S, E;
Eric Andersencc8ed391999-10-05 16:24:54 +000024
Erik Andersen5e1189e2000-04-15 16:34:54 +000025 if (argc < 7 || *argv[1]=='-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 bb_show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +000027
Eric Andersen19732c62002-04-13 14:26:44 +000028 basedev = argv[1];
29 type = argv[2];
Eric Andersencbcdbc42004-07-30 17:48:21 +000030 Smajor = atoi(argv[3]);
31 Sminor = atoi(argv[4]);
Eric Andersen19732c62002-04-13 14:26:44 +000032 S = atoi(argv[5]);
33 E = atoi(argv[6]);
Eric Andersen36006022002-06-08 12:44:17 +000034 nodname = argc == 8 ? basedev : buf;
35
36 mode = 0660;
Eric Andersen19732c62002-04-13 14:26:44 +000037
Eric Andersencc8ed391999-10-05 16:24:54 +000038 switch (type[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 case 'c':
Eric Andersen36006022002-06-08 12:44:17 +000040 mode |= S_IFCHR;
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 break;
42 case 'b':
Eric Andersen36006022002-06-08 12:44:17 +000043 mode |= S_IFBLK;
Erik Andersene49d5ec2000-02-08 19:58:47 +000044 break;
45 case 'f':
Eric Andersen36006022002-06-08 12:44:17 +000046 mode |= S_IFIFO;
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 break;
48 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 }
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 while (S <= E) {
Eric Andersen36006022002-06-08 12:44:17 +000053 int sz;
Erik Andersene49d5ec2000-02-08 19:58:47 +000054
Eric Andersen36006022002-06-08 12:44:17 +000055 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
56 if(sz<0 || sz>=sizeof(buf)) /* libc different */
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 bb_error_msg_and_die("%s too large", basedev);
Erik Andersene49d5ec2000-02-08 19:58:47 +000058
Eric Andersen36006022002-06-08 12:44:17 +000059 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
Erik Andersene49d5ec2000-02-08 19:58:47 +000060
Eric Andersene71e7602004-07-30 14:36:37 +000061 if (mknod(nodname, mode, makedev(Smajor, Sminor)))
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_error_msg("Failed to create: %s", nodname);
Erik Andersene49d5ec2000-02-08 19:58:47 +000063
Eric Andersen36006022002-06-08 12:44:17 +000064 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
65 nodname = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 S++;
67 Sminor++;
Eric Andersencc8ed391999-10-05 16:24:54 +000068 }
69
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000071}
72
73/*
Glenn L McGrath07e1ccd2004-06-05 07:58:18 +000074And this is what this program replaces. The shell is too slow!
Eric Andersencc8ed391999-10-05 16:24:54 +000075
Glenn L McGrath07e1ccd2004-06-05 07:58:18 +000076makedev () {
77local basedev=$1; local S=$2; local E=$3
78local major=$4; local Sminor=$5; local type=$6
79local sbase=$7
Eric Andersencc8ed391999-10-05 16:24:54 +000080
Glenn L McGrath07e1ccd2004-06-05 07:58:18 +000081 if [ ! "$sbase" = "" ]; then
82 mknod "$basedev" $type $major $Sminor
83 S=`expr $S + 1`
84 Sminor=`expr $Sminor + 1`
85 fi
Eric Andersencc8ed391999-10-05 16:24:54 +000086
Glenn L McGrath07e1ccd2004-06-05 07:58:18 +000087 while [ $S -le $E ]; do
88 mknod "$basedev$S" $type $major $Sminor
89 S=`expr $S + 1`
90 Sminor=`expr $Sminor + 1`
91 done
Eric Andersencc8ed391999-10-05 16:24:54 +000092}
Glenn L McGrath07e1ccd2004-06-05 07:58:18 +000093*/