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