src/compat.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 26 Feb 2010 01:42:46 +0100
changeset 49 59b09b0aeb96
parent 13 d3554afaa768
permissions -rw-r--r--
safe_strncpy replaces the insecure strncpy function strncpy copies up to n bytes and does not terminate it, except the null byte is in the first n bytes... committer: Markus Bröker <mbroeker@largo.homelinux.org>

/**
 *  $Id: config.c 171 2008-08-10 18:20:39Z mbroeker $
 * $URL: http://localhost/svn/c/mcbot/trunk/src/compat.c $
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <compat.h>

char *compat_strdup (const char *s)
{
    char *buf;

    buf = malloc (strlen (s) + 1);
    if (buf != NULL)
        strcpy (buf, s);

    return buf;
}

char *safe_strncpy (char *dest, const char *src, size_t size)
{
    size_t i;

    for (i = 0; i < size - 1; i++) {
        if (src[i] == '\0')
            break;
        dest[i] = src[i];
    }

    dest[i] = '\0';

    return dest;
}