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