]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2 | * "$Id: string.c 4683 2005-09-21 22:17:44Z mike $" | |
3 | * | |
4 | * String functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2005 by Easy Software Products. | |
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 | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * This file is subject to the Apple OS-Developed Software exception. | |
25 | * | |
26 | * Contents: | |
27 | * | |
28 | * _cups_strcpy() - Copy a string allowing for overlapping strings. | |
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. | |
32 | * _cups_strlcat() - Safely concatenate two strings. | |
33 | * _cups_strlcpy() - Safely copy two strings. | |
34 | */ | |
35 | ||
36 | /* | |
37 | * Include necessary headers... | |
38 | */ | |
39 | ||
40 | #include "string.h" | |
41 | ||
42 | ||
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 | ||
58 | /* | |
59 | * '_cups_strdup()' - Duplicate a string. | |
60 | */ | |
61 | ||
62 | #ifndef HAVE_STRDUP | |
63 | char * /* O - New string pointer */ | |
64 | _cups_strdup(const char *s) /* I - String to duplicate */ | |
65 | { | |
66 | char *t; /* New string pointer */ | |
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 | /* | |
81 | * '_cups_strcasecmp()' - Do a case-insensitive comparison. | |
82 | */ | |
83 | ||
84 | #ifndef HAVE_STRCASECMP | |
85 | int /* O - Result of comparison (-1, 0, or 1) */ | |
86 | _cups_strcasecmp(const char *s, /* I - First string */ | |
87 | const char *t) /* I - Second string */ | |
88 | { | |
89 | while (*s != '\0' && *t != '\0') | |
90 | { | |
91 | if (tolower(*s & 255) < tolower(*t & 255)) | |
92 | return (-1); | |
93 | else if (tolower(*s & 255) > tolower(*t & 255)) | |
94 | return (1); | |
95 | ||
96 | s ++; | |
97 | t ++; | |
98 | } | |
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 | /* | |
110 | * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars. | |
111 | */ | |
112 | ||
113 | #ifndef HAVE_STRNCASECMP | |
114 | int /* O - Result of comparison (-1, 0, or 1) */ | |
115 | _cups_strncasecmp(const char *s, /* I - First string */ | |
116 | vconst char *t, /* I - Second string */ | |
117 | size_t n) /* I - Maximum number of characters to compare */ | |
118 | { | |
119 | while (*s != '\0' && *t != '\0' && n > 0) | |
120 | { | |
121 | if (tolower(*s & 255) < tolower(*t & 255)) | |
122 | return (-1); | |
123 | else if (tolower(*s & 255) > tolower(*t & 255)) | |
124 | return (1); | |
125 | ||
126 | s ++; | |
127 | t ++; | |
128 | n --; | |
129 | } | |
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 | } | |
140 | #endif /* !HAVE_STRNCASECMP */ | |
141 | ||
142 | ||
143 | #ifndef HAVE_STRLCAT | |
144 | /* | |
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 | /* | |
225 | * End of "$Id: string.c 4683 2005-09-21 22:17:44Z mike $". | |
226 | */ |