├── .gitignore ├── .travis.yml ├── Readme.md ├── Makefile ├── clib.json ├── package.json ├── test.c ├── History.md ├── strdup.h ├── strdup.c └── COPYING /.gitignore: -------------------------------------------------------------------------------- 1 | strdup.o 2 | test.o 3 | test 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | script: make check 3 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # strdup 3 | 4 | Drop-in replacement for [`strdup(3)` from libc](http://www.unix.com/man-page/freebsd/3/strdup/). 5 | 6 | ## License 7 | 8 | Expat License ("MIT License") 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | CFLAGS = -std=c99 -Wall -Wextra 3 | 4 | check: test 5 | 6 | test: strdup.o test.o 7 | $(CC) $^ -o $@ 8 | ./test 9 | 10 | %.o: %.c 11 | $(CC) $< -c -o $@ $(CFLAGS) 12 | 13 | clean: 14 | rm -f test strdup.o test.o 15 | 16 | .PHONY: check clean 17 | -------------------------------------------------------------------------------- /clib.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "strdup", 3 | "version": "0.1.5", 4 | "repo": "clibs/strdup", 5 | "description": "drop-in replacement for strdup(3) from libc", 6 | "keywords": [ "string", "copy" ], 7 | "license": "Expat", 8 | "src": [ 9 | "strdup.c", 10 | "strdup.h" 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "strdup", 3 | "version": "0.1.5", 4 | "repo": "clibs/strdup", 5 | "description": "drop-in replacement for strdup(3) from libc", 6 | "keywords": [ 7 | "string", 8 | "copy" 9 | ], 10 | "license": "Expat", 11 | "src": [ 12 | "strdup.c", 13 | "strdup.h" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "strdup.h" 5 | 6 | int 7 | main(void) { 8 | char string[] = "hello world"; 9 | char *copy = strdup(string); 10 | assert(copy); 11 | assert(0 == strcmp("hello world", copy)); 12 | assert(11 == strlen(copy)); 13 | free(copy); 14 | 15 | assert(0 == strdup(0)); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.0.2 / 2015-12-15 3 | ================== 4 | 5 | * Fix licensing issue (#3, @maandree) 6 | 7 | 0.0.1 / 2015-04-01 8 | ================== 9 | 10 | * test: Add a NULL check 11 | * ignore compiled files 12 | * strdup: included condition to test if source is `NULL` before duplication (#1, @alexpreynolds) 13 | 14 | 0.0.0 / 2014-06-29 15 | ================== 16 | 17 | * Add travis 18 | * rename: Readmd.md -> Readme.md 19 | * Add package.json 20 | * dox 21 | * Initial commit 22 | -------------------------------------------------------------------------------- /strdup.h: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // strdup.h 4 | // 5 | // Copyright (c) 2014 Stephen Mathieson 6 | // MIT licensed 7 | // 8 | 9 | #ifndef HAVE_STRDUP 10 | #define HAVE_STRDUP 11 | 12 | /** 13 | * Drop-in replacement for strdup(3) from libc. 14 | * 15 | * Creates a copy of `str`. Free when done. 16 | * 17 | * Returns a pointer to the newly allocated 18 | * copy of `str`, or `NULL` on failure. 19 | */ 20 | 21 | #ifndef strdup 22 | char * 23 | strdup(const char *str); 24 | #endif 25 | 26 | #endif /* HAVE_STRDUP */ 27 | -------------------------------------------------------------------------------- /strdup.c: -------------------------------------------------------------------------------- 1 | 2 | // 3 | // strdup.c 4 | // 5 | // Copyright (c) 2014 Stephen Mathieson 6 | // MIT licensed 7 | // 8 | 9 | #ifndef HAVE_STRDUP 10 | 11 | #include 12 | #include 13 | #include "strdup.h" 14 | 15 | #ifndef strdup 16 | 17 | char * 18 | strdup(const char *str) { 19 | if (NULL == (char *) str) { 20 | return NULL; 21 | } 22 | 23 | int len = strlen(str) + 1; 24 | char *buf = malloc(len); 25 | 26 | if (buf) { 27 | memset(buf, 0, len); 28 | memcpy(buf, str, len - 1); 29 | } 30 | return buf; 31 | } 32 | 33 | #endif 34 | 35 | #endif /* HAVE_STRDUP */ 36 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, 2015 Stephen Mathieson 2 | 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without 7 | restriction, including without limitation the rights to use, 8 | copy, modify, merge, publish, distribute, sublicense, and/or 9 | sell copies of the Software, and to permit persons to whom 10 | the Software is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice shall 14 | be included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 17 | KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 18 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 19 | AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | --------------------------------------------------------------------------------