]>
Commit | Line | Data |
---|---|---|
3b960317 | 1 | /* |
64bbab09 | 2 | * "$Id: string.c,v 1.5.2.9 2004/02/25 16:58:32 mike Exp $" |
3b960317 | 3 | * |
4 | * String functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
1d9595ab | 6 | * Copyright 1997-2003 by Easy Software Products. |
3b960317 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
8784b6a6 | 17 | * 44141 Airport View Drive, Suite 204 |
3b960317 | 18 | * Hollywood, Maryland 20636-3111 USA |
19 | * | |
20 | * Voice: (301) 373-9603 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
dab1a4d8 | 24 | * This file is subject to the Apple OS-Developed Software exception. |
25 | * | |
3b960317 | 26 | * Contents: |
27 | * | |
ff40b65e | 28 | * cups_strcpy() - Copy a string allowing for overlapping strings. |
aa7e125a | 29 | * cups_strdup() - Duplicate a string. |
30 | * cups_strcasecmp() - Do a case-insensitive comparison. | |
31 | * cups_strncasecmp() - Do a case-insensitive comparison on up to N chars. | |
def978d5 | 32 | * cups_strlcat() - Safely concatenate two strings. |
33 | * cups_strlcpy() - Safely copy two strings. | |
3b960317 | 34 | */ |
35 | ||
36 | /* | |
37 | * Include necessary headers... | |
38 | */ | |
39 | ||
40 | #include "string.h" | |
41 | ||
42 | ||
ff40b65e | 43 | /* |
44 | * 'cups_strcpy()' - Copy a string allowing for overlapping strings. | |
45 | */ | |
46 | ||
47 | void | |
48 | cups_strcpy(char *dst, /* I - Destination string */ | |
49 | const char *src) /* I - Source string */ | |
50 | { | |
51 | while (*src) | |
52 | *dst++ = *src++; | |
53 | ||
54 | *dst = '\0'; | |
55 | } | |
56 | ||
57 | ||
3b960317 | 58 | /* |
aa7e125a | 59 | * 'cups_strdup()' - Duplicate a string. |
3b960317 | 60 | */ |
61 | ||
62 | # ifndef HAVE_STRDUP | |
a87857fe | 63 | char * /* O - New string pointer */ |
aa7e125a | 64 | cups_strdup(const char *s) /* I - String to duplicate */ |
3b960317 | 65 | { |
a87857fe | 66 | char *t; /* New string pointer */ |
3b960317 | 67 | |
68 | ||
69 | if (s == NULL) | |
70 | return (NULL); | |
71 | ||
72 | if ((t = malloc(strlen(s) + 1)) == NULL) | |
73 | return (NULL); | |
74 | ||
75 | return (strcpy(t, s)); | |
76 | } | |
77 | # endif /* !HAVE_STRDUP */ | |
78 | ||
79 | ||
80 | /* | |
aa7e125a | 81 | * 'cups_strcasecmp()' - Do a case-insensitive comparison. |
3b960317 | 82 | */ |
83 | ||
84 | # ifndef HAVE_STRCASECMP | |
85 | int /* O - Result of comparison (-1, 0, or 1) */ | |
aa7e125a | 86 | cups_strcasecmp(const char *s, /* I - First string */ |
a87857fe | 87 | const char *t) /* I - Second string */ |
3b960317 | 88 | { |
89 | while (*s != '\0' && *t != '\0') | |
90 | { | |
64bbab09 | 91 | if (tolower(*s & 255) < tolower(*t & 255)) |
3b960317 | 92 | return (-1); |
64bbab09 | 93 | else if (tolower(*s & 255) > tolower(*t & 255)) |
3b960317 | 94 | return (1); |
95 | ||
96 | s ++; | |
97 | t ++; | |
e8fda7b9 | 98 | } |
3b960317 | 99 | |
100 | if (*s == '\0' && *t == '\0') | |
101 | return (0); | |
102 | else if (*s != '\0') | |
103 | return (1); | |
104 | else | |
105 | return (-1); | |
106 | } | |
107 | # endif /* !HAVE_STRCASECMP */ | |
108 | ||
109 | /* | |
aa7e125a | 110 | * 'cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars. |
3b960317 | 111 | */ |
112 | ||
113 | # ifndef HAVE_STRNCASECMP | |
114 | int /* O - Result of comparison (-1, 0, or 1) */ | |
aa7e125a | 115 | cups_strncasecmp(const char *s, /* I - First string */ |
a87857fe | 116 | const char *t, /* I - Second string */ |
117 | size_t n) /* I - Maximum number of characters to compare */ | |
3b960317 | 118 | { |
119 | while (*s != '\0' && *t != '\0' && n > 0) | |
120 | { | |
64bbab09 | 121 | if (tolower(*s & 255) < tolower(*t & 255)) |
3b960317 | 122 | return (-1); |
64bbab09 | 123 | else if (tolower(*s & 255) > tolower(*t & 255)) |
3b960317 | 124 | return (1); |
125 | ||
126 | s ++; | |
127 | t ++; | |
128 | n --; | |
e8fda7b9 | 129 | } |
3b960317 | 130 | |
131 | if (n == 0) | |
132 | return (0); | |
133 | else if (*s == '\0' && *t == '\0') | |
134 | return (0); | |
135 | else if (*s != '\0') | |
136 | return (1); | |
137 | else | |
138 | return (-1); | |
139 | } | |
def978d5 | 140 | #endif /* !HAVE_STRNCASECMP */ |
3b960317 | 141 | |
142 | ||
def978d5 | 143 | #ifndef HAVE_STRLCAT |
3b960317 | 144 | /* |
def978d5 | 145 | * 'cups_strlcat()' - Safely concatenate two strings. |
146 | */ | |
147 | ||
148 | size_t /* O - Length of string */ | |
149 | cups_strlcat(char *dst, /* O - Destination string */ | |
150 | const char *src, /* I - Source string */ | |
151 | size_t size) /* I - Size of destination string buffer */ | |
152 | { | |
153 | size_t srclen; /* Length of source string */ | |
154 | size_t dstlen; /* Length of destination string */ | |
155 | ||
156 | ||
157 | /* | |
158 | * Figure out how much room is left... | |
159 | */ | |
160 | ||
161 | dstlen = strlen(dst); | |
162 | size -= dstlen + 1; | |
163 | ||
164 | if (!size) | |
165 | return (dstlen); /* No room, return immediately... */ | |
166 | ||
167 | /* | |
168 | * Figure out how much room is needed... | |
169 | */ | |
170 | ||
171 | srclen = strlen(src); | |
172 | ||
173 | /* | |
174 | * Copy the appropriate amount... | |
175 | */ | |
176 | ||
177 | if (srclen > size) | |
178 | srclen = size; | |
179 | ||
180 | memcpy(dst + dstlen, src, srclen); | |
181 | dst[dstlen + srclen] = '\0'; | |
182 | ||
183 | return (dstlen + srclen); | |
184 | } | |
185 | #endif /* !HAVE_STRLCAT */ | |
186 | ||
187 | ||
188 | #ifndef HAVE_STRLCPY | |
189 | /* | |
190 | * 'cups_strlcpy()' - Safely copy two strings. | |
191 | */ | |
192 | ||
193 | size_t /* O - Length of string */ | |
194 | cups_strlcpy(char *dst, /* O - Destination string */ | |
195 | const char *src, /* I - Source string */ | |
196 | size_t size) /* I - Size of destination string buffer */ | |
197 | { | |
198 | size_t srclen; /* Length of source string */ | |
199 | ||
200 | ||
201 | /* | |
202 | * Figure out how much room is needed... | |
203 | */ | |
204 | ||
205 | size --; | |
206 | ||
207 | srclen = strlen(src); | |
208 | ||
209 | /* | |
210 | * Copy the appropriate amount... | |
211 | */ | |
212 | ||
213 | if (srclen > size) | |
214 | srclen = size; | |
215 | ||
216 | memcpy(dst, src, srclen); | |
217 | dst[srclen] = '\0'; | |
218 | ||
219 | return (srclen); | |
220 | } | |
221 | #endif /* !HAVE_STRLCPY */ | |
222 | ||
223 | ||
224 | /* | |
64bbab09 | 225 | * End of "$Id: string.c,v 1.5.2.9 2004/02/25 16:58:32 mike Exp $". |
3b960317 | 226 | */ |