tcpreplay: Bump version to v4.2.0
[buildroot-gz.git] / package / mkpasswd / utils.c
blob254bf2ac68b73adfe5d43c359bcd33246f9dcc1e
1 /*
2 * Copyright 1999-2008 by Marco d'Itri <md@linux.it>.
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.
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.
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.
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.
23 /* for strdup */
24 #define _XOPEN_SOURCE 500
26 /* System library */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <errno.h>
33 /* Application-specific */
34 #include "utils.h"
36 void *do_nofail(void *ptr, const char *file, const int line)
38 if (ptr)
39 return ptr;
41 err_quit("Memory allocation failure at %s:%d.", file, line);
44 /* Prepend options from a string. */
45 char **merge_args(char *args, char *argv[], int *argc)
47 char *arg, *argstring;
48 char **newargs = NULL;
49 unsigned int i, num_env = 0;
51 if (!args)
52 return argv;
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;
62 if (!newargs)
63 return argv;
65 /* Append commandline args */
66 newargs[0] = argv[0];
67 for (i = 1; i <= *argc; i++)
68 newargs[num_env + i] = argv[i];
70 *argc += num_env;
71 return newargs;
74 /* Error routines */
75 void err_sys(const char *fmt, ...)
77 va_list ap;
79 va_start(ap, fmt);
80 vfprintf(stderr, fmt, ap);
81 fprintf(stderr, ": %s\n", strerror(errno));
82 va_end(ap);
83 exit(2);
86 void err_quit(const char *fmt, ...)
88 va_list ap;
90 va_start(ap, fmt);
91 vfprintf(stderr, fmt, ap);
92 fputs("\n", stderr);
93 va_end(ap);
94 exit(2);