/compat.c

https://gitlab.com/ekollof/Sysalerter-backend · C · 259 lines · 191 code · 36 blank · 32 comment · 48 complexity · 4c888c8ca6110f270fa20ad453d582da MD5 · raw file

  1. /*
  2. * Some code Copyright (C) 2001 Federico Di Gregorio <fog@debian.org>
  3. * Portions of code are from the OpenBSD project
  4. *
  5. * This source is free software; you can redistribute it and/or modify it under
  6. * the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2, or (at your option) any later
  8. * version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, 59 Temple
  17. * Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include "sysalert.h"
  20. #ifdef NO_ASPRINTF
  21. int
  22. asprintf(char **ret, const char *fmt,...)
  23. {
  24. va_list ap;
  25. int retval;
  26. va_start(ap, fmt);
  27. retval = vasprintf(ret, fmt, ap);
  28. va_end(ap);
  29. return retval;
  30. }
  31. int
  32. vasprintf(char **ret, const char *fmt, va_list ap)
  33. {
  34. char *buf, *new_buf;
  35. size_t len;
  36. int retval;
  37. len = 128;
  38. buf = malloc(len);
  39. if (buf == NULL) {
  40. *ret = NULL;
  41. return -1;
  42. }
  43. retval = vsnprintf(buf, len, fmt, ap);
  44. if (retval < 0) {
  45. free(buf);
  46. *ret = NULL;
  47. return -1;
  48. }
  49. if (retval < len) {
  50. new_buf = realloc(buf, retval + 1);
  51. if (new_buf == NULL)
  52. *ret = buf;
  53. else
  54. *ret = new_buf;
  55. return retval;
  56. }
  57. len = (size_t) retval + 1;
  58. free(buf);
  59. buf = malloc(len);
  60. if (buf == NULL) {
  61. *ret = NULL;
  62. return -1;
  63. }
  64. retval = vsnprintf(buf, len, fmt, ap);
  65. if (retval != len - 1) {
  66. free(buf);
  67. *ret = NULL;
  68. return -1;
  69. }
  70. *ret = buf;
  71. return retval;
  72. }
  73. #endif
  74. #ifdef NO_DAEMON
  75. int
  76. daemon(int nochdir, int noclose)
  77. {
  78. int fd;
  79. switch (fork()) {
  80. case -1:
  81. return -1;
  82. case 0:
  83. break;
  84. default:
  85. exit(0);
  86. }
  87. if (setsid() == -1)
  88. return -1;
  89. if (!nochdir)
  90. chdir("/");
  91. if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
  92. dup2(fd, 0);
  93. dup2(fd, 1);
  94. dup2(fd, 2);
  95. if (fd > 2)
  96. close(fd);
  97. }
  98. return 0;
  99. }
  100. #endif
  101. #ifdef NO_NATIVE_STRLCPY
  102. /**
  103. * Size-bounded string copy
  104. * @param dst Destination string
  105. * @param src Source string
  106. * @param sz Size
  107. * @return Amount of data copied.
  108. */
  109. size_t
  110. strlcpy(char *dst, const char *src, size_t sz)
  111. {
  112. char *d = dst;
  113. const char *s = src;
  114. size_t n = sz;
  115. if (n) {
  116. --n;
  117. while (n && *s)
  118. *d++ = *s++, --n;
  119. *d = 0;
  120. }
  121. while (*s++);
  122. return s - src - 1;
  123. }
  124. /**
  125. * Size-bounded string concatenation
  126. * @param dst Destination string
  127. * @param src Source string
  128. * @param sz Size
  129. * @returns Amount of data concatenated
  130. */
  131. size_t
  132. strlcat(char *dst, const char *src, size_t sz)
  133. {
  134. char *d = dst;
  135. const char *s = src;
  136. size_t n = sz;
  137. if (n) {
  138. --n;
  139. while (n && *d)
  140. ++d, --n;
  141. if (n) {
  142. while (n && *s)
  143. *d++ = *s++, --n;
  144. *d = 0;
  145. }
  146. n = d - dst + (*d != 0);
  147. }
  148. src = s;
  149. while (*s++);
  150. return n + (s - src - 1);
  151. }
  152. #endif
  153. #ifdef NO_STRDUP
  154. char *
  155. strdup(char *buf)
  156. {
  157. size_t len;
  158. char *ret;
  159. if (!buf)
  160. return (NULL);
  161. len = strlen(buf) + 1;
  162. ret = (char *) malloc(len);
  163. if (!ret)
  164. return (NULL);
  165. memcpy(ret, buf, len);
  166. return (ret);
  167. }
  168. #endif
  169. #ifdef NO_SETPROCTITLE
  170. #endif
  171. #ifdef NO_UTENT
  172. struct utmp *
  173. getutent(void)
  174. {
  175. if (ufp == NULL) {
  176. if ((ufp = fopen(utmpfil, "r")) == NULL) {
  177. dbprintf("Couldn't open utmp (%s): %s\n", utmpfil, strerror(errno));
  178. return((struct utmp *)NULL);
  179. }
  180. }
  181. do {
  182. if (fread(&ut, sizeof(ut), 1, ufp) != 1) {
  183. return((struct utmp *)NULL);
  184. }
  185. } while (ut.ut_name[0] == 0); /* valid entry? */
  186. return(&ut);
  187. }
  188. struct utmp *
  189. getutline(struct utmp *line)
  190. {
  191. do {
  192. if (strcmp(ut.ut_line, line->ut_line) == 0) {
  193. return(&ut);
  194. }
  195. } while (getutent() != (struct utmp *)NULL);
  196. return((struct utmp *)NULL);
  197. }
  198. void
  199. setutent(void)
  200. {
  201. if (ufp != NULL) rewind(ufp);
  202. }
  203. void
  204. endutent(void)
  205. {
  206. if (ufp != NULL) fclose(ufp);
  207. }
  208. void
  209. utmpname(char *file)
  210. {
  211. utmpfil = file;
  212. }
  213. #endif
  214. #ifdef NO_SETPROCTITLE
  215. void
  216. init_setproctitle(int argc, char **argv)
  217. {
  218. }
  219. void
  220. setproctitle(char *fmt, ...)
  221. {
  222. }
  223. #endif