mirror of
https://github.com/moparisthebest/pacman
synced 2025-03-01 01:41:52 -05:00
add line length parameter to _alpm_strip_newline
If known, callers can pass the line size to this function in order to avoid an strlen call. Otherwise, they simply pass 0 and _alpm_strip_newline will do the call instead. Signed-off-by: Dave Reisner <dreisner@archlinux.org> Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
parent
74274b5dc3
commit
f556fe8b4a
@ -496,7 +496,7 @@ char *_alpm_local_db_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_strip_newline(line); \
|
||||
_alpm_strip_newline(line, 0); \
|
||||
} while(0)
|
||||
|
||||
#define READ_AND_STORE(f) do { \
|
||||
@ -509,7 +509,7 @@ char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
|
||||
if(fgets(line, sizeof(line), fp) == NULL) {\
|
||||
if(!feof(fp)) goto error; else break; \
|
||||
} \
|
||||
if(_alpm_strip_newline(line) == 0) break; \
|
||||
if(_alpm_strip_newline(line, 0) == 0) break; \
|
||||
STRDUP(linedup, line, goto error); \
|
||||
f = alpm_list_add(f, linedup); \
|
||||
} while(1) /* note the while(1) and not (0) */
|
||||
@ -518,7 +518,7 @@ char *_alpm_local_db_pkgpath(alpm_db_t *db, alpm_pkg_t *info,
|
||||
if(fgets(line, sizeof(line), fp) == NULL) {\
|
||||
if(!feof(fp)) goto error; else break; \
|
||||
} \
|
||||
if(_alpm_strip_newline(line) == 0) break; \
|
||||
if(_alpm_strip_newline(line, 0) == 0) break; \
|
||||
f = alpm_list_add(f, _alpm_splitdep(line)); \
|
||||
} while(1) /* note the while(1) and not (0) */
|
||||
|
||||
@ -564,7 +564,7 @@ static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
|
||||
if(fgets(line, sizeof(line), fp) == NULL && !feof(fp)) {
|
||||
goto error;
|
||||
}
|
||||
if(_alpm_strip_newline(line) == 0) {
|
||||
if(_alpm_strip_newline(line, 0) == 0) {
|
||||
/* length of stripped line was zero */
|
||||
continue;
|
||||
}
|
||||
@ -651,13 +651,13 @@ static int local_db_read(alpm_pkg_t *info, alpm_dbinfrq_t inforeq)
|
||||
}
|
||||
free(path);
|
||||
while(fgets(line, sizeof(line), fp)) {
|
||||
_alpm_strip_newline(line);
|
||||
_alpm_strip_newline(line, 0);
|
||||
if(strcmp(line, "%FILES%") == 0) {
|
||||
size_t files_count = 0, files_size = 0, len;
|
||||
alpm_file_t *files = NULL;
|
||||
|
||||
while(fgets(line, sizeof(line), fp) &&
|
||||
(len = _alpm_strip_newline(line))) {
|
||||
(len = _alpm_strip_newline(line, 0))) {
|
||||
if(files_count >= files_size) {
|
||||
size_t old_size = files_size;
|
||||
if(files_size == 0) {
|
||||
@ -691,7 +691,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) && _alpm_strip_newline(line)) {
|
||||
while(fgets(line, sizeof(line), fp) && _alpm_strip_newline(line, 0)) {
|
||||
alpm_backup_t *backup;
|
||||
CALLOC(backup, 1, sizeof(alpm_backup_t), goto error);
|
||||
if(_alpm_split_backup(line, &backup)) {
|
||||
|
@ -168,7 +168,7 @@ 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) {
|
||||
size_t len = _alpm_strip_newline(buf.line);
|
||||
size_t len = _alpm_strip_newline(buf.line, buf.real_line_size);
|
||||
|
||||
linenum++;
|
||||
key = buf.line;
|
||||
|
@ -482,7 +482,7 @@ cleanup:
|
||||
#define READ_NEXT() do { \
|
||||
if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \
|
||||
line = buf.line; \
|
||||
_alpm_strip_newline(line); \
|
||||
_alpm_strip_newline(line, buf.real_line_size); \
|
||||
} while(0)
|
||||
|
||||
#define READ_AND_STORE(f) do { \
|
||||
@ -493,14 +493,14 @@ cleanup:
|
||||
#define READ_AND_STORE_ALL(f) do { \
|
||||
char *linedup; \
|
||||
if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \
|
||||
if(_alpm_strip_newline(buf.line) == 0) break; \
|
||||
if(_alpm_strip_newline(buf.line, buf.real_line_size) == 0) break; \
|
||||
STRDUP(linedup, buf.line, goto error); \
|
||||
f = alpm_list_add(f, linedup); \
|
||||
} while(1) /* note the while(1) and not (0) */
|
||||
|
||||
#define READ_AND_SPLITDEP(f) do { \
|
||||
if(_alpm_archive_fgets(archive, &buf) != ARCHIVE_OK) goto error; \
|
||||
if(_alpm_strip_newline(buf.line) == 0) break; \
|
||||
if(_alpm_strip_newline(buf.line, buf.real_line_size) == 0) break; \
|
||||
f = alpm_list_add(f, _alpm_splitdep(line)); \
|
||||
} while(1) /* note the while(1) and not (0) */
|
||||
|
||||
@ -539,7 +539,7 @@ static int sync_db_read(alpm_db_t *db, struct archive *archive,
|
||||
int ret;
|
||||
while((ret = _alpm_archive_fgets(archive, &buf)) == ARCHIVE_OK) {
|
||||
char *line = buf.line;
|
||||
if(_alpm_strip_newline(line) == 0) {
|
||||
if(_alpm_strip_newline(line, buf.real_line_size) == 0) {
|
||||
/* length of stripped line was zero */
|
||||
continue;
|
||||
}
|
||||
|
@ -195,15 +195,17 @@ cleanup:
|
||||
|
||||
/** Trim trailing newlines from a string (if any exist).
|
||||
* @param str a single line of text
|
||||
* @param len size of str, if known, else 0
|
||||
* @return the length of the trimmed string
|
||||
*/
|
||||
size_t _alpm_strip_newline(char *str)
|
||||
size_t _alpm_strip_newline(char *str, size_t len)
|
||||
{
|
||||
size_t len;
|
||||
if(*str == '\0') {
|
||||
return 0;
|
||||
}
|
||||
if(len == 0) {
|
||||
len = strlen(str);
|
||||
}
|
||||
while(len > 0 && str[len - 1] == '\n') {
|
||||
len--;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user