summaryrefslogtreecommitdiff
path: root/lib/libalpm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libalpm')
-rw-r--r--lib/libalpm/be_local.c12
-rw-r--r--lib/libalpm/be_package.c7
-rw-r--r--lib/libalpm/be_sync.c12
-rw-r--r--lib/libalpm/util.c20
-rw-r--r--lib/libalpm/util.h1
5 files changed, 37 insertions, 15 deletions
diff --git a/lib/libalpm/be_local.c b/lib/libalpm/be_local.c
index 261ad871..49661e24 100644
--- a/lib/libalpm/be_local.c
+++ b/lib/libalpm/be_local.c
@@ -500,7 +500,7 @@ static char *get_pkgpath(alpm_db_t *db, alpm_pkg_t *info)
#define READ_NEXT() do { \
if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
- _alpm_strtrim(line); \
+ _alpm_strip_newline(line); \
} while(0)
#define READ_AND_STORE(f) do { \
@@ -510,8 +510,8 @@ static char *get_pkgpath(alpm_db_t *db, alpm_pkg_t *info)
#define READ_AND_STORE_ALL(f) do { \
char *linedup; \
- READ_NEXT(); \
- if(strlen(line) == 0) break; \
+ if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) goto error; \
+ if(_alpm_strip_newline(line) == 0) break; \
STRDUP(linedup, line, goto error); \
f = alpm_list_add(f, linedup); \
} while(1) /* note the while(1) and not (0) */
@@ -629,12 +629,12 @@ static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
goto error;
}
while(fgets(line, sizeof(line), fp)) {
- _alpm_strtrim(line);
+ _alpm_strip_newline(line);
if(strcmp(line, "%FILES%") == 0) {
size_t files_count = 0, files_size = 0;
alpm_file_t *files = NULL;
- while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sizeof(line), fp) && _alpm_strip_newline(line)) {
if(files_count >= files_size) {
size_t old_size = files_size;
if(files_size == 0) {
@@ -661,7 +661,7 @@ static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
info->files.count = files_count;
info->files.files = files;
} else if(strcmp(line, "%BACKUP%") == 0) {
- while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
+ while(fgets(line, sizeof(line), fp) && _alpm_strip_newline(line)) {
alpm_backup_t *backup;
CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
if(_alpm_split_backup(line, &backup)) {
diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c
index 0edaa5a3..7e90af71 100644
--- a/lib/libalpm/be_package.c
+++ b/lib/libalpm/be_package.c
@@ -149,13 +149,13 @@ static int parse_descfile(alpm_handle_t *handle, struct archive *a, alpm_pkg_t *
/* loop until we reach EOF or other error */
while((ret = _alpm_archive_fgets(a, &buf)) == ARCHIVE_OK) {
- char *line = _alpm_strtrim(buf.line);
+ size_t len = _alpm_strip_newline(buf.line);
linenum++;
- if(strlen(line) == 0 || line[0] == '#') {
+ if(len == 0 || buf.line[0] == '#') {
continue;
}
- ptr = line;
+ ptr = buf.line;
key = strsep(&ptr, "=");
if(key == NULL || ptr == NULL) {
_alpm_log(handle, ALPM_LOG_DEBUG, "%s: syntax error in description file line %d\n",
@@ -213,7 +213,6 @@ static int parse_descfile(alpm_handle_t *handle, struct archive *a, alpm_pkg_t *
newpkg->name ? newpkg->name : "error", key, linenum);
}
}
- line[0] = '\0';
}
if(ret != ARCHIVE_EOF) {
_alpm_log(handle, ALPM_LOG_DEBUG, "error parsing package descfile\n");
diff --git a/lib/libalpm/be_sync.c b/lib/libalpm/be_sync.c
index 07356f0a..d4c71a8e 100644
--- a/lib/libalpm/be_sync.c
+++ b/lib/libalpm/be_sync.c
@@ -443,7 +443,8 @@ static int sync_db_populate(alpm_db_t *db)
#define READ_NEXT() do { \
if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \
- line = _alpm_strtrim(buf.line); \
+ line = buf.line; \
+ _alpm_strip_newline(line); \
} while(0)
#define READ_AND_STORE(f) do { \
@@ -453,9 +454,9 @@ static int sync_db_populate(alpm_db_t *db)
#define READ_AND_STORE_ALL(f) do { \
char *linedup; \
- READ_NEXT(); \
- if(strlen(line) == 0) break; \
- STRDUP(linedup, line, goto error); \
+ if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \
+ if(_alpm_strip_newline(buf.line) == 0) break; \
+ STRDUP(linedup, buf.line, goto error); \
f = alpm_list_add(f, linedup); \
} while(1) /* note the while(1) and not (0) */
@@ -493,7 +494,8 @@ static int sync_db_read(alpm_db_t *db, struct archive *archive,
|| strcmp(filename, "deltas") == 0) {
int ret;
while((ret = _alpm_archive_fgets(archive, &buf)) == ARCHIVE_OK) {
- char *line = _alpm_strtrim(buf.line);
+ char *line = buf.line;
+ _alpm_strip_newline(line);
if(strcmp(line, "%NAME%") == 0) {
READ_NEXT();
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 030cf43b..123cd24e 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -207,6 +207,26 @@ char *_alpm_strtrim(char *str)
return str;
}
+/**
+ * Trim trailing newline from a string (if one exists).
+ * @param str a single line of text
+ * @return the length of the trimmed string
+ */
+size_t _alpm_strip_newline(char *str)
+{
+ size_t len;
+ if(str == '\0') {
+ return 0;
+ }
+ len = strlen(str);
+ while(str[len - 1] == '\n') {
+ len--;
+ }
+ str[len] = '\0';
+
+ return len;
+}
+
/* Compression functions */
/**
diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
index a75c5aae..9ee63709 100644
--- a/lib/libalpm/util.h
+++ b/lib/libalpm/util.h
@@ -95,6 +95,7 @@ int _alpm_makepath(const char *path);
int _alpm_makepath_mode(const char *path, mode_t mode);
int _alpm_copyfile(const char *src, const char *dest);
char *_alpm_strtrim(char *str);
+size_t _alpm_strip_newline(char *str);
int _alpm_unpack_single(alpm_handle_t *handle, const char *archive,
const char *prefix, const char *filename);
int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix,