makedevs.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* vi: set sw=4 ts=4: */
  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. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. #include "busybox.h"
  16. int makedevs_main(int argc, char **argv)
  17. {
  18. mode_t mode;
  19. char *basedev, *type, *nodname, buf[255];
  20. int major, Sminor, S, E;
  21. if (argc < 7 || *argv[1]=='-')
  22. show_usage();
  23. basedev = argv[1];
  24. type = argv[2];
  25. major = atoi(argv[3]) << 8; /* correcting param to mknod() */
  26. Sminor = atoi(argv[4]);
  27. S = atoi(argv[5]);
  28. E = atoi(argv[6]);
  29. nodname = argc == 8 ? basedev : buf;
  30. mode = 0660;
  31. switch (type[0]) {
  32. case 'c':
  33. mode |= S_IFCHR;
  34. break;
  35. case 'b':
  36. mode |= S_IFBLK;
  37. break;
  38. case 'f':
  39. mode |= S_IFIFO;
  40. break;
  41. default:
  42. show_usage();
  43. }
  44. while (S <= E) {
  45. int sz;
  46. sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
  47. if(sz<0 || sz>=sizeof(buf)) /* libc different */
  48. error_msg_and_die("%s too large", basedev);
  49. /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
  50. if (mknod(nodname, mode, major | Sminor))
  51. error_msg("Failed to create: %s", nodname);
  52. if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
  53. nodname = buf;
  54. S++;
  55. Sminor++;
  56. }
  57. return 0;
  58. }
  59. /*
  60. And this is what this program replaces. The shell is too slow!
  61. makedev () {
  62. local basedev=$1; local S=$2; local E=$3
  63. local major=$4; local Sminor=$5; local type=$6
  64. local sbase=$7
  65. if [ ! "$sbase" = "" ]; then
  66. mknod "$basedev" $type $major $Sminor
  67. S=`expr $S + 1`
  68. Sminor=`expr $Sminor + 1`
  69. fi
  70. while [ $S -le $E ]; do
  71. mknod "$basedev$S" $type $major $Sminor
  72. S=`expr $S + 1`
  73. Sminor=`expr $Sminor + 1`
  74. done
  75. }
  76. */