"Fossies" - the Fresh Open Source Software Archive

Member "tin-2.6.2/src/envarg.c" (9 Dec 2022, 3519 Bytes) of package /linux/misc/tin-2.6.2.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 "envarg.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.6.1_vs_2.6.2.

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