eca60b63d1b7149a4404f946e53503e7c7a72704
[openafs.git] / src / util / casestrcpy.c
1 /*
2 ****************************************************************************
3 *        Copyright IBM Corporation 1988, 1989 - All Rights Reserved        *
4 *                                                                          *
5 * Permission to use, copy, modify, and distribute this software and its    *
6 * documentation for any purpose and without fee is hereby granted,         *
7 * provided that the above copyright notice appear in all copies and        *
8 * that both that copyright notice and this permission notice appear in     *
9 * supporting documentation, and that the name of IBM not be used in        *
10 * advertising or publicity pertaining to distribution of the software      *
11 * without specific, written prior permission.                              *
12 *                                                                          *
13 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
15 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY      *
16 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER  *
17 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING   *
18 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.    *
19 ****************************************************************************
20 */
21
22 #include <afs/param.h>
23 #include <ctype.h>
24 #include <stddef.h>
25 #include <stdarg.h>
26
27 /* Just like strncpy but shift-case in transit and forces null termination */
28 char *lcstring (char *d, char *s, int n)
29 {   char *original_d = d;
30     char  c;
31
32     if ((s == 0) || (d == 0)) return 0; /* just to be safe */
33     while (n) {
34         c = *s++;
35         if (isupper(c)) c = tolower(c);
36         *d++ = c;
37         if (c == 0) break;              /* quit after transferring null */
38         if (--n == 0) *(d-1) = 0;       /* make sure null terminated */
39     }
40     return original_d;
41 }
42
43 char *ucstring (char *d, char *s, int n)
44 {   char *original_d = d;
45     char  c;
46
47     if ((s == 0) || (d == 0)) return 0;
48     while (n) {
49         c = *s++;
50         if (islower(c)) c = toupper(c);
51         *d++ = c;
52         if (c == 0) break;
53         if (--n == 0) *(d-1) = 0;       /* make sure null terminated */
54     }
55     return original_d;
56 }
57
58 /* strcompose - concatenate strings passed to it.
59  * Input: 
60  *   buf: storage for the composed string. Any data in it will be lost.
61  *   len: length of the buffer.
62  *   ...: variable number of string arguments. The last argument must be
63  *        NULL. 
64  * Returns buf or NULL if the buffer was not sufficiently large.
65  */             
66 char *strcompose(char *buf, size_t len, ...)
67 {
68   va_list ap;
69   size_t spaceleft = len - 1;
70   char *str;
71   size_t slen; 
72
73   if (buf == NULL || len <= 0)
74     return NULL;
75
76   *buf = '\0';
77   va_start(ap, len);
78   str = va_arg(ap, char *);
79   while(str) { 
80     slen = strlen(str);
81     if (spaceleft < slen) /* not enough space left */
82       return NULL;
83     
84     strcat(buf, str);
85     spaceleft -= slen;
86     
87     str = va_arg(ap, char *);
88   }
89   va_end(ap);
90
91   return buf;
92 }
93