PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/textproc/nbsed/files/misc.c

https://github.com/Morgawr/minix-pkgsrc
C | 179 lines | 84 code | 14 blank | 81 comment | 11 complexity | 0976cc5d9aa3cd87641278c74a5ef66e MD5 | raw file
  1. /* $NetBSD: misc.c,v 1.8 2003/08/07 11:15:50 agc Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Diomidis Spinellis of Imperial College, University of London.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. /*-
  34. * Copyright (c) 1992 Diomidis Spinellis.
  35. *
  36. * This code is derived from software contributed to Berkeley by
  37. * Diomidis Spinellis of Imperial College, University of London.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. * 3. All advertising materials mentioning features or use of this software
  48. * must display the following acknowledgement:
  49. * This product includes software developed by the University of
  50. * California, Berkeley and its contributors.
  51. * 4. Neither the name of the University nor the names of its contributors
  52. * may be used to endorse or promote products derived from this software
  53. * without specific prior written permission.
  54. *
  55. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  56. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  58. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  59. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  60. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  61. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  62. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  63. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  64. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  65. * SUCH DAMAGE.
  66. */
  67. #if HAVE_CONFIG_H
  68. #include "config.h"
  69. #endif
  70. #include <nbcompat.h>
  71. #if HAVE_SYS_CDEFS_H
  72. #include <sys/cdefs.h>
  73. #endif
  74. #ifndef lint
  75. #if 0
  76. static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
  77. #else
  78. __RCSID("$NetBSD: misc.c,v 1.8 2003/08/07 11:15:50 agc Exp $");
  79. #endif
  80. #endif /* not lint */
  81. #if HAVE_SYS_TYPES_H
  82. #include <sys/types.h>
  83. #endif
  84. #if HAVE_ERRNO_H
  85. #include <errno.h>
  86. #endif
  87. #if HAVE_REGEX_H
  88. #include <regex.h>
  89. #endif
  90. #if HAVE_STDARG_H
  91. #include <stdarg.h>
  92. #endif
  93. #if HAVE_STDIO_H
  94. #include <stdio.h>
  95. #endif
  96. #if HAVE_STDLIB_H
  97. #include <stdlib.h>
  98. #endif
  99. #if HAVE_STRING_H
  100. #include <string.h>
  101. #endif
  102. #include "defs.h"
  103. #include "extern.h"
  104. /*
  105. * malloc with result test
  106. */
  107. void *
  108. xmalloc(u_int size)
  109. {
  110. void *p;
  111. if ((p = malloc(size)) == NULL)
  112. err(FATAL, "%s", strerror(errno));
  113. return (p);
  114. }
  115. /*
  116. * realloc with result test
  117. */
  118. void *
  119. xrealloc(void *p, u_int size)
  120. {
  121. if (p == NULL) /* Compatibility hack. */
  122. return (xmalloc(size));
  123. if ((p = realloc(p, size)) == NULL)
  124. err(FATAL, "%s", strerror(errno));
  125. return (p);
  126. }
  127. /*
  128. * Return a string for a regular expression error passed. This is a overkill,
  129. * because of the silly semantics of regerror (we can never know the size of
  130. * the buffer).
  131. */
  132. char *
  133. strregerror(int errcode, regex_t *preg)
  134. {
  135. static char *oe;
  136. size_t s;
  137. if (oe != NULL)
  138. free(oe);
  139. s = regerror(errcode, preg, "", 0);
  140. oe = xmalloc(s);
  141. (void)regerror(errcode, preg, oe, s);
  142. return (oe);
  143. }
  144. /*
  145. * Error reporting function
  146. */
  147. void
  148. err(int severity, const char *fmt, ...)
  149. {
  150. va_list ap;
  151. va_start(ap, fmt);
  152. (void)fprintf(stderr, "sed: ");
  153. switch (severity) {
  154. case WARNING:
  155. case COMPILE:
  156. (void)fprintf(stderr, "%lu: %s: ", linenum, fname);
  157. }
  158. (void)vfprintf(stderr, fmt, ap);
  159. va_end(ap);
  160. (void)fprintf(stderr, "\n");
  161. if (severity == WARNING)
  162. return;
  163. exit(1);
  164. /* NOTREACHED */
  165. }