misc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* $OpenBSD: misc.c,v 1.12 2017/01/20 10:26:16 krw Exp $ */
  2. /*-
  3. * Copyright (c) 2016
  4. * mirabilos <m@mirbsd.org>
  5. * Copyright (c) 1992 Diomidis Spinellis.
  6. * Copyright (c) 1992, 1993
  7. * The Regents of the University of California. All rights reserved.
  8. *
  9. * This code is derived from software contributed to Berkeley by
  10. * Diomidis Spinellis of Imperial College, University of London.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. /* $OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $ */
  37. /*
  38. * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
  39. *
  40. * Permission to use, copy, modify, and distribute this software for any
  41. * purpose with or without fee is hereby granted, provided that the above
  42. * copyright notice and this permission notice appear in all copies.
  43. *
  44. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  45. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  46. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  47. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  48. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  49. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  50. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  51. */
  52. #include <sys/types.h>
  53. #include <errno.h>
  54. #include <regex.h>
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <stdarg.h>
  59. #include "defs.h"
  60. #include "extern.h"
  61. __RCSID("$MirOS: src/usr.bin/sed/misc.c,v 1.3 2017/11/20 01:23:57 tg Exp $");
  62. /*
  63. * malloc with result test
  64. */
  65. void *
  66. xmalloc(size_t size)
  67. {
  68. void *p;
  69. if ((p = malloc(size)) == NULL)
  70. error(FATAL, "%s", strerror(errno));
  71. return (p);
  72. }
  73. /*
  74. * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
  75. * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
  76. */
  77. #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
  78. void *
  79. xreallocarray(void *o, size_t nmemb, size_t size)
  80. {
  81. void *p;
  82. if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
  83. nmemb > 0 && __SIZE_MAX__ / nmemb < size) {
  84. p = NULL;
  85. errno = ENOMEM;
  86. } else
  87. p = realloc(o, size * nmemb);
  88. if (p == NULL)
  89. error(FATAL, "%s", strerror(errno));
  90. return (p);
  91. }
  92. /*
  93. * realloc with result test
  94. */
  95. void *
  96. xrealloc(void *p, size_t size)
  97. {
  98. if ((p = realloc(p, size)) == NULL)
  99. error(FATAL, "%s", strerror(errno));
  100. return (p);
  101. }
  102. /*
  103. * Return a string for a regular expression error passed. This is a overkill,
  104. * because of the silly semantics of regerror (we can never know the size of
  105. * the buffer).
  106. */
  107. char *
  108. strregerror(int errcode, regex_t *preg)
  109. {
  110. static char *oe;
  111. size_t s;
  112. free(oe);
  113. s = regerror(errcode, preg, NULL, 0);
  114. oe = xmalloc(s);
  115. (void)regerror(errcode, preg, oe, s);
  116. return (oe);
  117. }
  118. /*
  119. * Error reporting function
  120. */
  121. __dead void
  122. error(int severity, const char *fmt, ...)
  123. {
  124. va_list ap;
  125. va_start(ap, fmt);
  126. (void)fprintf(stderr, "sed: ");
  127. switch (severity) {
  128. case COMPILE:
  129. (void)fprintf(stderr, "%lu: %s: ", linenum, fname);
  130. }
  131. (void)vfprintf(stderr, fmt, ap);
  132. va_end(ap);
  133. (void)fprintf(stderr, "\n");
  134. exit(1);
  135. }
  136. void
  137. warning(const char *fmt, ...)
  138. {
  139. va_list ap;
  140. va_start(ap, fmt);
  141. (void)fprintf(stderr, "sed: ");
  142. (void)fprintf(stderr, "%lu: %s: ", linenum, fname);
  143. (void)vfprintf(stderr, fmt, ap);
  144. va_end(ap);
  145. (void)fprintf(stderr, "\n");
  146. }