Thomas De Schampheleire | 59e0692 | 2013-08-07 20:01:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1999-2008 by Marco d'Itri <md@linux.it>. |
| 3 | * |
| 4 | * do_nofail and merge_args come from the module-init-tools package. |
| 5 | * Copyright 2001 by Rusty Russell. |
| 6 | * Copyright 2002, 2003 by Rusty Russell, IBM Corporation. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | /* for strdup */ |
| 24 | #define _XOPEN_SOURCE 500 |
| 25 | |
| 26 | /* System library */ |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdarg.h> |
| 30 | #include <string.h> |
| 31 | #include <errno.h> |
| 32 | |
| 33 | /* Application-specific */ |
| 34 | #include "utils.h" |
| 35 | |
| 36 | void *do_nofail(void *ptr, const char *file, const int line) |
| 37 | { |
| 38 | if (ptr) |
| 39 | return ptr; |
| 40 | |
| 41 | err_quit("Memory allocation failure at %s:%d.", file, line); |
| 42 | } |
| 43 | |
| 44 | /* Prepend options from a string. */ |
| 45 | char **merge_args(char *args, char *argv[], int *argc) |
| 46 | { |
| 47 | char *arg, *argstring; |
| 48 | char **newargs = NULL; |
| 49 | unsigned int i, num_env = 0; |
| 50 | |
| 51 | if (!args) |
| 52 | return argv; |
| 53 | |
| 54 | argstring = NOFAIL(strdup(args)); |
| 55 | for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) { |
| 56 | num_env++; |
| 57 | newargs = NOFAIL(realloc(newargs, |
| 58 | sizeof(newargs[0]) * (num_env + *argc + 1))); |
| 59 | newargs[num_env] = arg; |
| 60 | } |
| 61 | |
| 62 | if (!newargs) |
| 63 | return argv; |
| 64 | |
| 65 | /* Append commandline args */ |
| 66 | newargs[0] = argv[0]; |
| 67 | for (i = 1; i <= *argc; i++) |
| 68 | newargs[num_env + i] = argv[i]; |
| 69 | |
| 70 | *argc += num_env; |
| 71 | return newargs; |
| 72 | } |
| 73 | |
| 74 | /* Error routines */ |
| 75 | void err_sys(const char *fmt, ...) |
| 76 | { |
| 77 | va_list ap; |
| 78 | |
| 79 | va_start(ap, fmt); |
| 80 | vfprintf(stderr, fmt, ap); |
| 81 | fprintf(stderr, ": %s\n", strerror(errno)); |
| 82 | va_end(ap); |
| 83 | exit(2); |
| 84 | } |
| 85 | |
| 86 | void err_quit(const char *fmt, ...) |
| 87 | { |
| 88 | va_list ap; |
| 89 | |
| 90 | va_start(ap, fmt); |
| 91 | vfprintf(stderr, fmt, ap); |
| 92 | fputs("\n", stderr); |
| 93 | va_end(ap); |
| 94 | exit(2); |
| 95 | } |
| 96 | |