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