blob: 45498bb1d0ece40bee018c87b1d990cf9ea1ffc0 [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 */
Erik Andersene49d5ec2000-02-08 19:58:47 +00009
Eric Andersencc8ed391999-10-05 16:24:54 +000010#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 Andersencbe31da2001-02-20 06:14:08 +000016#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000017
Erik Andersene49d5ec2000-02-08 19:58:47 +000018int makedevs_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000019{
Eric Andersen36006022002-06-08 12:44:17 +000020 mode_t mode;
21 char *basedev, *type, *nodname, buf[255];
22 int major, Sminor, S, E;
Eric Andersencc8ed391999-10-05 16:24:54 +000023
Erik Andersen5e1189e2000-04-15 16:34:54 +000024 if (argc < 7 || *argv[1]=='-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000025 bb_show_usage();
Erik Andersen5e1189e2000-04-15 16:34:54 +000026
Eric Andersen19732c62002-04-13 14:26:44 +000027 basedev = argv[1];
28 type = argv[2];
Eric Andersen36006022002-06-08 12:44:17 +000029 major = atoi(argv[3]) << 8; /* correcting param to mknod() */
Eric Andersen19732c62002-04-13 14:26:44 +000030 Sminor = atoi(argv[4]);
31 S = atoi(argv[5]);
32 E = atoi(argv[6]);
Eric Andersen36006022002-06-08 12:44:17 +000033 nodname = argc == 8 ? basedev : buf;
34
35 mode = 0660;
Eric Andersen19732c62002-04-13 14:26:44 +000036
Eric Andersencc8ed391999-10-05 16:24:54 +000037 switch (type[0]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000038 case 'c':
Eric Andersen36006022002-06-08 12:44:17 +000039 mode |= S_IFCHR;
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 break;
41 case 'b':
Eric Andersen36006022002-06-08 12:44:17 +000042 mode |= S_IFBLK;
Erik Andersene49d5ec2000-02-08 19:58:47 +000043 break;
44 case 'f':
Eric Andersen36006022002-06-08 12:44:17 +000045 mode |= S_IFIFO;
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 break;
47 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000048 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 }
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 while (S <= E) {
Eric Andersen36006022002-06-08 12:44:17 +000052 int sz;
Erik Andersene49d5ec2000-02-08 19:58:47 +000053
Eric Andersen36006022002-06-08 12:44:17 +000054 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
55 if(sz<0 || sz>=sizeof(buf)) /* libc different */
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 bb_error_msg_and_die("%s too large", basedev);
Erik Andersene49d5ec2000-02-08 19:58:47 +000057
Eric Andersen36006022002-06-08 12:44:17 +000058 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
Erik Andersene49d5ec2000-02-08 19:58:47 +000059
Eric Andersen36006022002-06-08 12:44:17 +000060 if (mknod(nodname, mode, major | Sminor))
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 bb_error_msg("Failed to create: %s", nodname);
Erik Andersene49d5ec2000-02-08 19:58:47 +000062
Eric Andersen36006022002-06-08 12:44:17 +000063 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
64 nodname = buf;
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 S++;
66 Sminor++;
Eric Andersencc8ed391999-10-05 16:24:54 +000067 }
68
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000070}
71
72/*
73And this is what this program replaces. The shell is too slow!
74
75makedev () {
76local basedev=$1; local S=$2; local E=$3
77local major=$4; local Sminor=$5; local type=$6
78local sbase=$7
79
80 if [ ! "$sbase" = "" ]; then
81 mknod "$basedev" $type $major $Sminor
82 S=`expr $S + 1`
83 Sminor=`expr $Sminor + 1`
84 fi
85
86 while [ $S -le $E ]; do
87 mknod "$basedev$S" $type $major $Sminor
88 S=`expr $S + 1`
89 Sminor=`expr $Sminor + 1`
90 done
91}
92*/