1 /* @(#)strlcatl.c	1.1 15/03/03 Copyright 2015 J. Schilling */
2 /*
3  *	list version of strlcat()
4  *
5  *	concatenates all past first parameter until a NULL pointer is reached
6  *
7  *	WARNING: a NULL constant is not a NULL pointer, so a caller must
8  *		cast a NULL constant to a pointer: (char *)NULL
9  *
10  *	returns number of bytes needed to fit all
11  *
12  *	Copyright (c) 2015 J. Schilling
13  */
14 /*
15  * The contents of this file are subject to the terms of the
16  * Common Development and Distribution License, Version 1.0 only
17  * (the "License").  You may not use this file except in compliance
18  * with the License.
19  *
20  * See the file CDDL.Schily.txt in this distribution for details.
21  * A copy of the CDDL is also available via the Internet at
22  * http://www.opensource.org/licenses/cddl1.txt
23  *
24  * When distributing Covered Code, include this CDDL HEADER in each
25  * file and include the License file CDDL.Schily.txt from this distribution.
26  */
27 
28 #include <schily/mconfig.h>
29 #include <schily/varargs.h>
30 #include <schily/standard.h>
31 #include <schily/string.h>
32 #include <schily/schily.h>
33 
34 /* VARARGS3 */
35 #ifdef	PROTOTYPES
36 EXPORT size_t
strlcatl(char * to,size_t len,...)37 strlcatl(char *to, size_t len, ...)
38 #else
39 EXPORT size_t
40 strlcatl(to, len, va_alist)
41 	char	*to;
42 	size_t	len;
43 	va_dcl
44 #endif
45 {
46 		va_list	args;
47 	register char	*p;
48 	register char	*tor = to;
49 		size_t	olen = len;
50 
51 #ifdef	PROTOTYPES
52 	va_start(args, len);
53 #else
54 	va_start(args);
55 #endif
56 	if (len > 0) {
57 		while (--len > 0 && *tor++ != '\0')
58 			;
59 
60 		if (len == 0)
61 			goto toolong;
62 
63 		while ((p = va_arg(args, char *)) != NULL) {
64 			tor--;
65 			len++;
66 			while (--len > 0 && (*tor++ = *p++) != '\0')
67 				;
68 			if (len == 0) {
69 				*tor = '\0';
70 				olen = tor - to + strlen(p);
71 				goto toolong;
72 			}
73 		}
74 		olen = --tor - to;
75 	}
76 	va_end(args);
77 	return (olen);
78 toolong:
79 	while ((p = va_arg(args, char *)) != NULL) {
80 		olen += strlen(p);
81 	}
82 	va_end(args);
83 	return (olen);
84 }
85