source: drsdaq/DRS/strlcpy.c@ 8837

Last change on this file since 8837 was 22, checked in by ogrimm, 16 years ago
First commit of drsdaq program
File size: 2.1 KB
Line 
1/********************************************************************\
2
3 Name: strlcpy.c
4 Created by: Stefan Ritt
5
6 Contents: Contains strlcpy and strlcat which are versions of
7 strcpy and strcat, but which avoid buffer overflows
8
9 $Id: strlcpy.c 16 2005-10-07 13:05:38Z ritt $
10
11\********************************************************************/
12
13#include <stdio.h>
14#include <string.h>
15#include "strlcpy.h"
16
17/*
18* Copy src to string dst of size siz. At most siz-1 characters
19* will be copied. Always NUL terminates (unless size == 0).
20* Returns strlen(src); if retval >= siz, truncation occurred.
21*/
22size_t strlcpy(char *dst, const char *src, size_t size)
23{
24 char *d = dst;
25 const char *s = src;
26 size_t n = size;
27
28 /* Copy as many bytes as will fit */
29 if (n != 0 && --n != 0) {
30 do {
31 if ((*d++ = *s++) == 0)
32 break;
33 } while (--n != 0);
34 }
35
36 /* Not enough room in dst, add NUL and traverse rest of src */
37 if (n == 0) {
38 if (size != 0)
39 *d = '\0'; /* NUL-terminate dst */
40 while (*s++);
41 }
42
43 return (s - src - 1); /* count does not include NUL */
44}
45
46/*-------------------------------------------------------------------*/
47
48/*
49* Appends src to string dst of size siz (unlike strncat, siz is the
50* full size of dst, not space left). At most siz-1 characters
51* will be copied. Always NUL terminates (unless size <= strlen(dst)).
52* Returns strlen(src) + MIN(size, strlen(initial dst)).
53* If retval >= size, truncation occurred.
54*/
55size_t strlcat(char *dst, const char *src, size_t size)
56{
57 char *d = dst;
58 const char *s = src;
59 size_t n = size;
60 size_t dlen;
61
62 /* Find the end of dst and adjust bytes left but don't go past end */
63 while (n-- != 0 && *d != '\0')
64 d++;
65 dlen = d - dst;
66 n = size - dlen;
67
68 if (n == 0)
69 return (dlen + strlen(s));
70 while (*s != '\0') {
71 if (n != 1) {
72 *d++ = *s;
73 n--;
74 }
75 s++;
76 }
77 *d = '\0';
78
79 return (dlen + (s - src)); /* count does not include NUL */
80}
81
82/*-------------------------------------------------------------------*/
Note: See TracBrowser for help on using the repository browser.