"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.4.1/src/envarg.c" (12 Oct 2016, 3408 Bytes) of archive /linux/misc/tin-2.4.1.tar.gz:


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 "envarg.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.4.0_vs_2.4.1.

    1 /*
    2  *  Project   : tin - a Usenet reader
    3  *  Module    : envarg.c
    4  *  Author    : B. Davidson
    5  *  Created   : 1991-10-13
    6  *  Updated   : 1993-03-10
    7  *  Notes     : Adds default options from environment to command line
    8  *
    9  * Copyright (c) 1991-2017 Bill Davidson
   10  * All rights reserved.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. The name of the author may not be used to endorse or promote
   21  *    products derived from this software without specific prior written
   22  *    permission.
   23  *
   24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
   25  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
   28  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
   30  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   35  */
   36 
   37 
   38 #ifndef TIN_H
   39 #   include "tin.h"
   40 #endif /* !TIN_H */
   41 
   42 static int count_args(char *s);
   43 
   44 static int
   45 count_args(
   46     char *s)
   47 {
   48     int ch, count = 0;
   49 
   50     do {
   51         /*
   52          * count and skip args
   53          */
   54         ++count;
   55         while ((ch = *s) != '\0' && ch != ' ')
   56             ++s;
   57         while ((ch = *s) != '\0' && ch == ' ')
   58             ++s;
   59     } while (ch);
   60 
   61     return count;
   62 }
   63 
   64 
   65 void
   66 envargs(
   67     int *Pargc,
   68     char ***Pargv,
   69     const char *envstr)
   70 {
   71     char *envptr;           /* value returned by getenv */
   72     char *bufptr;           /* copy of env info */
   73     int argc;           /* internal arg count */
   74     int ch;             /* spare temp value */
   75     char **argv;            /* internal arg vector */
   76     char **argvect;         /* copy of vector address */
   77 
   78     /*
   79      * see if anything in the environment
   80      */
   81     envptr = getenv(envstr);
   82     if (envptr == NULL || *envptr == 0)
   83         return;
   84 
   85     /*
   86      * count the args so we can allocate room for them
   87      */
   88     argc = count_args(envptr);
   89     bufptr = my_strdup(envptr);
   90 
   91     /*
   92      * allocate a vector large enough for all args
   93      */
   94     argv = my_malloc((argc + *Pargc + 1) * sizeof(char *));
   95     argvect = argv;
   96 
   97     /*
   98      * copy the program name first, that's always true
   99      */
  100     *(argv++) = *((*Pargv)++);
  101 
  102     /*
  103      * copy the environment args first, may be changed
  104      */
  105     do {
  106         *(argv++) = bufptr;
  107         /*
  108          * skip the arg and any trailing blanks
  109          */
  110         while ((ch = *bufptr) != '\0' && ch != ' ')
  111             ++bufptr;
  112         if (ch == ' ')
  113             *(bufptr++) = '\0';
  114         while ((ch = *bufptr) != '\0' && ch == ' ')
  115             ++bufptr;
  116     } while (ch);
  117 
  118     /*
  119      * now save old argc and copy in the old args
  120      */
  121     argc += *Pargc;
  122     while (--(*Pargc))
  123         *(argv++) = *((*Pargv)++);
  124 
  125     /*
  126      * finally, add a NULL after the last arg, like UNIX
  127      */
  128     *argv = NULL;
  129 
  130     /*
  131      * save the values and return
  132      */
  133     *Pargv = argvect;
  134     *Pargc = argc;
  135 }