1 /* $OpenBSD: magic-common.c,v 1.1 2015/04/24 16:24:11 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_warn(struct magic_line *ml, const char *fmt, ...) 67 { 68 va_list ap; 69 char *msg; 70 71 if (!ml->root->warnings) 72 return; 73 74 va_start(ap, fmt); 75 if (vasprintf(&msg, fmt, ap) == -1) { 76 va_end(ap); 77 return; 78 } 79 va_end(ap); 80 81 fprintf(stderr, "%s:%u: %s\n", ml->root->path, ml->line, msg); 82 free(msg); 83 } 84