diff options
Diffstat (limited to 'examples/hello_strlcpy')
-rw-r--r-- | examples/hello_strlcpy/Makefile | 6 | ||||
-rw-r--r-- | examples/hello_strlcpy/expect.out | 23 | ||||
-rw-r--r-- | examples/hello_strlcpy/getline.c | 49 | ||||
-rw-r--r-- | examples/hello_strlcpy/hello.c | 12 | ||||
-rw-r--r-- | examples/hello_strlcpy/test.mk | 27 |
5 files changed, 97 insertions, 20 deletions
diff --git a/examples/hello_strlcpy/Makefile b/examples/hello_strlcpy/Makefile index 1ac88ee..f24ed5a 100644 --- a/examples/hello_strlcpy/Makefile +++ b/examples/hello_strlcpy/Makefile @@ -1,7 +1,7 @@ -MKC_SOURCE_FUNCLIBS = strlcpy -MKC_CHECK_FUNCS3 += strlcpy:string.h +MKC_SOURCE_FUNCLIBS = strlcpy getline +MKC_CHECK_FUNCS3 += strlcpy:string.h getline:stdio.h -PROG = hello4 +PROG = hello SRCS = hello.c WARNS = 4 diff --git a/examples/hello_strlcpy/expect.out b/examples/hello_strlcpy/expect.out index 060d314..d64ce75 100644 --- a/examples/hello_strlcpy/expect.out +++ b/examples/hello_strlcpy/expect.out @@ -25,18 +25,25 @@ /objdir/Makefile /objdir/_mkc_compiler_type.err /objdir/_mkc_compiler_type.res +/objdir/_mkc_func3_getline_stdio_h.c +/objdir/_mkc_func3_getline_stdio_h.err +/objdir/_mkc_func3_getline_stdio_h.res /objdir/_mkc_func3_strlcpy_string_h.c /objdir/_mkc_func3_strlcpy_string_h.err /objdir/_mkc_func3_strlcpy_string_h.res +/objdir/_mkc_funclib_getline.c +/objdir/_mkc_funclib_getline.err +/objdir/_mkc_funclib_getline.res /objdir/_mkc_funclib_strlcpy.c /objdir/_mkc_funclib_strlcpy.err /objdir/_mkc_funclib_strlcpy.res /objdir/_mkc_prog_cc.err /objdir/_mkc_prog_cc.res /objdir/expect.out +/objdir/getline.c +/objdir/hello /objdir/hello.c /objdir/hello.o -/objdir/hello4 /objdir/hello_strlcpy.test.out.tmp /objdir/input.in /objdir/strlcpy.c @@ -44,21 +51,28 @@ ========= install ========== /objdir/prefix /objdir/prefix/bin -/objdir/prefix/bin/hello4 +/objdir/prefix/bin/hello ======== uninstall ========= ========== clean =========== /objdir/Makefile /objdir/_mkc_compiler_type.err /objdir/_mkc_compiler_type.res +/objdir/_mkc_func3_getline_stdio_h.c +/objdir/_mkc_func3_getline_stdio_h.err +/objdir/_mkc_func3_getline_stdio_h.res /objdir/_mkc_func3_strlcpy_string_h.c /objdir/_mkc_func3_strlcpy_string_h.err /objdir/_mkc_func3_strlcpy_string_h.res +/objdir/_mkc_funclib_getline.c +/objdir/_mkc_funclib_getline.err +/objdir/_mkc_funclib_getline.res /objdir/_mkc_funclib_strlcpy.c /objdir/_mkc_funclib_strlcpy.err /objdir/_mkc_funclib_strlcpy.res /objdir/_mkc_prog_cc.err /objdir/_mkc_prog_cc.res /objdir/expect.out +/objdir/getline.c /objdir/hello.c /objdir/hello_strlcpy.test.out.tmp /objdir/input.in @@ -67,8 +81,13 @@ ======= distclean ========== /objdir/Makefile /objdir/expect.out +/objdir/getline.c /objdir/hello.c /objdir/hello_strlcpy.test.out.tmp /objdir/input.in /objdir/strlcpy.c /objdir/test.mk +======= CLEANFILES ========== +strlcpy.o +getline.o +hello.o diff --git a/examples/hello_strlcpy/getline.c b/examples/hello_strlcpy/getline.c new file mode 100644 index 0000000..777f5c4 --- /dev/null +++ b/examples/hello_strlcpy/getline.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2007-2013 by Aleksey Cheusov + * + * See LICENSE file in the distribution. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "decls.h" + +ssize_t +getline(char** lineptr, size_t* n, FILE* stream) +{ + int c; + size_t sz = 0; + + while (c = getc (stream), c != EOF){ + if (sz+1 >= *n){ + /* +2 is for `c' and 0-terminator */ + *n = *n * 3 / 2 + 2; + *lineptr = realloc (*lineptr, *n); + if (!*lineptr) + return -1; + } + + (*lineptr) [sz++] = (char) c; + if (c == '\n') + break; + } + + if (ferror (stdin)) + return (ssize_t) -1; + + if (!sz){ + if (feof (stdin)){ + return (ssize_t) -1; + }else if (!*n){ + *lineptr = malloc (1); + if (!*lineptr) + return -1; + + *n = 1; + } + } + + (*lineptr) [sz] = 0; + return sz; +} diff --git a/examples/hello_strlcpy/hello.c b/examples/hello_strlcpy/hello.c index b7dd661..177b6b5 100644 --- a/examples/hello_strlcpy/hello.c +++ b/examples/hello_strlcpy/hello.c @@ -5,14 +5,18 @@ size_t strlcpy(char *dst, const char *src, size_t siz); #endif +#ifndef HAVE_FUNC3_GETLINE_STDIO_H +ssize_t getline(char** lineptr, size_t* n, FILE* stream); +#endif + int main (int argc, char ** argv) { - char buf [2000]; + char *buf = NULL; + size_t size = 0; + ssize_t len = 0; char small_buf [10]; - size_t len; - - while (fgets (buf, sizeof (buf), stdin)){ + while (len = getline (&buf, &size, stdin), len != -1){ len = strlen (buf); if (len > 0 && buf [len-1] == '\n') buf [len-1] = 0; diff --git a/examples/hello_strlcpy/test.mk b/examples/hello_strlcpy/test.mk index ffa417d..e0155bb 100644 --- a/examples/hello_strlcpy/test.mk +++ b/examples/hello_strlcpy/test.mk @@ -1,33 +1,38 @@ .PHONY : test_output test_output: @set -e; \ - ${.OBJDIR}/hello4 < ${.CURDIR}/input.in; \ + ${.OBJDIR}/hello < ${.CURDIR}/input.in; \ rm -rf ${.OBJDIR}${PREFIX}; \ + MKCATPAGES=yes; export MKCATPAGES; \ \ echo =========== all ============; \ - find ${.OBJDIR} -type f | grep -v 'strlcpy[.]o' | \ + find ${.OBJDIR} -type f | grep -Ev '(strlcpy|getline)[.]o' | \ mkc_test_helper "${PREFIX}" "${.OBJDIR}"; \ \ echo ========= install ==========; \ - ${MAKE} ${MAKEFLAGS} install DESTDIR=${.OBJDIR} \ + ${MAKE} ${MAKEFLAGS} install -j3 DESTDIR=${.OBJDIR} \ > /dev/null; \ find ${.OBJDIR}${PREFIX} -type f -o -type d | \ - grep -v 'strlcpy[.]o' | \ + grep -vE '(strlcpy|getline)[.]o' | \ mkc_test_helper "${PREFIX}" "${.OBJDIR}"; \ \ echo ======== uninstall =========; \ - ${MAKE} ${MAKEFLAGS} uninstall DESTDIR=${.OBJDIR} > /dev/null; \ - find ${.OBJDIR}${PREFIX} -type f | grep -v 'strlcpy[.]o' | \ + ${MAKE} ${MAKEFLAGS} -j4 uninstall DESTDIR=${.OBJDIR} > /dev/null; \ + find ${.OBJDIR}${PREFIX} -type f | grep -vE '(strlcpy|getline)[.]o' | \ mkc_test_helper "${PREFIX}" "${.OBJDIR}"; \ \ echo ========== clean ===========; \ - ${MAKE} ${MAKEFLAGS} clean DESTDIR=${.OBJDIR} > /dev/null; \ - find ${.OBJDIR} -type f | grep -v 'strlcpy[.]o' | \ + ${MAKE} ${MAKEFLAGS} clean > /dev/null; \ + find ${.OBJDIR} -type f | grep -vE '(strlcpy|getline)[.]o' | \ mkc_test_helper "${PREFIX}" "${.OBJDIR}"; \ \ echo ======= distclean ==========; \ - ${MAKE} ${MAKEFLAGS} distclean DESTDIR=${.OBJDIR} > /dev/null; \ - find ${.OBJDIR} -type f | grep -v 'strlcpy[.]o' | \ - mkc_test_helper "${PREFIX}" "${.OBJDIR}" + ${MAKE} ${MAKEFLAGS} distclean > /dev/null; \ + find ${.OBJDIR} -type f | grep -vE '(strlcpy|getline)[.]o' | \ + mkc_test_helper "${PREFIX}" "${.OBJDIR}"; \ + \ + echo ======= CLEANFILES ==========; \ + ${MAKE} ${MAKEFLAGS} print-values VARS='CLEANFILES' MKCHECKS=no | \ + awk '{for(i=1; i<=NF; ++i) if ($$i ~ /[.]o$$/) print $$i}' .include <mkc.minitest.mk> |