~ubuntu-branches/ubuntu/intrepid/xserver-xgl/intrepid

« back to all changes in this revision

Viewing changes to os/strlcat.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2006-02-13 14:21:43 UTC
  • Revision ID: james.westby@ubuntu.com-20060213142143-mad6z9xzem7hzxz9
Tags: upstream-7.0.0
ImportĀ upstreamĀ versionĀ 7.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $OpenBSD: strlcat.c,v 1.10 2003/04/12 21:56:39 millert Exp $    */
 
2
 
 
3
/*
 
4
 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
 
5
 *
 
6
 * Permission to use, copy, modify, and distribute this software for any
 
7
 * purpose with or without fee is hereby granted, provided that the above
 
8
 * copyright notice and this permission notice appear in all copies.
 
9
 *
 
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
 
11
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 
12
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
 
13
 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 
15
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 
16
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
17
 */
 
18
/* $XFree86$ */
 
19
 
 
20
 
 
21
#ifdef HAVE_XORG_CONFIG_H
 
22
#include <xorg-config.h>
 
23
#endif
 
24
 
 
25
#include <sys/types.h>
 
26
#include <string.h>
 
27
 
 
28
/*
 
29
 * Appends src to string dst of size siz (unlike strncat, siz is the
 
30
 * full size of dst, not space left).  At most siz-1 characters
 
31
 * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
 
32
 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
 
33
 * If retval >= siz, truncation occurred.
 
34
 */
 
35
size_t
 
36
strlcat(char *dst, const char *src, size_t siz)
 
37
{
 
38
        register char *d = dst;
 
39
        register const char *s = src;
 
40
        register size_t n = siz;
 
41
        size_t dlen;
 
42
 
 
43
        /* Find the end of dst and adjust bytes left but don't go past end */
 
44
        while (n-- != 0 && *d != '\0')
 
45
                d++;
 
46
        dlen = d - dst;
 
47
        n = siz - dlen;
 
48
 
 
49
        if (n == 0)
 
50
                return(dlen + strlen(s));
 
51
        while (*s != '\0') {
 
52
                if (n != 1) {
 
53
                        *d++ = *s;
 
54
                        n--;
 
55
                }
 
56
                s++;
 
57
        }
 
58
        *d = '\0';
 
59
 
 
60
        return(dlen + (s - src));       /* count does not include NUL */
 
61
}