convert-volparse-to-afsconfig-and-cleanup-20010605
[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 <afs/param.h>
11 #include <ctype.h>
12 #include <stddef.h>
13 #include <stdarg.h>
14
15 /* Just like strncpy but shift-case in transit and forces null termination */
16 char *lcstring (char *d, char *s, int n)
17 {   char *original_d = d;
18     char  c;
19
20     if ((s == 0) || (d == 0)) return 0; /* just to be safe */
21     while (n) {
22         c = *s++;
23         if (isupper(c)) c = tolower(c);
24         *d++ = c;
25         if (c == 0) break;              /* quit after transferring null */
26         if (--n == 0) *(d-1) = 0;       /* make sure null terminated */
27     }
28     return original_d;
29 }
30
31 char *ucstring (char *d, char *s, int n)
32 {   char *original_d = d;
33     char  c;
34
35     if ((s == 0) || (d == 0)) return 0;
36     while (n) {
37         c = *s++;
38         if (islower(c)) c = toupper(c);
39         *d++ = c;
40         if (c == 0) break;
41         if (--n == 0) *(d-1) = 0;       /* make sure null terminated */
42     }
43     return original_d;
44 }
45
46 /* strcompose - concatenate strings passed to it.
47  * Input: 
48  *   buf: storage for the composed string. Any data in it will be lost.
49  *   len: length of the buffer.
50  *   ...: variable number of string arguments. The last argument must be
51  *        NULL. 
52  * Returns buf or NULL if the buffer was not sufficiently large.
53  */             
54 char *strcompose(char *buf, size_t len, ...)
55 {
56   va_list ap;
57   size_t spaceleft = len - 1;
58   char *str;
59   size_t slen; 
60
61   if (buf == NULL || len <= 0)
62     return NULL;
63
64   *buf = '\0';
65   va_start(ap, len);
66   str = va_arg(ap, char *);
67   while(str) { 
68     slen = strlen(str);
69     if (spaceleft < slen) /* not enough space left */
70       return NULL;
71     
72     strcat(buf, str);
73     spaceleft -= slen;
74     
75     str = va_arg(ap, char *);
76   }
77   va_end(ap);
78
79   return buf;
80 }
81