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> |
| 4 | * |
| 5 | * makedevs |
| 6 | * Make ranges of device files quickly. |
| 7 | * known bugs: can't deal with alpha ranges |
| 8 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 9 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | #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> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 17 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 18 | int makedevs_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 19 | { |
| 20 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 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; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 29 | mode_t mode = 0; |
| 30 | dev_t dev = 0; |
| 31 | char devname[255]; |
| 32 | char buf[255]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 34 | if (argc < 7 || *argv[1]=='-') |
| 35 | usage(makedevs_usage); |
| 36 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 37 | switch (type[0]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 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 | usage(makedevs_usage); |
| 49 | } |
| 50 | mode |= 0660; |
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) { |
| 53 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 54 | if (type[0] != 'f') |
| 55 | dev = (major << 8) | Sminor; |
| 56 | strcpy(devname, basedev); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 58 | if (sbase == 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | sprintf(buf, "%d", S); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 60 | strcat(devname, buf); |
| 61 | } else { |
| 62 | sbase = 0; |
| 63 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | |
| 65 | if (mknod(devname, mode, dev)) |
| 66 | printf("Failed to create: %s\n", devname); |
| 67 | |
| 68 | S++; |
| 69 | Sminor++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | And this is what this program replaces. The shell is too slow! |
| 77 | |
| 78 | makedev () { |
| 79 | local basedev=$1; local S=$2; local E=$3 |
| 80 | local major=$4; local Sminor=$5; local type=$6 |
| 81 | local 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 | */ |