Skip to content

Commit

Permalink
Show pkg_info output inline instead of buffering.
Browse files Browse the repository at this point in the history
This avoids an occasional issue where the exec_list() output was being
corrupted.  Buffering the entire output could be considered useful if we
were actually using it to return an error status and print a useful
message, but we weren't, so this way is faster, uses less memory, and
allows us to remove more code.

We also now correctly return the exit code of pkg_info.
  • Loading branch information
Jonathan Perkin committed Jun 18, 2018
1 parent 6b502bc commit 23cf457
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 81 deletions.
6 changes: 3 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,15 @@ main(int argc, char *argv[])
break;
case PKG_SHPKGCONT_CMD: /* show remote package content */
missing_param(argc, 2, MSG_MISSING_PKGNAME);
show_pkg_info('L', argv[1]); /* pkg_info flag */
rc = show_pkg_info('L', argv[1]); /* pkg_info flag */
break;
case PKG_SHPKGDESC_CMD: /* show remote package DESCR */
missing_param(argc, 2, MSG_MISSING_PKGNAME);
show_pkg_info('d', argv[1]); /* pkg_info flag */
rc = show_pkg_info('d', argv[1]); /* pkg_info flag */
break;
case PKG_SHPKGBDEFS_CMD: /* show remote package build definitions */
missing_param(argc, 2, MSG_MISSING_PKGNAME);
show_pkg_info('B', argv[1]); /* pkg_info flag */
rc = show_pkg_info('B', argv[1]); /* pkg_info flag */
break;
case PKG_SHCAT_CMD: /* show packages belonging to a category */
missing_param(argc, 2, MSG_MISSING_CATEGORY);
Expand Down
28 changes: 17 additions & 11 deletions pkg_infos.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@

#include "pkgin.h"

void
int
show_pkg_info(char flag, char *pkgname)
{
int i;
char cmd[BUFSIZ], *fullpkgname, **prepos, **out_cmd = NULL;
FILE *fp;
int rv = 0;
char buf[MAXLEN], cmd[BUFSIZ], *fullpkgname, **prepos;

if ((fullpkgname = unique_pkg(pkgname, REMOTE_PKG)) == NULL)
errx(EXIT_FAILURE, MSG_PKG_NOT_AVAIL, pkgname);
Expand All @@ -43,16 +44,21 @@ show_pkg_info(char flag, char *pkgname)
snprintf(cmd, BUFSIZ, "%s -%c %s/%s%s",
pkg_info, flag, *prepos, fullpkgname, PKG_EXT);

if ((out_cmd = exec_list(cmd, NULL)) == NULL)
continue;
if ((fp = popen(cmd, "r")) == NULL)
return 1;

for (i = 0; out_cmd[i] != NULL; i++)
printf("%s\n", out_cmd[i]);
/*
* Historically pkgin skipped blank lines, so we preserve
* that behaviour for now.
*/
while (fgets(buf, MAXLEN, fp) != NULL) {
if (buf[0] != '\n')
printf("%s", buf);
}

free_list(out_cmd);
if (pclose(fp) != 0)
rv = 1;
}

XFREE(fullpkgname);

return;
return rv;
}
2 changes: 1 addition & 1 deletion pkgin.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ int pkg_met_reqs(Plisthead *);
int pkg_has_conflicts(Pkglist *);
void show_prov_req(const char *, const char *);
/* pkg_infos.c */
void show_pkg_info(char, char *);
int show_pkg_info(char, char *);

/* pkg_install.c */
extern char *pkg_install_dir;
Expand Down
64 changes: 0 additions & 64 deletions tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,6 @@ trimcr(char *str)
return (0);
}

char **
splitstr(char *str, const char *sep)
{
size_t size;
int i;
char *p, *tmp, **split;

for (i = 0, size = 0; str[i] != '\0'; i++)
if (str[i] == *sep)
size++;

/* size = number of separators + 1 member + NULL */
size += 2;
split = xmalloc(size * sizeof(char *));

i = 0;
for (p = str; p != NULL;)
while ((tmp = strsep(&p, sep)) != NULL) {
if (*tmp != '\0') {
while (*tmp == ' ' || *tmp == '\t')
tmp++;
split[i] = xstrdup(tmp);
i++;
}
}

split[i] = NULL;

return(split);
}

void
free_list(char **list)
{
Expand Down Expand Up @@ -122,39 +91,6 @@ trunc_str(char *str, char limit, int direction)
}
}

/* execute a command and receive result on a char ** */
char **
exec_list(const char *cmd, const char *match)
{
FILE *fp;
size_t size;
char **res, *rawlist, buf[MAXLEN];

if ((fp = popen(cmd, "r")) == NULL)
return(NULL);

rawlist = NULL;
size = 0;

while (fgets(buf, MAXLEN, fp) != NULL) {
if (match == NULL || strstr(buf, match) != NULL) {
size += (strlen(buf) + 1) * sizeof(char);

rawlist = xrealloc(rawlist, size);
strlcat(rawlist, buf, size);
}
}
pclose(fp);

if (rawlist == NULL)
return(NULL);

res = splitstr(rawlist, "\n");
XFREE(rawlist);

return(res);
}

void
do_log(const char *path, const char *fmt, ...)
{
Expand Down
2 changes: 0 additions & 2 deletions tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,7 @@ extern uint8_t noflag;

extern int charcount(char *, char);
extern int trimcr(char *);
extern char **splitstr(char *, const char *);
extern void free_list(char **);
extern char **exec_list(const char *, const char *);
extern void do_log(const char *, const char *, ...);
extern void trunc_str(char *, char, int);
extern char *strreplace(char *, const char *, const char *);
Expand Down

0 comments on commit 23cf457

Please sign in to comment.