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