reindent-20030715
[openafs.git] / src / util / casestrcpy.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID
14     ("$Header$");
15
16 #ifdef HAVE_STRING_H
17 #include <string.h>
18 #else
19 #ifdef HAVE_STRINGS_H
20 #include <strings.h>
21 #endif
22 #endif
23 #include <ctype.h>
24 #include <stddef.h>
25 #include <stdarg.h>
26
27 /* Just like strncpy but shift-case in transit and forces null termination */
28 char *
29 lcstring(char *d, char *s, int n)
30 {
31     char *original_d = d;
32     char c;
33
34     if ((s == 0) || (d == 0))
35         return 0;               /* just to be safe */
36     while (n) {
37         c = *s++;
38         if (isupper(c))
39             c = tolower(c);
40         *d++ = c;
41         if (c == 0)
42             break;              /* quit after transferring null */
43         if (--n == 0)
44             *(d - 1) = 0;       /* make sure null terminated */
45     }
46     return original_d;
47 }
48
49 char *
50 ucstring(char *d, char *s, int n)
51 {
52     char *original_d = d;
53     char c;
54
55     if ((s == 0) || (d == 0))
56         return 0;
57     while (n) {
58         c = *s++;
59         if (islower(c))
60             c = toupper(c);
61         *d++ = c;
62         if (c == 0)
63             break;
64         if (--n == 0)
65             *(d - 1) = 0;       /* make sure null terminated */
66     }
67     return original_d;
68 }
69
70 /* strcompose - concatenate strings passed to it.
71  * Input: 
72  *   buf: storage for the composed string. Any data in it will be lost.
73  *   len: length of the buffer.
74  *   ...: variable number of string arguments. The last argument must be
75  *        NULL. 
76  * Returns buf or NULL if the buffer was not sufficiently large.
77  */
78 char *
79 strcompose(char *buf, size_t len, ...)
80 {
81     va_list ap;
82     size_t spaceleft = len - 1;
83     char *str;
84     size_t slen;
85
86     if (buf == NULL || len <= 0)
87         return NULL;
88
89     *buf = '\0';
90     va_start(ap, len);
91     str = va_arg(ap, char *);
92     while (str) {
93         slen = strlen(str);
94         if (spaceleft < slen)   /* not enough space left */
95             return NULL;
96
97         strcat(buf, str);
98         spaceleft -= slen;
99
100         str = va_arg(ap, char *);
101     }
102     va_end(ap);
103
104     return buf;
105 }