mknod.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <sys/stat.h>
  29. #include <unistd.h>
  30. inline constexpr unsigned encoded_device(unsigned major, unsigned minor)
  31. {
  32. return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
  33. }
  34. static int usage()
  35. {
  36. printf("usage: mknod <name> <c|b|p> [<major> <minor>]\n");
  37. return 0;
  38. }
  39. int main(int argc, char** argv)
  40. {
  41. if (pledge("stdio dpath", nullptr) < 0) {
  42. perror("pledge");
  43. return 1;
  44. }
  45. // FIXME: Add some kind of option for specifying the file permissions.
  46. if (argc < 3)
  47. return usage();
  48. if (argv[2][0] == 'p') {
  49. if (argc != 3)
  50. return usage();
  51. } else if (argc != 5) {
  52. return usage();
  53. }
  54. const char* name = argv[1];
  55. mode_t mode = 0666;
  56. switch (argv[2][0]) {
  57. case 'c':
  58. case 'u':
  59. mode |= S_IFCHR;
  60. break;
  61. case 'b':
  62. mode |= S_IFBLK;
  63. break;
  64. case 'p':
  65. mode |= S_IFIFO;
  66. break;
  67. default:
  68. return usage();
  69. }
  70. int major = 0;
  71. int minor = 0;
  72. if (argc == 5) {
  73. major = atoi(argv[3]);
  74. minor = atoi(argv[4]);
  75. }
  76. int rc = mknod(name, mode, encoded_device(major, minor));
  77. if (rc < 0) {
  78. perror("mknod");
  79. return 1;
  80. }
  81. return 0;
  82. }