e1dc37a44b540a6e2ce4116280ad18c68e3fac72
[openafs.git] / src / opr / 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 #include <roken.h>
14
15 #include <ctype.h>
16
17 #include <afs/opr.h>
18
19 /* Just like strncpy but shift-case in transit and forces null termination */
20 char *
21 lcstring(char *d, const char *s, int n)
22 {
23     char *original_d = d;
24     char c;
25
26     if ((s == 0) || (d == 0))
27         return 0;               /* just to be safe */
28     while (n) {
29         c = *s++;
30         if (isupper(c))
31             c = tolower(c);
32         *d++ = c;
33         if (c == 0)
34             break;              /* quit after transferring null */
35         if (--n == 0)
36             *(d - 1) = 0;       /* make sure null terminated */
37     }
38     return original_d;
39 }
40
41 char *
42 ucstring(char *d, const char *s, int n)
43 {
44     char *original_d = d;
45     char c;
46
47     if ((s == 0) || (d == 0))
48         return 0;
49     while (n) {
50         c = *s++;
51         if (islower(c))
52             c = toupper(c);
53         *d++ = c;
54         if (c == 0)
55             break;
56         if (--n == 0)
57             *(d - 1) = 0;       /* make sure null terminated */
58     }
59     return original_d;
60 }
61
62 void
63 stolower(char *s)
64 {
65   while (*s) {
66         if (isupper(*s))
67             *s = tolower(*s);
68         s++;
69     }
70     return;
71 }
72
73 void
74 stoupper(char *s)
75 {
76   while (*s) {
77         if (islower(*s))
78             *s = toupper(*s);
79         s++;
80     }
81     return;
82 }
83
84 /* strcompose - concatenate strings passed to it.
85  * Input:
86  *   buf: storage for the composed string. Any data in it will be lost.
87  *   len: length of the buffer.
88  *   ...: variable number of string arguments. The last argument must be
89  *        (char *)NULL.
90  * Returns buf or NULL if the buffer was not sufficiently large.
91  */
92 char *
93 strcompose(char *buf, size_t len, ...)
94 {
95     va_list ap;
96     size_t spaceleft = len - 1;
97     char *str;
98     size_t slen;
99
100     if (buf == NULL || len <= 0)
101         return NULL;
102
103     *buf = '\0';
104     va_start(ap, len);
105     str = va_arg(ap, char *);
106     while (str) {
107         slen = strlen(str);
108         if (spaceleft < slen)   /* not enough space left */
109             return NULL;
110
111         strcat(buf, str);
112         spaceleft -= slen;
113
114         str = va_arg(ap, char *);
115     }
116     va_end(ap);
117
118     return buf;
119 }