Mercurial > code
changeset 677:e12fc88c11bc
strdup: provided by POSIX
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 24 Mar 2022 07:33:26 +0100 |
parents | a84f1baeb9e7 |
children | 4d90d8d138c3 |
files | c/strdup/strdup.c c/strdup/strdup.h c/strndup/strndup.c c/strndup/strndup.h |
diffstat | 4 files changed, 0 insertions(+), 137 deletions(-) [+] |
line wrap: on
line diff
--- a/c/strdup/strdup.c Thu Mar 24 07:32:50 2022 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -/* - * strdup.c -- duplicate a string - * - * Copyright (c) 2011-2019 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -char * -strdup(const char *src) -{ - char *dst; - size_t len; - - len = strlen(src); - - if ((dst = calloc(len + 1, sizeof (char))) == NULL) - return NULL; - - return strcpy(dst, src); -}
--- a/c/strdup/strdup.h Thu Mar 24 07:32:50 2022 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -/* - * strdup.h -- duplicate a string - * - * Copyright (c) 2011-2019 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef STRDUP_H -#define STRDUP_H - -#ifdef __cplusplus -extern "C" { -#endif - -char * -strdup(const char *); - -#ifdef __cplusplus -} -#endif - -#endif /* !STRDUP_H */
--- a/c/strndup/strndup.c Thu Mar 24 07:32:50 2022 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,36 +0,0 @@ -/* - * strndup.c -- duplicate a string - * - * Copyright (c) 2011-2019 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -char * -strndup(const char *src, size_t len) -{ - size_t tocopy; - char *dst; - - for (tocopy = 0; tocopy < len && src[tocopy] != '\0'; ++tocopy) - continue; - - if ((dst = calloc(tocopy + 1, 1)) != NULL) - strncpy(dst, src, tocopy); - - return dst; -}
--- a/c/strndup/strndup.h Thu Mar 24 07:32:50 2022 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -/* - * strndup.h -- duplicate a string - * - * Copyright (c) 2011-2019 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef STRNDUP_H -#define STRNDUP_H - -#ifdef __cplusplus -extern "C" { -#endif - -char * -strndup(const char *, size_t); - -#ifdef __cplusplus -} -#endif - -#endif /* !STRNDUP_H */