File: strdup.c

package info (click to toggle)
coda 2.20-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 11,292 kB
  • sloc: ansic: 124,817; sh: 4,657; java: 2,391; python: 1,091; yacc: 1,003; makefile: 596; lex: 204; fortran: 60; xml: 5
file content (27 lines) | stat: -rw-r--r-- 419 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

char *strdup(const char *str)
{
    char *new_str;
    size_t length;

    if (str == NULL)
    {
        return NULL;
    }
    length = strlen(str);
    new_str = malloc(length + 1);
    if (new_str == NULL)
    {
        return NULL;
    }
    memcpy(new_str, str, length);
    new_str[length] = '\0';

    return new_str;
}