home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / libcs / concat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  2.8 KB  |  101 lines

  1. /*
  2.  * Copyright (c) 1990 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
  12.  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT
  14.  * SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
  15.  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  17.  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * Users of this software agree to return to Carnegie Mellon any
  21.  * improvements or extensions that they make and grant Carnegie the
  22.  * rights to redistribute these changes.
  23.  *
  24.  * Export of this software is permitted only after complying with the
  25.  * regulations of the U.S. Deptartment of Commerce relating to the
  26.  * Export of Technical Data.
  27.  */
  28. /*
  29.  * subroutine for concatenating strings into a buffer
  30.  *
  31.  * char *concat(buf, buflen, ptr1, ptr2, ..., NULL) { char *ep; return(ep); }
  32.  * char *buf, *ptr1, *ptr2, ...;
  33.  * int buflen;
  34.  *
  35.  * "buflen" should be sizeof("buf")
  36.  * "buf" will be terminated by a null byte
  37.  * "concat" will return a pointer to the null byte, if return is non-null
  38.  *
  39.  * concat will return null(0) under any of the following conditions:
  40.  *    1) buf is null
  41.  *    2) buflen <= 0
  42.  *    3) buf was not large enough to hold the contents of all the ptrs.
  43.  *
  44.  **********************************************************************
  45.  * HISTORY
  46.  * $Log:    concat.c,v $
  47.  * Revision 2.3  90/12/11  17:51:13  mja
  48.  *     Add copyright/disclaimer for distribution.
  49.  * 
  50.  * Revision 2.2  88/12/13  13:51:06  gm0w
  51.  *     Created.
  52.  *     [88/12/04            gm0w]
  53.  * 
  54.  **********************************************************************
  55.  */
  56.  
  57. #include <stdio.h>
  58. #include <varargs.h>
  59.  
  60. char *vconcat();
  61.  
  62. /*VARARGS2*/
  63. char *
  64. concat(buf, buflen, va_alist)
  65. char *buf;
  66. int buflen;
  67. va_dcl
  68. {
  69.     char *ptr;
  70.     va_list ap;
  71.  
  72.     va_start(ap);
  73.     ptr = vconcat(buf, buflen, ap);
  74.     va_end(ap);
  75.     return(ptr);
  76. }
  77.  
  78. char *
  79. vconcat(buf, buflen, ap)
  80. char *buf;
  81. int buflen;
  82. va_list ap;
  83. {
  84.     register char *arg, *ptr, *ep;
  85.  
  86.     if (buf == NULL)
  87.     return(NULL);
  88.     if (buflen <= 0)
  89.     return(NULL);
  90.     ptr = buf;
  91.     *ptr = '\0';
  92.     ep = buf + buflen;
  93.     while (ptr != NULL && (arg = va_arg(ap, char *)) != NULL)
  94.     while (*ptr = *arg++)
  95.         if (++ptr == ep) {
  96.         ptr = NULL;
  97.         break;
  98.         }
  99.     return(ptr);
  100. }
  101.