home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume14 / xtoolplaces / part01 / combine.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-26  |  1.9 KB  |  70 lines

  1. /*Copyright (c) 1991 Xerox Corporation.  All Rights Reserved.
  2.  
  3.   Permission to use,  copy,  modify  and  distribute  without
  4.   charge this software, documentation, images, etc. is grant-
  5.   ed, provided that this copyright and the author's  name  is
  6.   retained.
  7.  
  8.   A fee may be charged for this program ONLY to recover costs
  9.   for distribution (i.e. media costs).  No profit can be made
  10.   on this program.
  11.  
  12.   The author assumes no responsibility for disasters (natural
  13.   or otherwise) as a consequence of use of this software.
  14.  
  15.   Adam Stein (stein.wbst129@xerox.com)
  16. */
  17.  
  18. #include <stdio.h>
  19.  
  20. extern char *program;
  21.  
  22. /*This function will combine multiple argv[] strings into a single string.
  23.  
  24.   Inputs:  argc     - number of arguments
  25.        argv     - list of arguments
  26.   Outputs: pointer  - pointer to single string of arguments
  27.   Locals:  loop     - loop through arguments
  28.        numbytes - number of bytes needed for single argument string
  29.        pointer  - pointer to single string of arguments
  30.   Globals: NULL     - 0
  31. */
  32. char *combine(argc,argv)
  33. register int argc;
  34. register char *argv[];
  35. {
  36.     register int numbytes,loop;
  37.     register char *pointer;
  38.     char *malloc(),*strdup();
  39.  
  40.     /*If argc equals 1, then all the arguments are already in a single
  41.       string and there's no reason to do anything else but copy it*/
  42.     if(argc != 1) {
  43.       /*Set numbytes initially to count the spaces between arguments
  44.         and the NULL (number of arguments - 1 + 1)*/
  45.       numbytes = argc;
  46.  
  47.       /*Count bytes in each argument*/
  48.       for(loop = 0;loop < argc;++loop)
  49.         numbytes += strlen(argv[loop]);
  50.  
  51.       if((pointer = malloc(numbytes)) == NULL) {
  52.         perror(program);
  53.         exit(1);
  54.       }
  55.  
  56.       strcpy(pointer,argv[0]);
  57.  
  58.       for(loop = 1;loop < argc;++loop) {
  59.         strcat(pointer," ");
  60.         strcat(pointer,argv[loop]);
  61.       }
  62.     } else if((pointer = strdup(argv[0])) == NULL) {
  63.          perror(program);
  64.          exit(1);
  65.            }
  66.  
  67.     return(pointer);
  68. }
  69.  
  70.