A clone of btpd with my configuration changes.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
2.4 KiB

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <inttypes.h>
  6. #include <limits.h>
  7. #include <stdarg.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. void
  13. set_bit(uint8_t *bits, unsigned long index)
  14. {
  15. bits[index / 8] |= (1 << (7 - index % 8));
  16. }
  17. void
  18. clear_bit(uint8_t *bits, unsigned long index)
  19. {
  20. bits[index / 8] &= ~(1 << (7 - index % 8));
  21. }
  22. int
  23. has_bit(uint8_t *bits, unsigned long index)
  24. {
  25. return bits[index / 8] & (1 << (7 - index % 8));
  26. }
  27. int
  28. set_nonblocking(int fd)
  29. {
  30. int oflags;
  31. if ((oflags = fcntl(fd, F_GETFL, 0)) == -1)
  32. return errno;
  33. if (fcntl(fd, F_SETFL, oflags | O_NONBLOCK) == -1)
  34. return errno;
  35. return 0;
  36. }
  37. int
  38. set_blocking(int fd)
  39. {
  40. int oflags;
  41. if ((oflags = fcntl(fd, F_GETFL, 0)) == -1)
  42. return errno;
  43. if (fcntl(fd, F_SETFL, oflags & ~O_NONBLOCK) == -1)
  44. return errno;
  45. return 0;
  46. }
  47. int
  48. mkdirs(char *path)
  49. {
  50. int err = 0;
  51. char *spos = strchr(path + 1, '/'); // Must ignore the root
  52. while (spos != NULL) {
  53. *spos = '\0';
  54. err = mkdir(path, 0777);
  55. *spos = '/';
  56. if (err != 0 && errno != EEXIST) {
  57. err = errno;
  58. break;
  59. }
  60. spos = strchr(spos + 1, '/');
  61. }
  62. return err;
  63. }
  64. int
  65. vopen(int *res, int flags, const char *fmt, ...)
  66. {
  67. int fd, didmkdirs;
  68. char path[PATH_MAX + 1];
  69. va_list ap;
  70. va_start(ap, fmt);
  71. if (vsnprintf(path, PATH_MAX, fmt, ap) >= PATH_MAX) {
  72. va_end(ap);
  73. return ENAMETOOLONG;
  74. }
  75. va_end(ap);
  76. didmkdirs = 0;
  77. again:
  78. fd = open(path, flags, 0666);
  79. if (fd < 0 && errno == ENOENT && (flags & O_CREAT) != 0 && !didmkdirs) {
  80. if (mkdirs(path) == 0) {
  81. didmkdirs = 1;
  82. goto again;
  83. } else
  84. return errno;
  85. }
  86. if (fd >= 0) {
  87. *res = fd;
  88. return 0;
  89. } else
  90. return errno;
  91. }
  92. int
  93. canon_path(const char *path, char **res)
  94. {
  95. char rp[PATH_MAX];
  96. if (realpath(path, rp) == NULL)
  97. return errno;
  98. #if 0
  99. // This could be necessary on solaris.
  100. if (rp[0] != '/') {
  101. char wd[MAXPATHLEN];
  102. if (getcwd(wd, MAXPATHLEN) == NULL)
  103. return errno;
  104. if (strlcat(wd, "/", MAXPATHLEN) >= MAXPATHLEN)
  105. return ENAMETOOLONG;
  106. if (strlcat(wd, rp, MAXPATHLEN) >= MAXPATHLEN)
  107. return ENAMETOOLONG;
  108. strcpy(rp, wd);
  109. }
  110. #endif
  111. if ((*res = strdup(rp)) == NULL)
  112. return ENOMEM;
  113. return 0;
  114. }
  115. size_t
  116. round_to_page(size_t size)
  117. {
  118. size_t psize = getpagesize();
  119. size_t rem = size % psize;
  120. if (rem != 0)
  121. size += psize - rem;
  122. return size;
  123. }