/[base]/head/usr.bin/m4/misc.c
ViewVC logotype

Diff of /head/usr.bin/m4/misc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 228063 by bapt, Mon Nov 28 13:32:39 2011 UTC revision 269162 by bapt, Sun Jul 27 22:54:13 2014 UTC
# Line 1  Line 1 
1  /*      $OpenBSD: misc.c,v 1.42 2010/09/07 19:58:09 marco Exp $ */  /*      $OpenBSD: misc.c,v 1.44 2014/05/12 19:11:19 espie Exp $ */
2  /*      $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $     */  /*      $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $     */
3    
4  /*  /*
# Line 32  Line 32 
32   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33   * SUCH DAMAGE.   * SUCH DAMAGE.
34   */   */
35    
36  #include <sys/cdefs.h>  #include <sys/cdefs.h>
37  __FBSDID("$FreeBSD$");  __FBSDID("$FreeBSD$");
38    
# Line 40  __FBSDID("$FreeBSD$"); Line 41  __FBSDID("$FreeBSD$");
41  #include <unistd.h>  #include <unistd.h>
42  #include <stdarg.h>  #include <stdarg.h>
43  #include <stdio.h>  #include <stdio.h>
44    #include <stdint.h>
45  #include <stdlib.h>  #include <stdlib.h>
46  #include <stddef.h>  #include <stddef.h>
47  #include <string.h>  #include <string.h>
# Line 165  initspaces(void) Line 167  initspaces(void)
167          strspace = xalloc(strsize+1, NULL);          strspace = xalloc(strsize+1, NULL);
168          ep = strspace;          ep = strspace;
169          endest = strspace+strsize;          endest = strspace+strsize;
170          buf = (unsigned char *)xalloc(bufsize, NULL);          buf = xalloc(bufsize, NULL);
171          bufbase = buf;          bufbase = buf;
172          bp = buf;          bp = buf;
173          endpbb = buf + bufsize;          endpbb = buf + bufsize;
# Line 239  getdiv(int n) Line 241  getdiv(int n)
241  }  }
242    
243  void  void
244  onintr(__unused int signo)  onintr(int signo __unused)
245  {  {
246  #define intrmessage     "m4: interrupted.\n"  #define intrmessage     "m4: interrupted.\n"
247          write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);          write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
# Line 263  killdiv(void) Line 265  killdiv(void)
265  extern char *__progname;  extern char *__progname;
266    
267  void  void
268  m4errx(int evaluation, const char *fmt, ...)  m4errx(int eval, const char *fmt, ...)
269  {  {
270          fprintf(stderr, "%s: ", __progname);          fprintf(stderr, "%s: ", __progname);
271          fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);          fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
# Line 275  m4errx(int evaluation, const char *fmt, Line 277  m4errx(int evaluation, const char *fmt,
277                  va_end(ap);                  va_end(ap);
278          }          }
279          fprintf(stderr, "\n");          fprintf(stderr, "\n");
280          exit(evaluation);          exit(eval);
281  }  }
282    
283  /*  /*
# Line 285  resizedivs(int n) Line 287  resizedivs(int n)
287  {  {
288          int i;          int i;
289    
290          outfile = (FILE **)xrealloc(outfile, sizeof(FILE *) * n,          outfile = xreallocarray(outfile, n, sizeof(FILE *),
291              "too many diverts %d", n);              "too many diverts %d", n);
292          for (i = maxout; i < n; i++)          for (i = maxout; i < n; i++)
293                  outfile[i] = NULL;                  outfile[i] = NULL;
# Line 312  xalloc(size_t n, const char *fmt, ...) Line 314  xalloc(size_t n, const char *fmt, ...)
314  }  }
315    
316  void *  void *
317    xcalloc(size_t n, size_t s, const char *fmt, ...)
318    {
319            void *p = calloc(n, s);
320    
321            if (p == NULL) {
322                    if (fmt == NULL)
323                            err(1, "calloc");
324                    else {
325                            va_list va;
326    
327                            va_start(va, fmt);
328                            verr(1, fmt, va);
329                            va_end(va);
330                    }
331            }
332            return p;
333    }
334    
335    void *
336  xrealloc(void *old, size_t n, const char *fmt, ...)  xrealloc(void *old, size_t n, const char *fmt, ...)
337  {  {
338          char *p = realloc(old, n);          char *p = realloc(old, n);
# Line 323  xrealloc(void *old, size_t n, const char Line 344  xrealloc(void *old, size_t n, const char
344                  else {                  else {
345                          va_list va;                          va_list va;
346    
347                            va_start(va, fmt);
348                            verr(1, fmt, va);
349                            va_end(va);
350                    }
351            }
352            return p;
353    }
354    
355    /*
356     * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
357     * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
358     */
359    #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4))
360    
361    static void *
362    reallocarray(void *optr, size_t nmemb, size_t size)
363    {
364            if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
365                nmemb > 0 && SIZE_MAX / nmemb < size) {
366                    errno = ENOMEM;
367                    return NULL;
368            }
369            return realloc(optr, size * nmemb);
370    }
371    
372    void *
373    xreallocarray(void *old, size_t s1, size_t s2, const char *fmt, ...)
374    {
375            void *p = reallocarray(old, s1, s2);
376    
377            if (p == NULL) {
378                    free(old);
379                    if (fmt == NULL)
380                            err(1, "reallocarray");
381                    else {
382                            va_list va;
383    
384                          va_start(va, fmt);                          va_start(va, fmt);
385                          verr(1, fmt, va);                          verr(1, fmt, va);
386                          va_end(va);                          va_end(va);

Legend:
Removed from v.228063  
changed lines
  Added in v.269162

  ViewVC Help
Powered by ViewVC 1.1.27