1 | /* $Id: strs.c 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
2 | * |
---|
3 | * Miscellaneous string functions. |
---|
4 | * |
---|
5 | * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology |
---|
6 | * For copying and distribution information, please see the file |
---|
7 | * <mit-copyright.h>. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <mit-copyright.h> |
---|
11 | #include <moira.h> |
---|
12 | |
---|
13 | #include <ctype.h> |
---|
14 | #include <string.h> |
---|
15 | |
---|
16 | RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/lib/strs.c $ $Id: strs.c 3956 2010-01-05 20:56:56Z zacheiss $"); |
---|
17 | |
---|
18 | /* |
---|
19 | * Trim whitespace off both ends of a string. |
---|
20 | */ |
---|
21 | char *strtrim(char *save) |
---|
22 | { |
---|
23 | char *t, *s; |
---|
24 | |
---|
25 | s = save; |
---|
26 | while (isspace(*s)) |
---|
27 | s++; |
---|
28 | /* skip to end of string */ |
---|
29 | if (*s == '\0') |
---|
30 | { |
---|
31 | if (*save) |
---|
32 | *save = '\0'; |
---|
33 | return save; |
---|
34 | } |
---|
35 | |
---|
36 | for (t = s; *t; t++) |
---|
37 | continue; |
---|
38 | while (t > s) |
---|
39 | { |
---|
40 | --t; |
---|
41 | if (!isspace(*t)) |
---|
42 | { |
---|
43 | t++; |
---|
44 | break; |
---|
45 | } |
---|
46 | } |
---|
47 | if (*t) |
---|
48 | *t = '\0'; |
---|
49 | return s; |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | /* Modify a string for all of the letters to be uppercase. */ |
---|
54 | |
---|
55 | char *uppercase(char *s) |
---|
56 | { |
---|
57 | char *p; |
---|
58 | |
---|
59 | for (p = s; *p; p++) |
---|
60 | { |
---|
61 | if (islower(*p)) |
---|
62 | *p = toupper(*p); |
---|
63 | } |
---|
64 | return s; |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | char *lowercase(char *s) |
---|
69 | { |
---|
70 | char *p; |
---|
71 | |
---|
72 | for (p = s; *p; p++) |
---|
73 | { |
---|
74 | if (isupper(*p)) |
---|
75 | *p = tolower(*p); |
---|
76 | } |
---|
77 | return s; |
---|
78 | } |
---|
79 | |
---|
80 | #ifndef HAVE_STRLCPY |
---|
81 | /* Copy as much of SRC will fit into a DST of size SIZE, always |
---|
82 | * NUL-terminating. (Originally from OpenBSD.) |
---|
83 | */ |
---|
84 | size_t strlcpy(char *dst, const char *src, size_t size) |
---|
85 | { |
---|
86 | size_t len = strlen(src); |
---|
87 | |
---|
88 | if (len < size) |
---|
89 | memcpy(dst, src, len + 1); |
---|
90 | else |
---|
91 | { |
---|
92 | memcpy(dst, src, size - 1); |
---|
93 | dst[size - 1] = '\0'; |
---|
94 | } |
---|
95 | return len; |
---|
96 | } |
---|
97 | #endif |
---|
98 | |
---|
99 | #ifndef HAVE_STRLCAT |
---|
100 | /* Catenate as must of SRC will fit onto the end of DST, which is |
---|
101 | * in a buffer of size SIZE, always NUL-terminating. (Originally |
---|
102 | * from OpenBSD.) |
---|
103 | */ |
---|
104 | size_t strlcat(char *dst, const char *src, size_t size) |
---|
105 | { |
---|
106 | size_t dlen = strlen(dst); |
---|
107 | size_t slen = strlen(src); |
---|
108 | |
---|
109 | if (dlen + slen < size) |
---|
110 | memcpy(dst + dlen, src, slen + 1); |
---|
111 | else |
---|
112 | { |
---|
113 | memcpy(dst + dlen, src, size - dlen - 1); |
---|
114 | dst[size - 1] = '\0'; |
---|
115 | } |
---|
116 | return dlen + slen; |
---|
117 | } |
---|
118 | #endif |
---|