Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
strlcpy.cc File Reference
#include <stdio.h>
#include <string.h>
#include "strlcpy.h"

Go to the source code of this file.

Functions

size_t strlcpy (char *dst, const char *src, size_t size)
 
size_t strlcat (char *dst, const char *src, size_t size)
 

Function Documentation

◆ strlcpy()

size_t strlcpy ( char * dst,
const char * src,
size_t size )

Definition at line 39 of file strlcpy.cc.

40{
41 char *d = dst;
42 const char *s = src;
43 size_t n = size;
44
45 /* Copy as many bytes as will fit */
46 if (n != 0 && --n != 0) {
47 do {
48 if ((*d++ = *s++) == 0)
49 break;
50 } while (--n != 0);
51 }
52
53 /* Not enough room in dst, add NUL and traverse rest of src */
54 if (n == 0) {
55 if (size != 0)
56 *d = '\0'; /* NUL-terminate dst */
57 while (*s++);
58 }
59
60 return (s - src - 1); /* count does not include NUL */
61}
const int n
Definition JPolint.hh:791

◆ strlcat()

size_t strlcat ( char * dst,
const char * src,
size_t size )

Definition at line 72 of file strlcpy.cc.

73{
74 char *d = dst;
75 const char *s = src;
76 size_t n = size;
77 size_t dlen;
78
79 /* Find the end of dst and adjust bytes left but don't go past end */
80 while (n-- != 0 && *d != '\0')
81 d++;
82 dlen = d - dst;
83 n = size - dlen;
84
85 if (n == 0)
86 return (dlen + strlen(s));
87 while (*s != '\0') {
88 if (n != 1) {
89 *d++ = *s;
90 n--;
91 }
92 s++;
93 }
94 *d = '\0';
95
96 return (dlen + (s - src)); /* count does not include NUL */
97}