1
0
mirror of https://github.com/moparisthebest/pacman synced 2025-01-09 04:57:59 -05:00

fgets invocation cleanup

From the fgets manpage:

	fgets() reads in at most one less than size characters from stream and
	stores them into the buffer pointed to by s. Reading stops after an EOF
	or a newline. If a newline is read, it is stored into the buffer. A
	'\0' is stored after the last character in the buffer.

This means there is no need at all to do 'size - 1' math. Remove all of that
and just use sizeof() for simplicity on the buffer we plan on reading into.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2010-07-07 17:12:42 -05:00
parent ce3f4e7800
commit d896527d21
3 changed files with 33 additions and 34 deletions

View File

@ -438,8 +438,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
{ {
FILE *fp = NULL; FILE *fp = NULL;
char path[PATH_MAX]; char path[PATH_MAX];
char line[513]; char line[1024];
int sline = sizeof(line)-1;
char *pkgpath = NULL; char *pkgpath = NULL;
ALPM_LOG_FUNC; ALPM_LOG_FUNC;
@ -471,7 +470,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
info->name, inforeq); info->name, inforeq);
/* clear out 'line', to be certain - and to make valgrind happy */ /* clear out 'line', to be certain - and to make valgrind happy */
memset(line, 0, sline+1); memset(line, 0, sizeof(line));
pkgpath = get_pkgpath(db, info); pkgpath = get_pkgpath(db, info);
@ -490,12 +489,12 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
goto error; goto error;
} }
while(!feof(fp)) { while(!feof(fp)) {
if(fgets(line, 256, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
break; break;
} }
_alpm_strtrim(line); _alpm_strtrim(line);
if(strcmp(line, "%NAME%") == 0) { if(strcmp(line, "%NAME%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
if(strcmp(_alpm_strtrim(line), info->name) != 0) { if(strcmp(_alpm_strtrim(line), info->name) != 0) {
@ -503,7 +502,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
"mismatch on package %s\n"), db->treename, info->name); "mismatch on package %s\n"), db->treename, info->name);
} }
} else if(strcmp(line, "%VERSION%") == 0) { } else if(strcmp(line, "%VERSION%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
if(strcmp(_alpm_strtrim(line), info->version) != 0) { if(strcmp(_alpm_strtrim(line), info->version) != 0) {
@ -511,39 +510,39 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
"mismatch on package %s\n"), db->treename, info->name); "mismatch on package %s\n"), db->treename, info->name);
} }
} else if(strcmp(line, "%FILENAME%") == 0) { } else if(strcmp(line, "%FILENAME%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
STRDUP(info->filename, _alpm_strtrim(line), goto error); STRDUP(info->filename, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%DESC%") == 0) { } else if(strcmp(line, "%DESC%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
STRDUP(info->desc, _alpm_strtrim(line), goto error); STRDUP(info->desc, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%GROUPS%") == 0) { } else if(strcmp(line, "%GROUPS%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
char *linedup; char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error); STRDUP(linedup, _alpm_strtrim(line), goto error);
info->groups = alpm_list_add(info->groups, linedup); info->groups = alpm_list_add(info->groups, linedup);
} }
} else if(strcmp(line, "%URL%") == 0) { } else if(strcmp(line, "%URL%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
STRDUP(info->url, _alpm_strtrim(line), goto error); STRDUP(info->url, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%LICENSE%") == 0) { } else if(strcmp(line, "%LICENSE%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
char *linedup; char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error); STRDUP(linedup, _alpm_strtrim(line), goto error);
info->licenses = alpm_list_add(info->licenses, linedup); info->licenses = alpm_list_add(info->licenses, linedup);
} }
} else if(strcmp(line, "%ARCH%") == 0) { } else if(strcmp(line, "%ARCH%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
STRDUP(info->arch, _alpm_strtrim(line), goto error); STRDUP(info->arch, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%BUILDDATE%") == 0) { } else if(strcmp(line, "%BUILDDATE%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(line); _alpm_strtrim(line);
@ -559,7 +558,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
info->builddate = atol(line); info->builddate = atol(line);
} }
} else if(strcmp(line, "%INSTALLDATE%") == 0) { } else if(strcmp(line, "%INSTALLDATE%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
_alpm_strtrim(line); _alpm_strtrim(line);
@ -575,12 +574,12 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
info->installdate = atol(line); info->installdate = atol(line);
} }
} else if(strcmp(line, "%PACKAGER%") == 0) { } else if(strcmp(line, "%PACKAGER%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
STRDUP(info->packager, _alpm_strtrim(line), goto error); STRDUP(info->packager, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%REASON%") == 0) { } else if(strcmp(line, "%REASON%") == 0) {
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line)); info->reason = (pmpkgreason_t)atol(_alpm_strtrim(line));
@ -590,7 +589,7 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
* is currently only used in sync databases, and SIZE is * is currently only used in sync databases, and SIZE is
* only used in local databases. * only used in local databases.
*/ */
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
info->size = atol(_alpm_strtrim(line)); info->size = atol(_alpm_strtrim(line));
@ -601,19 +600,19 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
} else if(strcmp(line, "%ISIZE%") == 0) { } else if(strcmp(line, "%ISIZE%") == 0) {
/* ISIZE (installed size) tag only appears in sync repositories, /* ISIZE (installed size) tag only appears in sync repositories,
* not the local one. */ * not the local one. */
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
info->isize = atol(_alpm_strtrim(line)); info->isize = atol(_alpm_strtrim(line));
} else if(strcmp(line, "%MD5SUM%") == 0) { } else if(strcmp(line, "%MD5SUM%") == 0) {
/* MD5SUM tag only appears in sync repositories, /* MD5SUM tag only appears in sync repositories,
* not the local one. */ * not the local one. */
if(fgets(line, sline, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
goto error; goto error;
} }
STRDUP(info->md5sum, _alpm_strtrim(line), goto error); STRDUP(info->md5sum, _alpm_strtrim(line), goto error);
} else if(strcmp(line, "%REPLACES%") == 0) { } else if(strcmp(line, "%REPLACES%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
char *linedup; char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error); STRDUP(linedup, _alpm_strtrim(line), goto error);
info->replaces = alpm_list_add(info->replaces, linedup); info->replaces = alpm_list_add(info->replaces, linedup);
@ -633,16 +632,16 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
_alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno)); _alpm_log(PM_LOG_ERROR, _("could not open file %s: %s\n"), path, strerror(errno));
goto error; goto error;
} }
while(fgets(line, 256, fp)) { while(fgets(line, sizeof(line), fp)) {
_alpm_strtrim(line); _alpm_strtrim(line);
if(strcmp(line, "%FILES%") == 0) { if(strcmp(line, "%FILES%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
char *linedup; char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error); STRDUP(linedup, _alpm_strtrim(line), goto error);
info->files = alpm_list_add(info->files, linedup); info->files = alpm_list_add(info->files, linedup);
} }
} else if(strcmp(line, "%BACKUP%") == 0) { } else if(strcmp(line, "%BACKUP%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
char *linedup; char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error); STRDUP(linedup, _alpm_strtrim(line), goto error);
info->backup = alpm_list_add(info->backup, linedup); info->backup = alpm_list_add(info->backup, linedup);
@ -661,29 +660,29 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
goto error; goto error;
} }
while(!feof(fp)) { while(!feof(fp)) {
if(fgets(line, 256, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
break; break;
} }
_alpm_strtrim(line); _alpm_strtrim(line);
if(strcmp(line, "%DEPENDS%") == 0) { if(strcmp(line, "%DEPENDS%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
pmdepend_t *dep = _alpm_splitdep(_alpm_strtrim(line)); pmdepend_t *dep = _alpm_splitdep(_alpm_strtrim(line));
info->depends = alpm_list_add(info->depends, dep); info->depends = alpm_list_add(info->depends, dep);
} }
} else if(strcmp(line, "%OPTDEPENDS%") == 0) { } else if(strcmp(line, "%OPTDEPENDS%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
char *linedup; char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error); STRDUP(linedup, _alpm_strtrim(line), goto error);
info->optdepends = alpm_list_add(info->optdepends, linedup); info->optdepends = alpm_list_add(info->optdepends, linedup);
} }
} else if(strcmp(line, "%CONFLICTS%") == 0) { } else if(strcmp(line, "%CONFLICTS%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
char *linedup; char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error); STRDUP(linedup, _alpm_strtrim(line), goto error);
info->conflicts = alpm_list_add(info->conflicts, linedup); info->conflicts = alpm_list_add(info->conflicts, linedup);
} }
} else if(strcmp(line, "%PROVIDES%") == 0) { } else if(strcmp(line, "%PROVIDES%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
char *linedup; char *linedup;
STRDUP(linedup, _alpm_strtrim(line), goto error); STRDUP(linedup, _alpm_strtrim(line), goto error);
info->provides = alpm_list_add(info->provides, linedup); info->provides = alpm_list_add(info->provides, linedup);
@ -699,12 +698,12 @@ int _alpm_db_read(pmdb_t *db, pmpkg_t *info, pmdbinfrq_t inforeq)
snprintf(path, PATH_MAX, "%sdeltas", pkgpath); snprintf(path, PATH_MAX, "%sdeltas", pkgpath);
if((fp = fopen(path, "r"))) { if((fp = fopen(path, "r"))) {
while(!feof(fp)) { while(!feof(fp)) {
if(fgets(line, 256, fp) == NULL) { if(fgets(line, sizeof(line), fp) == NULL) {
break; break;
} }
_alpm_strtrim(line); _alpm_strtrim(line);
if(strcmp(line, "%DELTAS%") == 0) { if(strcmp(line, "%DELTAS%") == 0) {
while(fgets(line, sline, fp) && strlen(_alpm_strtrim(line))) { while(fgets(line, sizeof(line), fp) && strlen(_alpm_strtrim(line))) {
pmdelta_t *delta = _alpm_delta_parse(line); pmdelta_t *delta = _alpm_delta_parse(line);
if(delta) { if(delta) {
info->deltas = alpm_list_add(info->deltas, delta); info->deltas = alpm_list_add(info->deltas, delta);

View File

@ -323,10 +323,11 @@ static int grep(const char *fn, const char *needle)
} }
while(!feof(fp)) { while(!feof(fp)) {
char line[1024]; char line[1024];
int sline = sizeof(line)-1; if(fgets(line, sizeof(line), fp) == NULL) {
if(fgets(line, sline, fp) == NULL) {
continue; continue;
} }
/* TODO: this will not work if the search string
* ends up being split across line reads */
if(strstr(line, needle)) { if(strstr(line, needle)) {
fclose(fp); fclose(fp);
return(1); return(1);

View File

@ -690,7 +690,6 @@ void display_optdepends(pmpkg_t *pkg)
static int question(short preset, char *fmt, va_list args) static int question(short preset, char *fmt, va_list args)
{ {
char response[32]; char response[32];
int sresponse = sizeof(response)-1;
FILE *stream; FILE *stream;
if(config->noconfirm) { if(config->noconfirm) {
@ -713,7 +712,7 @@ static int question(short preset, char *fmt, va_list args)
return(preset); return(preset);
} }
if(fgets(response, sresponse, stdin)) { if(fgets(response, sizeof(response), stdin)) {
strtrim(response); strtrim(response);
if(strlen(response) == 0) { if(strlen(response) == 0) {
return(preset); return(preset);