mknod.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* mknod.c - make block or character special file
  2. *
  3. * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
  4. *
  5. * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mknod.html
  6. USE_MKNOD(NEWTOY(mknod, "<2>4m(mode):"USE_MKNOD_Z("Z:"), TOYFLAG_BIN|TOYFLAG_UMASK))
  7. config MKNOD
  8. bool "mknod"
  9. default y
  10. help
  11. usage: mknod [-m MODE] NAME TYPE [MAJOR MINOR]
  12. Create a special file NAME with a given type. TYPE is b for block device,
  13. c or u for character device, p for named pipe (which ignores MAJOR/MINOR).
  14. -m Mode (file permissions) of new device, in octal or u+x format
  15. config MKNOD_Z
  16. bool
  17. default y
  18. depends on MKNOD && !TOYBOX_LSM_NONE
  19. help
  20. usage: mknod [-Z CONTEXT] ...
  21. -Z Set security context to created file
  22. */
  23. #define FOR_mknod
  24. #include "toys.h"
  25. GLOBALS(
  26. char *Z, *m;
  27. )
  28. void mknod_main(void)
  29. {
  30. mode_t modes[] = {S_IFIFO, S_IFCHR, S_IFCHR, S_IFBLK};
  31. int major=0, minor=0, type;
  32. int mode = TT.m ? string_to_mode(TT.m, 0777) : 0660;
  33. type = stridx("pcub", *toys.optargs[1]);
  34. if (type == -1) perror_exit("bad type '%c'", *toys.optargs[1]);
  35. if (type) {
  36. if (toys.optc != 4) perror_exit("need major/minor");
  37. major = atoi(toys.optargs[2]);
  38. minor = atoi(toys.optargs[3]);
  39. }
  40. if (toys.optflags & FLAG_Z)
  41. if (-1 == lsm_set_create(TT.Z))
  42. perror_exit("-Z '%s' failed", TT.Z);
  43. if (mknod(*toys.optargs, mode|modes[type], dev_makedev(major, minor)))
  44. perror_exit_raw(*toys.optargs);
  45. }