"Fossies" - the Fresh Open Source Software Archive

Member "whois/utils.c" (27 Jul 2017, 2376 Bytes) of package /linux/privat/whois_5.5.9.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "utils.c" see the Fossies "Dox" file reference documentation.

    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     int i;
   50     unsigned int num_env = 0;
   51 
   52     if (!args)
   53     return argv;
   54 
   55     argstring = NOFAIL(strdup(args));
   56     for (arg = strtok(argstring, " "); arg; arg = strtok(NULL, " ")) {
   57     num_env++;
   58     newargs = NOFAIL(realloc(newargs,
   59             sizeof(newargs[0]) * (num_env + *argc + 1)));
   60     newargs[num_env] = arg;
   61     }
   62 
   63     if (!newargs)
   64     return argv;
   65 
   66     /* Append commandline args */
   67     newargs[0] = argv[0];
   68     for (i = 1; i <= *argc; i++)
   69     newargs[num_env + i] = argv[i];
   70 
   71     *argc += num_env;
   72     return newargs;
   73 }
   74 
   75 /* Error routines */
   76 void NORETURN err_sys(const char *fmt, ...)
   77 {
   78     va_list ap;
   79 
   80     va_start(ap, fmt);
   81     vfprintf(stderr, fmt, ap);
   82     fprintf(stderr, ": %s\n", strerror(errno));
   83     va_end(ap);
   84     exit(2);
   85 }
   86 
   87 void NORETURN err_quit(const char *fmt, ...)
   88 {
   89     va_list ap;
   90 
   91     va_start(ap, fmt);
   92     vfprintf(stderr, fmt, ap);
   93     fputs("\n", stderr);
   94     va_end(ap);
   95     exit(2);
   96 }
   97