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