sourcepawn/compiler/lstring.c
author Ruben Gonzalez <drifter01620@gmail.com>
Thu May 22 09:49:27 2014 -0400 (2014-05-22)
changeset 4378 568f4497ef4a
parent 27 764c6dd3dc25
permissions -rw-r--r--
Update CS:GO team score gamedata.
     1 /* Safe string copying and concatenation
     2  * These routines are originally distributed in two separate files. I have
     3  * copied the files verbatim in this single source file, including all comments.
     4  * The only change is that the second set of include files is commented out
     5  * (there is no need to include the same files twice).
     6  */
     7 
     8 #include "lstring.h"
     9 
    10 #if !defined HAVE_SAFESTR
    11 
    12 /*	$OpenBSD: strlcpy.c,v 1.10 2005/08/08 08:05:37 espie Exp $	*/
    13 
    14 /*
    15  * Copyright (c) 1998 Todd C. Miller <[email protected]>
    16  *
    17  * Permission to use, copy, modify, and distribute this software for any
    18  * purpose with or without fee is hereby granted, provided that the above
    19  * copyright notice and this permission notice appear in all copies.
    20  *
    21  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    22  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    23  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    24  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    25  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    26  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    27  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    28  */
    29 
    30 /*
    31   #include <sys/types.h>  already included through lstring.h
    32  */
    33 #include <string.h>   /* for strlen() */
    34 
    35 /*
    36  * Copy src to string dst of size siz.  At most siz-1 characters
    37  * will be copied.  Always NUL terminates (unless siz == 0).
    38  * Returns strlen(src); if retval >= siz, truncation occurred.
    39  */
    40 size_t
    41 strlcpy(char *dst, const char *src, size_t siz)
    42 {
    43 	char *d = dst;
    44 	const char *s = src;
    45 	size_t n = siz;
    46 
    47 	/* Copy as many bytes as will fit */
    48 	if (n != 0 && --n != 0) {
    49 		do {
    50 			if ((*d++ = *s++) == 0)
    51 				break;
    52 		} while (--n != 0);
    53 	}
    54 
    55 	/* Not enough room in dst, add NUL and traverse rest of src */
    56 	if (n == 0) {
    57 		if (siz != 0)
    58 			*d = '\0';		/* NUL-terminate dst */
    59 		while (*s++)
    60 			;
    61 	}
    62 
    63 	return(s - src - 1);	/* count does not include NUL */
    64 }
    65 
    66 /*	$OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $	*/
    67 
    68 /*
    69  * Copyright (c) 1998 Todd C. Miller <[email protected]>
    70  *
    71  * Permission to use, copy, modify, and distribute this software for any
    72  * purpose with or without fee is hereby granted, provided that the above
    73  * copyright notice and this permission notice appear in all copies.
    74  *
    75  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    76  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    77  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
    78  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    79  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    80  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
    81  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    82  */
    83 
    84 /*
    85   #include <sys/types.h>    already included
    86   #include <string.h>       already included
    87 */
    88 
    89 /*
    90  * Appends src to string dst of size siz (unlike strncat, siz is the
    91  * full size of dst, not space left).  At most siz-1 characters
    92  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
    93  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
    94  * If retval >= siz, truncation occurred.
    95  */
    96 size_t
    97 strlcat(char *dst, const char *src, size_t siz)
    98 {
    99 	char *d = dst;
   100 	const char *s = src;
   101 	size_t n = siz;
   102 	size_t dlen;
   103 
   104 	/* Find the end of dst and adjust bytes left but don't go past end */
   105 	while (n-- != 0 && *d != '\0')
   106 		d++;
   107 	dlen = d - dst;
   108 	n = siz - dlen;
   109 
   110 	if (n == 0)
   111 		return(dlen + strlen(s));
   112 	while (*s != '\0') {
   113 		if (n != 1) {
   114 			*d++ = *s;
   115 			n--;
   116 		}
   117 		s++;
   118 	}
   119 	*d = '\0';
   120 
   121 	return(dlen + (s - src));	/* count does not include NUL */
   122 }
   123 
   124 #endif /* #if !defined HAVE_SAFESTR */