Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 5 | * makedevs |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 6 | * Make ranges of device files quickly. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | * known bugs: can't deal with alpha ranges |
| 8 | */ |
Glenn L McGrath | 07e1ccd | 2004-06-05 07:58:18 +0000 | [diff] [blame] | 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> |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 16 | #include <sys/sysmacros.h> /* major() and minor() */ |
Glenn L McGrath | 07e1ccd | 2004-06-05 07:58:18 +0000 | [diff] [blame] | 17 | #include "busybox.h" |
| 18 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 19 | int makedevs_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 20 | { |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 21 | mode_t mode; |
| 22 | char *basedev, *type, *nodname, buf[255]; |
Eric Andersen | 4f807a8 | 2004-07-26 09:11:12 +0000 | [diff] [blame] | 23 | int Smajor, Sminor, S, E; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 25 | if (argc < 7 || *argv[1]=='-') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | bb_show_usage(); |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 27 | |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 28 | basedev = argv[1]; |
| 29 | type = argv[2]; |
Eric Andersen | cbcdbc4 | 2004-07-30 17:48:21 +0000 | [diff] [blame] | 30 | Smajor = atoi(argv[3]); |
| 31 | Sminor = atoi(argv[4]); |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 32 | S = atoi(argv[5]); |
| 33 | E = atoi(argv[6]); |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 34 | nodname = argc == 8 ? basedev : buf; |
| 35 | |
| 36 | mode = 0660; |
Eric Andersen | 19732c6 | 2002-04-13 14:26:44 +0000 | [diff] [blame] | 37 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 38 | switch (type[0]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 39 | case 'c': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 40 | mode |= S_IFCHR; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | break; |
| 42 | case 'b': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 43 | mode |= S_IFBLK; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | break; |
| 45 | case 'f': |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 46 | mode |= S_IFIFO; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 47 | break; |
| 48 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 51 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | while (S <= E) { |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 53 | int sz; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 55 | sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S); |
| 56 | if(sz<0 || sz>=sizeof(buf)) /* libc different */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | bb_error_msg_and_die("%s too large", basedev); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 59 | /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | |
Eric Andersen | e71e760 | 2004-07-30 14:36:37 +0000 | [diff] [blame] | 61 | if (mknod(nodname, mode, makedev(Smajor, Sminor))) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | bb_error_msg("Failed to create: %s", nodname); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | |
Eric Andersen | 3600602 | 2002-06-08 12:44:17 +0000 | [diff] [blame] | 64 | if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */ |
| 65 | nodname = buf; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | S++; |
| 67 | Sminor++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /* |
Glenn L McGrath | 07e1ccd | 2004-06-05 07:58:18 +0000 | [diff] [blame] | 74 | And this is what this program replaces. The shell is too slow! |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 75 | |
Glenn L McGrath | 07e1ccd | 2004-06-05 07:58:18 +0000 | [diff] [blame] | 76 | makedev () { |
| 77 | local basedev=$1; local S=$2; local E=$3 |
| 78 | local major=$4; local Sminor=$5; local type=$6 |
| 79 | local sbase=$7 |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 80 | |
Glenn L McGrath | 07e1ccd | 2004-06-05 07:58:18 +0000 | [diff] [blame] | 81 | if [ ! "$sbase" = "" ]; then |
| 82 | mknod "$basedev" $type $major $Sminor |
| 83 | S=`expr $S + 1` |
| 84 | Sminor=`expr $Sminor + 1` |
| 85 | fi |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 86 | |
Glenn L McGrath | 07e1ccd | 2004-06-05 07:58:18 +0000 | [diff] [blame] | 87 | 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 Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | } |
Glenn L McGrath | 07e1ccd | 2004-06-05 07:58:18 +0000 | [diff] [blame] | 93 | */ |