From bff5f15d0aeb287ec5ad70bb815528332958339b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20P=C3=A9dron?= Date: Thu, 31 Jul 2014 16:15:31 +0200 Subject: [PATCH] Support .pc file without a trailing newline Some editors write text files without a newline character at the end of the last line. Before this change, the main loop in pkg_new_from_file() expected a newline character at the end of the line and overwrote it with a NUL character. In the case of the last line in a non-newline-terminated file, this ate the last character of that line. Now, the pkg_fgetline() function takes care of returning a line without the newline character (or the "\r\n" sequence in case of a DOS file format). --- fileio.c | 12 ++++++++++-- pkg.c | 2 -- tests/lib1/no-trailing-newline.pc | 10 ++++++++++ tests/run.sh.in | 2 ++ 4 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 tests/lib1/no-trailing-newline.pc diff --git a/fileio.c b/fileio.c index 2c6b9e9..fed3da9 100644 --- a/fileio.c +++ b/fileio.c @@ -83,10 +83,18 @@ pkg_fgetline(char *line, size_t size, FILE *stream) } - *s = '\0'; - if (c == EOF && (s == line || ferror(stream))) return NULL; + *s = '\0'; + + /* Remove newline character. */ + if (s > line && *(--s) == '\n') { + *s = '\0'; + + if (s > line && *(--s) == '\r') + *s = '\0'; + } + return line; } diff --git a/pkg.c b/pkg.c index caed158..5388094 100644 --- a/pkg.c +++ b/pkg.c @@ -212,8 +212,6 @@ pkg_new_from_file(const char *filename, FILE *f, unsigned int flags) { char op, *p, *key, *value; - readbuf[strlen(readbuf) - 1] = '\0'; - p = readbuf; while (*p && (isalpha(*p) || isdigit(*p) || *p == '_' || *p == '.')) p++; diff --git a/tests/lib1/no-trailing-newline.pc b/tests/lib1/no-trailing-newline.pc new file mode 100644 index 0000000..3359ec7 --- /dev/null +++ b/tests/lib1/no-trailing-newline.pc @@ -0,0 +1,10 @@ +prefix=/usr +exec_prefix=${prefix} +libdir=${prefix}/lib +includedir=${prefix}/include + +Name: no-trailing-newline +Description: A testing pkg-config file +Version: 1.2.3 +Libs: -L${libdir}/no-trailing-newline -lno-trailing-newline +Cflags: -I${includedir}/no-trailing-newline \ No newline at end of file diff --git a/tests/run.sh.in b/tests/run.sh.in index 102dbbb..ba6b34e 100644 --- a/tests/run.sh.in +++ b/tests/run.sh.in @@ -122,6 +122,8 @@ run_test "PKG_CONFIG_PATH='${selfdir}/lib1' ${1} --static --libs-only-l private- # 4) tests for parser bugs run_test "PKG_CONFIG_PATH='${selfdir}/lib1' ${1} --libs dos-lineendings" \ '-L/usr/lib/dos-lineendings -ldos-lineendings' +run_test "PKG_CONFIG_PATH='${selfdir}/lib1' ${1} --cflags no-trailing-newline" \ + '-I/usr/include/no-trailing-newline' run_test "PKG_CONFIG_PATH='${selfdir}/lib1' ${1} --libs argv-parse" \ '-llib-2 -lpthread' run_test "PKG_CONFIG_PATH='${selfdir}/lib1' ${1} --exists -foo; echo \$?" \