1 /* @(#)wcslcatl.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: (wchar_t *)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/wchar.h>
32 #include <schily/string.h>
33 #include <schily/schily.h>
34 
35 /* VARARGS3 */
36 #ifdef	PROTOTYPES
37 EXPORT size_t
wcslcatl(wchar_t * to,size_t len,...)38 wcslcatl(wchar_t *to, size_t len, ...)
39 #else
40 EXPORT size_t
41 wcslcatl(to, len, va_alist)
42 	wchar_t	*to;
43 	size_t	len;
44 	va_dcl
45 #endif
46 {
47 		va_list	args;
48 	register wchar_t	*p;
49 	register wchar_t	*tor = to;
50 		size_t		olen = len;
51 
52 #ifdef	PROTOTYPES
53 	va_start(args, len);
54 #else
55 	va_start(args);
56 #endif
57 	if (len > 0) {
58 		while (--len > 0 && *tor++ != '\0')
59 			;
60 
61 		if (len == 0)
62 			goto toolong;
63 
64 		while ((p = va_arg(args, wchar_t *)) != NULL) {
65 			tor--;
66 			len++;
67 			while (--len > 0 && (*tor++ = *p++) != '\0')
68 				;
69 			if (len == 0) {
70 				*tor = '\0';
71 				olen = tor - to + wcslen(p);
72 				goto toolong;
73 			}
74 		}
75 		olen = --tor - to;
76 	}
77 	va_end(args);
78 	return (olen);
79 toolong:
80 	while ((p = va_arg(args, wchar_t *)) != NULL) {
81 		olen += wcslen(p);
82 	}
83 	va_end(args);
84 	return (olen);
85 }
86