1 /* $OpenBSD: magic-common.c,v 1.2 2015/08/11 21:42:16 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2015 Nicholas Marriott <nicm@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 21 #include <ctype.h> 22 #include <errno.h> 23 #include <limits.h> 24 #include <stdarg.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 29 #include "magic.h" 30 31 char * 32 magic_strtoull(const char *s, uint64_t *u) 33 { 34 char *endptr; 35 36 if (*s == '-') 37 return (NULL); 38 errno = 0; 39 *u = strtoull(s, &endptr, 0); 40 if (*s == '\0') 41 return (NULL); 42 if (errno == ERANGE && *u == ULLONG_MAX) 43 return (NULL); 44 if (*endptr == 'L') 45 endptr++; 46 return (endptr); 47 } 48 49 char * 50 magic_strtoll(const char *s, int64_t *i) 51 { 52 char *endptr; 53 54 errno = 0; 55 *i = strtoll(s, &endptr, 0); 56 if (*s == '\0') 57 return (NULL); 58 if (errno == ERANGE && *i == LLONG_MAX) 59 return (NULL); 60 if (*endptr == 'L') 61 endptr++; 62 return (endptr); 63 } 64 65 void 66 magic_vwarnm(struct magic *m, u_int line, const char *fmt, va_list ap) 67 { 68 char *msg; 69 70 if (!m->warnings) 71 return; 72 73 if (vasprintf(&msg, fmt, ap) == -1) 74 return; 75 fprintf(stderr, "%s:%u: %s\n", m->path, line, msg); 76 free(msg); 77 } 78 79 void 80 magic_warnm(struct magic *m, u_int line, const char *fmt, ...) 81 { 82 va_list ap; 83 84 va_start(ap, fmt); 85 magic_vwarnm (m, line, fmt, ap); 86 va_end(ap); 87 } 88 89 void 90 magic_warn(struct magic_line *ml, const char *fmt, ...) 91 { 92 va_list ap; 93 94 va_start(ap, fmt); 95 magic_vwarnm (ml->root, ml->line, fmt, ap); 96 va_end(ap); 97 } 98