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> |
| 17 | #include <sys/stat.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 | { |
| 21 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 22 | const char *basedev = argv[1]; |
| 23 | const char *type = argv[2]; |
| 24 | int major = atoi(argv[3]); |
| 25 | int Sminor = atoi(argv[4]); |
| 26 | int S = atoi(argv[5]); |
| 27 | int E = atoi(argv[6]); |
| 28 | int sbase = argc == 8 ? 1 : 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 30 | mode_t mode = 0; |
| 31 | dev_t dev = 0; |
| 32 | char devname[255]; |
| 33 | char buf[255]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | |
Erik Andersen | 5e1189e | 2000-04-15 16:34:54 +0000 | [diff] [blame] | 35 | if (argc < 7 || *argv[1]=='-') |
| 36 | usage(makedevs_usage); |
| 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': |
| 40 | mode = S_IFCHR; |
| 41 | break; |
| 42 | case 'b': |
| 43 | mode = S_IFBLK; |
| 44 | break; |
| 45 | case 'f': |
| 46 | mode = S_IFIFO; |
| 47 | break; |
| 48 | default: |
| 49 | usage(makedevs_usage); |
| 50 | } |
| 51 | mode |= 0660; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 52 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 53 | while (S <= E) { |
| 54 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | if (type[0] != 'f') |
| 56 | dev = (major << 8) | Sminor; |
| 57 | strcpy(devname, basedev); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 59 | if (sbase == 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | sprintf(buf, "%d", S); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 61 | strcat(devname, buf); |
| 62 | } else { |
| 63 | sbase = 0; |
| 64 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | |
| 66 | if (mknod(devname, mode, dev)) |
| 67 | printf("Failed to create: %s\n", devname); |
| 68 | |
| 69 | S++; |
| 70 | Sminor++; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | And this is what this program replaces. The shell is too slow! |
| 78 | |
| 79 | makedev () { |
| 80 | local basedev=$1; local S=$2; local E=$3 |
| 81 | local major=$4; local Sminor=$5; local type=$6 |
| 82 | local sbase=$7 |
| 83 | |
| 84 | if [ ! "$sbase" = "" ]; then |
| 85 | mknod "$basedev" $type $major $Sminor |
| 86 | S=`expr $S + 1` |
| 87 | Sminor=`expr $Sminor + 1` |
| 88 | fi |
| 89 | |
| 90 | while [ $S -le $E ]; do |
| 91 | mknod "$basedev$S" $type $major $Sminor |
| 92 | S=`expr $S + 1` |
| 93 | Sminor=`expr $Sminor + 1` |
| 94 | done |
| 95 | } |
| 96 | */ |