xfuncs.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include "libbb.h"
  17. #ifndef DMALLOC
  18. #ifdef L_xmalloc
  19. void *xmalloc(size_t size)
  20. {
  21. void *ptr = malloc(size);
  22. if (ptr == NULL && size != 0)
  23. bb_error_msg_and_die(bb_msg_memory_exhausted);
  24. return ptr;
  25. }
  26. #endif
  27. #ifdef L_xrealloc
  28. void *xrealloc(void *ptr, size_t size)
  29. {
  30. ptr = realloc(ptr, size);
  31. if (ptr == NULL && size != 0)
  32. bb_error_msg_and_die(bb_msg_memory_exhausted);
  33. return ptr;
  34. }
  35. #endif
  36. #ifdef L_xcalloc
  37. void *xcalloc(size_t nmemb, size_t size)
  38. {
  39. void *ptr = calloc(nmemb, size);
  40. if (ptr == NULL && nmemb != 0 && size != 0)
  41. bb_error_msg_and_die(bb_msg_memory_exhausted);
  42. return ptr;
  43. }
  44. #endif
  45. #endif /* DMALLOC */
  46. #ifdef L_xstrdup
  47. char * bb_xstrdup (const char *s)
  48. {
  49. char *t;
  50. if (s == NULL)
  51. return NULL;
  52. t = strdup (s);
  53. if (t == NULL)
  54. bb_error_msg_and_die(bb_msg_memory_exhausted);
  55. return t;
  56. }
  57. #endif
  58. #ifdef L_xstrndup
  59. char * bb_xstrndup (const char *s, int n)
  60. {
  61. char *t;
  62. if (s == NULL)
  63. bb_error_msg_and_die("bb_xstrndup bug");
  64. t = xmalloc(++n);
  65. return safe_strncpy(t,s,n);
  66. }
  67. #endif
  68. #ifdef L_xfopen
  69. FILE *bb_xfopen(const char *path, const char *mode)
  70. {
  71. FILE *fp;
  72. if ((fp = fopen(path, mode)) == NULL)
  73. bb_perror_msg_and_die("%s", path);
  74. return fp;
  75. }
  76. #endif
  77. #ifdef L_xopen
  78. int bb_xopen(const char *pathname, int flags)
  79. {
  80. int ret;
  81. ret = open(pathname, flags, 0777);
  82. if (ret == -1) {
  83. bb_perror_msg_and_die("%s", pathname);
  84. }
  85. return ret;
  86. }
  87. #endif
  88. #ifdef L_xread
  89. ssize_t bb_xread(int fd, void *buf, size_t count)
  90. {
  91. ssize_t size;
  92. size = read(fd, buf, count);
  93. if (size == -1) {
  94. bb_perror_msg_and_die(bb_msg_read_error);
  95. }
  96. return(size);
  97. }
  98. #endif
  99. #ifdef L_xread_all
  100. void bb_xread_all(int fd, void *buf, size_t count)
  101. {
  102. ssize_t size;
  103. while (count) {
  104. if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */
  105. bb_error_msg_and_die("Short read");
  106. }
  107. count -= size;
  108. buf = ((char *) buf) + size;
  109. }
  110. return;
  111. }
  112. #endif
  113. #ifdef L_xread_char
  114. unsigned char bb_xread_char(int fd)
  115. {
  116. char tmp;
  117. bb_xread_all(fd, &tmp, 1);
  118. return(tmp);
  119. }
  120. #endif
  121. #ifdef L_xferror
  122. void bb_xferror(FILE *fp, const char *fn)
  123. {
  124. if (ferror(fp)) {
  125. bb_error_msg_and_die("%s", fn);
  126. }
  127. }
  128. #endif
  129. #ifdef L_xferror_stdout
  130. void bb_xferror_stdout(void)
  131. {
  132. bb_xferror(stdout, bb_msg_standard_output);
  133. }
  134. #endif
  135. #ifdef L_xfflush_stdout
  136. void bb_xfflush_stdout(void)
  137. {
  138. if (fflush(stdout)) {
  139. bb_perror_msg_and_die(bb_msg_standard_output);
  140. }
  141. }
  142. #endif
  143. /* GCC forces inlining of strlen everywhere, which is generally a byte
  144. larger than calling a function, and it's called a lot so it adds up.
  145. */
  146. #ifdef L_strlen
  147. size_t bb_strlen(const char *string)
  148. {
  149. return(__builtin_strlen(string));
  150. }
  151. #endif
  152. /* END CODE */
  153. /*
  154. Local Variables:
  155. c-file-style: "linux"
  156. c-basic-offset: 4
  157. tab-width: 4
  158. End:
  159. */