]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
75bd9771 | 2 | * "$Id: string.c 7460 2008-04-16 02:19:54Z mike $" |
ef416fc2 | 3 | * |
4 | * String functions for the Common UNIX Printing System (CUPS). | |
5 | * | |
426c6a59 | 6 | * Copyright 2007-2008 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
426c6a59 MS |
19 | * _cupsStrAlloc() - Allocate/reference a string. |
20 | * _cupsStrFlush() - Flush the string pool. | |
21 | * _cupsStrFormatd() - Format a floating-point number. | |
22 | * _cupsStrFree() - Free/dereference a string. | |
23 | * _cupsStrRetain() - Increment the reference count of a string. | |
24 | * _cupsStrScand() - Scan a string for a floating-point number. | |
25 | * _cupsStrStatistics() - Return allocation statistics for string pool. | |
26 | * _cups_strcpy() - Copy a string allowing for overlapping strings. | |
27 | * _cups_strdup() - Duplicate a string. | |
28 | * _cups_strcasecmp() - Do a case-insensitive comparison. | |
29 | * _cups_strncasecmp() - Do a case-insensitive comparison on up to N chars. | |
30 | * _cups_strlcat() - Safely concatenate two strings. | |
31 | * _cups_strlcpy() - Safely copy two strings. | |
32 | * compare_sp_items() - Compare two string pool items... | |
ef416fc2 | 33 | */ |
34 | ||
35 | /* | |
36 | * Include necessary headers... | |
37 | */ | |
38 | ||
4400e98d | 39 | #include <stdlib.h> |
40 | #include <limits.h> | |
d6ae789d | 41 | #include "array.h" |
4400e98d | 42 | #include "debug.h" |
ef416fc2 | 43 | #include "string.h" |
d6ae789d | 44 | #ifdef HAVE_PTHREAD_H |
45 | # include <pthread.h> | |
46 | #endif /* HAVE_PTHREAD_H */ | |
47 | ||
48 | ||
49 | /* | |
50 | * Local globals... | |
51 | */ | |
52 | ||
53 | #ifdef HAVE_PTHREAD_H | |
54 | static pthread_mutex_t sp_mutex = PTHREAD_MUTEX_INITIALIZER; | |
55 | /* Mutex to control access to pool */ | |
56 | #endif /* HAVE_PTHREAD_H */ | |
57 | static cups_array_t *stringpool = NULL; | |
58 | /* Global string pool */ | |
4400e98d | 59 | |
60 | ||
61 | /* | |
62 | * Local functions... | |
63 | */ | |
64 | ||
65 | static int compare_sp_items(_cups_sp_item_t *a, _cups_sp_item_t *b); | |
66 | ||
67 | ||
68 | /* | |
757d2cad | 69 | * '_cupsStrAlloc()' - Allocate/reference a string. |
4400e98d | 70 | */ |
71 | ||
72 | char * /* O - String pointer */ | |
757d2cad | 73 | _cupsStrAlloc(const char *s) /* I - String */ |
4400e98d | 74 | { |
4400e98d | 75 | _cups_sp_item_t *item, /* String pool item */ |
426c6a59 | 76 | *key; /* Search key */ |
4400e98d | 77 | |
78 | ||
79 | /* | |
80 | * Range check input... | |
81 | */ | |
82 | ||
83 | if (!s) | |
84 | return (NULL); | |
85 | ||
86 | /* | |
87 | * Get the string pool... | |
88 | */ | |
89 | ||
d6ae789d | 90 | #ifdef HAVE_PTHREAD_H |
91 | pthread_mutex_lock(&sp_mutex); | |
92 | #endif /* HAVE_PTHREAD_H */ | |
93 | ||
94 | if (!stringpool) | |
95 | stringpool = cupsArrayNew((cups_array_func_t)compare_sp_items, NULL); | |
4400e98d | 96 | |
d6ae789d | 97 | if (!stringpool) |
98 | { | |
99 | #ifdef HAVE_PTHREAD_H | |
100 | pthread_mutex_unlock(&sp_mutex); | |
101 | #endif /* HAVE_PTHREAD_H */ | |
4400e98d | 102 | |
4400e98d | 103 | return (NULL); |
d6ae789d | 104 | } |
4400e98d | 105 | |
106 | /* | |
107 | * See if the string is already in the pool... | |
108 | */ | |
109 | ||
426c6a59 | 110 | key = (_cups_sp_item_t *)(s - sizeof(unsigned int)); |
4400e98d | 111 | |
426c6a59 | 112 | if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL) |
4400e98d | 113 | { |
114 | /* | |
115 | * Found it, return the cached string... | |
116 | */ | |
117 | ||
118 | item->ref_count ++; | |
119 | ||
d6ae789d | 120 | #ifdef HAVE_PTHREAD_H |
121 | pthread_mutex_unlock(&sp_mutex); | |
122 | #endif /* HAVE_PTHREAD_H */ | |
123 | ||
4400e98d | 124 | return (item->str); |
125 | } | |
126 | ||
127 | /* | |
128 | * Not found, so allocate a new one... | |
129 | */ | |
130 | ||
426c6a59 | 131 | item = (_cups_sp_item_t *)calloc(1, sizeof(_cups_sp_item_t) + strlen(s)); |
4400e98d | 132 | if (!item) |
d6ae789d | 133 | { |
134 | #ifdef HAVE_PTHREAD_H | |
135 | pthread_mutex_unlock(&sp_mutex); | |
136 | #endif /* HAVE_PTHREAD_H */ | |
137 | ||
4400e98d | 138 | return (NULL); |
d6ae789d | 139 | } |
4400e98d | 140 | |
141 | item->ref_count = 1; | |
426c6a59 | 142 | strcpy(item->str, s); |
4400e98d | 143 | |
144 | /* | |
145 | * Add the string to the pool and return it... | |
146 | */ | |
147 | ||
d6ae789d | 148 | cupsArrayAdd(stringpool, item); |
149 | ||
150 | #ifdef HAVE_PTHREAD_H | |
151 | pthread_mutex_unlock(&sp_mutex); | |
152 | #endif /* HAVE_PTHREAD_H */ | |
4400e98d | 153 | |
154 | return (item->str); | |
155 | } | |
156 | ||
157 | ||
158 | /* | |
426c6a59 | 159 | * '_cupsStrFlush()' - Flush the string pool. |
4400e98d | 160 | */ |
161 | ||
162 | void | |
d6ae789d | 163 | _cupsStrFlush(void) |
4400e98d | 164 | { |
165 | _cups_sp_item_t *item; /* Current item */ | |
166 | ||
167 | ||
ae71f5de MS |
168 | DEBUG_printf(("_cupsStrFlush: %d strings in array\n", |
169 | cupsArrayCount(stringpool))); | |
d6ae789d | 170 | |
171 | #ifdef HAVE_PTHREAD_H | |
172 | pthread_mutex_lock(&sp_mutex); | |
173 | #endif /* HAVE_PTHREAD_H */ | |
174 | ||
175 | for (item = (_cups_sp_item_t *)cupsArrayFirst(stringpool); | |
4400e98d | 176 | item; |
d6ae789d | 177 | item = (_cups_sp_item_t *)cupsArrayNext(stringpool)) |
4400e98d | 178 | free(item); |
4400e98d | 179 | |
d6ae789d | 180 | cupsArrayDelete(stringpool); |
181 | stringpool = NULL; | |
182 | ||
183 | #ifdef HAVE_PTHREAD_H | |
184 | pthread_mutex_unlock(&sp_mutex); | |
185 | #endif /* HAVE_PTHREAD_H */ | |
4400e98d | 186 | } |
187 | ||
188 | ||
189 | /* | |
757d2cad | 190 | * '_cupsStrFormatd()' - Format a floating-point number. |
191 | */ | |
192 | ||
193 | char * /* O - Pointer to end of string */ | |
194 | _cupsStrFormatd(char *buf, /* I - String */ | |
195 | char *bufend, /* I - End of string buffer */ | |
196 | double number, /* I - Number to format */ | |
197 | struct lconv *loc) /* I - Locale data */ | |
198 | { | |
199 | char *bufptr, /* Pointer into buffer */ | |
200 | temp[1024], /* Temporary string */ | |
201 | *tempdec, /* Pointer to decimal point */ | |
202 | *tempptr; /* Pointer into temporary string */ | |
203 | const char *dec; /* Decimal point */ | |
204 | int declen; /* Length of decimal point */ | |
205 | ||
206 | ||
207 | /* | |
208 | * Format the number using the "%.12f" format and then eliminate | |
209 | * unnecessary trailing 0's. | |
210 | */ | |
211 | ||
212 | snprintf(temp, sizeof(temp), "%.12f", number); | |
213 | for (tempptr = temp + strlen(temp) - 1; | |
214 | tempptr > temp && *tempptr == '0'; | |
215 | *tempptr-- = '\0'); | |
216 | ||
217 | /* | |
218 | * Next, find the decimal point... | |
219 | */ | |
220 | ||
221 | if (loc && loc->decimal_point) | |
222 | { | |
223 | dec = loc->decimal_point; | |
b86bc4cf | 224 | declen = (int)strlen(dec); |
757d2cad | 225 | } |
226 | else | |
227 | { | |
228 | dec = "."; | |
229 | declen = 1; | |
230 | } | |
231 | ||
232 | if (declen == 1) | |
233 | tempdec = strchr(temp, *dec); | |
234 | else | |
235 | tempdec = strstr(temp, dec); | |
236 | ||
237 | /* | |
238 | * Copy everything up to the decimal point... | |
239 | */ | |
240 | ||
241 | if (tempdec) | |
242 | { | |
243 | for (tempptr = temp, bufptr = buf; | |
244 | tempptr < tempdec && bufptr < bufend; | |
245 | *bufptr++ = *tempptr++); | |
246 | ||
7594b224 | 247 | tempptr += declen; |
757d2cad | 248 | |
7594b224 | 249 | if (*tempptr && bufptr < bufend) |
757d2cad | 250 | { |
251 | *bufptr++ = '.'; | |
252 | ||
253 | while (*tempptr && bufptr < bufend) | |
254 | *bufptr++ = *tempptr++; | |
255 | } | |
256 | ||
257 | *bufptr = '\0'; | |
258 | } | |
259 | else | |
260 | { | |
261 | strlcpy(buf, temp, bufend - buf + 1); | |
262 | bufptr = buf + strlen(buf); | |
263 | } | |
264 | ||
265 | return (bufptr); | |
266 | } | |
267 | ||
268 | ||
269 | /* | |
270 | * '_cupsStrFree()' - Free/dereference a string. | |
4400e98d | 271 | */ |
272 | ||
273 | void | |
757d2cad | 274 | _cupsStrFree(const char *s) /* I - String to free */ |
4400e98d | 275 | { |
4400e98d | 276 | _cups_sp_item_t *item, /* String pool item */ |
426c6a59 | 277 | *key; /* Search key */ |
4400e98d | 278 | |
279 | ||
280 | /* | |
281 | * Range check input... | |
282 | */ | |
283 | ||
284 | if (!s) | |
285 | return; | |
286 | ||
287 | /* | |
d6ae789d | 288 | * Check the string pool... |
289 | * | |
290 | * We don't need to lock the mutex yet, as we only want to know if | |
291 | * the stringpool is initialized. The rest of the code will still | |
292 | * work if it is initialized before we lock... | |
4400e98d | 293 | */ |
294 | ||
d6ae789d | 295 | if (!stringpool) |
4400e98d | 296 | return; |
297 | ||
298 | /* | |
299 | * See if the string is already in the pool... | |
300 | */ | |
301 | ||
d6ae789d | 302 | #ifdef HAVE_PTHREAD_H |
303 | pthread_mutex_lock(&sp_mutex); | |
304 | #endif /* HAVE_PTHREAD_H */ | |
305 | ||
426c6a59 | 306 | key = (_cups_sp_item_t *)(s - sizeof(unsigned int)); |
4400e98d | 307 | |
426c6a59 MS |
308 | if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL && |
309 | item == key) | |
4400e98d | 310 | { |
311 | /* | |
312 | * Found it, dereference... | |
313 | */ | |
314 | ||
315 | item->ref_count --; | |
316 | ||
317 | if (!item->ref_count) | |
318 | { | |
319 | /* | |
320 | * Remove and free... | |
321 | */ | |
322 | ||
d6ae789d | 323 | cupsArrayRemove(stringpool, item); |
4400e98d | 324 | |
4400e98d | 325 | free(item); |
326 | } | |
327 | } | |
d6ae789d | 328 | |
329 | #ifdef HAVE_PTHREAD_H | |
330 | pthread_mutex_unlock(&sp_mutex); | |
331 | #endif /* HAVE_PTHREAD_H */ | |
4400e98d | 332 | } |
333 | ||
334 | ||
426c6a59 MS |
335 | /* |
336 | * '_cupsStrRetain()' - Increment the reference count of a string. | |
337 | * | |
338 | * Note: This function does not verify that the passed pointer is in the | |
339 | * string pool, so any calls to it MUST know they are passing in a | |
340 | * good pointer. | |
341 | */ | |
342 | ||
343 | char * /* O - Pointer to string */ | |
344 | _cupsStrRetain(char *s) /* I - String to retain */ | |
345 | { | |
346 | _cups_sp_item_t *item; /* Pointer to string pool item */ | |
347 | ||
348 | ||
349 | if (s) | |
350 | { | |
351 | item = (_cups_sp_item_t *)(s - sizeof(unsigned int)); | |
352 | ||
353 | #ifdef HAVE_PTHREAD_H | |
354 | pthread_mutex_lock(&sp_mutex); | |
355 | #endif /* HAVE_PTHREAD_H */ | |
356 | ||
357 | item->ref_count ++; | |
358 | ||
359 | #ifdef HAVE_PTHREAD_H | |
360 | pthread_mutex_unlock(&sp_mutex); | |
361 | #endif /* HAVE_PTHREAD_H */ | |
362 | } | |
363 | ||
364 | return (s); | |
365 | } | |
366 | ||
367 | ||
4400e98d | 368 | /* |
757d2cad | 369 | * '_cupsStrScand()' - Scan a string for a floating-point number. |
370 | * | |
371 | * This function handles the locale-specific BS so that a decimal | |
372 | * point is always the period (".")... | |
373 | */ | |
374 | ||
375 | double /* O - Number */ | |
376 | _cupsStrScand(const char *buf, /* I - Pointer to number */ | |
377 | char **bufptr, /* O - New pointer or NULL on error */ | |
378 | struct lconv *loc) /* I - Locale data */ | |
379 | { | |
380 | char temp[1024], /* Temporary buffer */ | |
381 | *tempptr; /* Pointer into temporary buffer */ | |
382 | ||
383 | ||
384 | /* | |
385 | * Range check input... | |
386 | */ | |
387 | ||
388 | if (!buf) | |
389 | return (0.0); | |
390 | ||
391 | /* | |
392 | * Skip leading whitespace... | |
393 | */ | |
394 | ||
395 | while (isspace(*buf & 255)) | |
396 | buf ++; | |
397 | ||
398 | /* | |
399 | * Copy leading sign, numbers, period, and then numbers... | |
400 | */ | |
401 | ||
402 | tempptr = temp; | |
403 | if (*buf == '-' || *buf == '+') | |
404 | *tempptr++ = *buf++; | |
405 | ||
406 | while (isdigit(*buf & 255)) | |
407 | if (tempptr < (temp + sizeof(temp) - 1)) | |
408 | *tempptr++ = *buf++; | |
409 | else | |
410 | { | |
411 | if (bufptr) | |
412 | *bufptr = NULL; | |
413 | ||
414 | return (0.0); | |
415 | } | |
416 | ||
417 | if (*buf == '.') | |
418 | { | |
e1d6a774 | 419 | /* |
420 | * Read fractional portion of number... | |
421 | */ | |
422 | ||
423 | buf ++; | |
424 | ||
757d2cad | 425 | if (loc && loc->decimal_point) |
426 | { | |
427 | strlcpy(tempptr, loc->decimal_point, sizeof(temp) - (tempptr - temp)); | |
428 | tempptr += strlen(tempptr); | |
429 | } | |
430 | else if (tempptr < (temp + sizeof(temp) - 1)) | |
431 | *tempptr++ = '.'; | |
432 | else | |
433 | { | |
434 | if (bufptr) | |
435 | *bufptr = NULL; | |
436 | ||
437 | return (0.0); | |
438 | } | |
439 | ||
440 | while (isdigit(*buf & 255)) | |
441 | if (tempptr < (temp + sizeof(temp) - 1)) | |
442 | *tempptr++ = *buf++; | |
443 | else | |
444 | { | |
445 | if (bufptr) | |
446 | *bufptr = NULL; | |
447 | ||
448 | return (0.0); | |
449 | } | |
450 | } | |
451 | ||
b86bc4cf | 452 | if (*buf == 'e' || *buf == 'E') |
453 | { | |
454 | /* | |
455 | * Read exponent... | |
456 | */ | |
457 | ||
458 | if (tempptr < (temp + sizeof(temp) - 1)) | |
459 | *tempptr++ = *buf++; | |
460 | else | |
461 | { | |
462 | if (bufptr) | |
463 | *bufptr = NULL; | |
464 | ||
465 | return (0.0); | |
466 | } | |
467 | ||
468 | if (*buf == '+' || *buf == '-') | |
469 | { | |
470 | if (tempptr < (temp + sizeof(temp) - 1)) | |
471 | *tempptr++ = *buf++; | |
472 | else | |
473 | { | |
474 | if (bufptr) | |
475 | *bufptr = NULL; | |
476 | ||
477 | return (0.0); | |
478 | } | |
479 | } | |
480 | ||
481 | while (isdigit(*buf & 255)) | |
482 | if (tempptr < (temp + sizeof(temp) - 1)) | |
483 | *tempptr++ = *buf++; | |
484 | else | |
485 | { | |
486 | if (bufptr) | |
487 | *bufptr = NULL; | |
488 | ||
489 | return (0.0); | |
490 | } | |
491 | } | |
492 | ||
757d2cad | 493 | /* |
494 | * Nul-terminate the temporary string and return the value... | |
495 | */ | |
496 | ||
497 | if (bufptr) | |
498 | *bufptr = (char *)buf; | |
499 | ||
500 | *tempptr = '\0'; | |
501 | ||
502 | return (strtod(temp, NULL)); | |
503 | } | |
504 | ||
505 | ||
506 | /* | |
507 | * '_cupsStrStatistics()' - Return allocation statistics for string pool. | |
4400e98d | 508 | */ |
509 | ||
510 | size_t /* O - Number of strings */ | |
757d2cad | 511 | _cupsStrStatistics(size_t *alloc_bytes, /* O - Allocated bytes */ |
512 | size_t *total_bytes) /* O - Total string bytes */ | |
4400e98d | 513 | { |
514 | size_t count, /* Number of strings */ | |
515 | abytes, /* Allocated string bytes */ | |
516 | tbytes, /* Total string bytes */ | |
517 | len; /* Length of string */ | |
518 | _cups_sp_item_t *item; /* Current item */ | |
4400e98d | 519 | |
520 | ||
521 | /* | |
522 | * Loop through strings in pool, counting everything up... | |
523 | */ | |
524 | ||
d6ae789d | 525 | #ifdef HAVE_PTHREAD_H |
526 | pthread_mutex_lock(&sp_mutex); | |
527 | #endif /* HAVE_PTHREAD_H */ | |
4400e98d | 528 | |
529 | for (count = 0, abytes = 0, tbytes = 0, | |
d6ae789d | 530 | item = (_cups_sp_item_t *)cupsArrayFirst(stringpool); |
4400e98d | 531 | item; |
d6ae789d | 532 | item = (_cups_sp_item_t *)cupsArrayNext(stringpool)) |
4400e98d | 533 | { |
534 | /* | |
535 | * Count allocated memory, using a 64-bit aligned buffer as a basis. | |
536 | */ | |
537 | ||
538 | count += item->ref_count; | |
539 | len = (strlen(item->str) + 8) & ~7; | |
540 | abytes += sizeof(_cups_sp_item_t) + len; | |
541 | tbytes += item->ref_count * len; | |
542 | } | |
543 | ||
d6ae789d | 544 | #ifdef HAVE_PTHREAD_H |
545 | pthread_mutex_unlock(&sp_mutex); | |
546 | #endif /* HAVE_PTHREAD_H */ | |
547 | ||
4400e98d | 548 | /* |
549 | * Return values... | |
550 | */ | |
551 | ||
552 | if (alloc_bytes) | |
553 | *alloc_bytes = abytes; | |
554 | ||
555 | if (total_bytes) | |
556 | *total_bytes = tbytes; | |
557 | ||
558 | return (count); | |
559 | } | |
ef416fc2 | 560 | |
561 | ||
562 | /* | |
563 | * '_cups_strcpy()' - Copy a string allowing for overlapping strings. | |
564 | */ | |
565 | ||
566 | void | |
567 | _cups_strcpy(char *dst, /* I - Destination string */ | |
568 | const char *src) /* I - Source string */ | |
569 | { | |
570 | while (*src) | |
571 | *dst++ = *src++; | |
572 | ||
573 | *dst = '\0'; | |
574 | } | |
575 | ||
576 | ||
577 | /* | |
578 | * '_cups_strdup()' - Duplicate a string. | |
579 | */ | |
580 | ||
581 | #ifndef HAVE_STRDUP | |
582 | char * /* O - New string pointer */ | |
583 | _cups_strdup(const char *s) /* I - String to duplicate */ | |
584 | { | |
585 | char *t; /* New string pointer */ | |
586 | ||
587 | ||
588 | if (s == NULL) | |
589 | return (NULL); | |
590 | ||
591 | if ((t = malloc(strlen(s) + 1)) == NULL) | |
592 | return (NULL); | |
593 | ||
594 | return (strcpy(t, s)); | |
595 | } | |
596 | #endif /* !HAVE_STRDUP */ | |
597 | ||
598 | ||
599 | /* | |
600 | * '_cups_strcasecmp()' - Do a case-insensitive comparison. | |
601 | */ | |
602 | ||
603 | #ifndef HAVE_STRCASECMP | |
604 | int /* O - Result of comparison (-1, 0, or 1) */ | |
605 | _cups_strcasecmp(const char *s, /* I - First string */ | |
606 | const char *t) /* I - Second string */ | |
607 | { | |
608 | while (*s != '\0' && *t != '\0') | |
609 | { | |
610 | if (tolower(*s & 255) < tolower(*t & 255)) | |
611 | return (-1); | |
612 | else if (tolower(*s & 255) > tolower(*t & 255)) | |
613 | return (1); | |
614 | ||
615 | s ++; | |
616 | t ++; | |
617 | } | |
618 | ||
619 | if (*s == '\0' && *t == '\0') | |
620 | return (0); | |
621 | else if (*s != '\0') | |
622 | return (1); | |
623 | else | |
624 | return (-1); | |
625 | } | |
626 | #endif /* !HAVE_STRCASECMP */ | |
627 | ||
628 | /* | |
629 | * '_cups_strncasecmp()' - Do a case-insensitive comparison on up to N chars. | |
630 | */ | |
631 | ||
632 | #ifndef HAVE_STRNCASECMP | |
633 | int /* O - Result of comparison (-1, 0, or 1) */ | |
634 | _cups_strncasecmp(const char *s, /* I - First string */ | |
0a682745 | 635 | const char *t, /* I - Second string */ |
ef416fc2 | 636 | size_t n) /* I - Maximum number of characters to compare */ |
637 | { | |
638 | while (*s != '\0' && *t != '\0' && n > 0) | |
639 | { | |
640 | if (tolower(*s & 255) < tolower(*t & 255)) | |
641 | return (-1); | |
642 | else if (tolower(*s & 255) > tolower(*t & 255)) | |
643 | return (1); | |
644 | ||
645 | s ++; | |
646 | t ++; | |
647 | n --; | |
648 | } | |
649 | ||
650 | if (n == 0) | |
651 | return (0); | |
652 | else if (*s == '\0' && *t == '\0') | |
653 | return (0); | |
654 | else if (*s != '\0') | |
655 | return (1); | |
656 | else | |
657 | return (-1); | |
658 | } | |
659 | #endif /* !HAVE_STRNCASECMP */ | |
660 | ||
661 | ||
662 | #ifndef HAVE_STRLCAT | |
663 | /* | |
664 | * '_cups_strlcat()' - Safely concatenate two strings. | |
665 | */ | |
666 | ||
667 | size_t /* O - Length of string */ | |
668 | _cups_strlcat(char *dst, /* O - Destination string */ | |
669 | const char *src, /* I - Source string */ | |
670 | size_t size) /* I - Size of destination string buffer */ | |
671 | { | |
672 | size_t srclen; /* Length of source string */ | |
673 | size_t dstlen; /* Length of destination string */ | |
674 | ||
675 | ||
676 | /* | |
677 | * Figure out how much room is left... | |
678 | */ | |
679 | ||
680 | dstlen = strlen(dst); | |
681 | size -= dstlen + 1; | |
682 | ||
683 | if (!size) | |
684 | return (dstlen); /* No room, return immediately... */ | |
685 | ||
686 | /* | |
687 | * Figure out how much room is needed... | |
688 | */ | |
689 | ||
690 | srclen = strlen(src); | |
691 | ||
692 | /* | |
693 | * Copy the appropriate amount... | |
694 | */ | |
695 | ||
696 | if (srclen > size) | |
697 | srclen = size; | |
698 | ||
699 | memcpy(dst + dstlen, src, srclen); | |
700 | dst[dstlen + srclen] = '\0'; | |
701 | ||
702 | return (dstlen + srclen); | |
703 | } | |
704 | #endif /* !HAVE_STRLCAT */ | |
705 | ||
706 | ||
707 | #ifndef HAVE_STRLCPY | |
708 | /* | |
709 | * '_cups_strlcpy()' - Safely copy two strings. | |
710 | */ | |
711 | ||
712 | size_t /* O - Length of string */ | |
713 | _cups_strlcpy(char *dst, /* O - Destination string */ | |
714 | const char *src, /* I - Source string */ | |
715 | size_t size) /* I - Size of destination string buffer */ | |
716 | { | |
717 | size_t srclen; /* Length of source string */ | |
718 | ||
719 | ||
720 | /* | |
721 | * Figure out how much room is needed... | |
722 | */ | |
723 | ||
724 | size --; | |
725 | ||
726 | srclen = strlen(src); | |
727 | ||
728 | /* | |
729 | * Copy the appropriate amount... | |
730 | */ | |
731 | ||
732 | if (srclen > size) | |
733 | srclen = size; | |
734 | ||
735 | memcpy(dst, src, srclen); | |
736 | dst[srclen] = '\0'; | |
737 | ||
738 | return (srclen); | |
739 | } | |
740 | #endif /* !HAVE_STRLCPY */ | |
741 | ||
742 | ||
743 | /* | |
4400e98d | 744 | * 'compare_sp_items()' - Compare two string pool items... |
745 | */ | |
746 | ||
747 | static int /* O - Result of comparison */ | |
748 | compare_sp_items(_cups_sp_item_t *a, /* I - First item */ | |
749 | _cups_sp_item_t *b) /* I - Second item */ | |
750 | { | |
751 | return (strcmp(a->str, b->str)); | |
752 | } | |
753 | ||
754 | ||
755 | /* | |
75bd9771 | 756 | * End of "$Id: string.c 7460 2008-04-16 02:19:54Z mike $". |
ef416fc2 | 757 | */ |