Index: usr.bin/ar/acpyacc.y =================================================================== --- usr.bin/ar/acpyacc.y +++ usr.bin/ar/acpyacc.y @@ -406,7 +406,7 @@ if (!arscp_target_exist()) return; arscp_mlist2argv(list); - ar_read_archive(bsdar, 'x'); + ar_read_archive(bsdar, 'x', stdout); arscp_free_argv(); arscp_free_mlist(list); } @@ -422,7 +422,7 @@ bsdar->argv = NULL; /* Always verbose. */ bsdar->options |= AR_V; - ar_read_archive(bsdar, 't'); + ar_read_archive(bsdar, 't', stdout); bsdar->options &= ~AR_V; } @@ -433,10 +433,9 @@ FILE *out; /* If rlt != NULL, redirect output to it */ - out = NULL; + out = stdout; if (rlt) { - out = stdout; - if ((stdout = fopen(rlt, "w")) == NULL) + if ((out = fopen(rlt, "w")) == NULL) bsdar_errc(bsdar, errno, "fopen %s failed", rlt); } @@ -449,13 +448,12 @@ } if (verbose) bsdar->options |= AR_V; - ar_read_archive(bsdar, 't'); + ar_read_archive(bsdar, 't', out); bsdar->options &= ~AR_V; if (rlt) { - if (fclose(stdout) == EOF) + if (fclose(out) == EOF) bsdar_errc(bsdar, errno, "fclose %s failed", rlt); - stdout = out; free(rlt); } free(archive); Index: usr.bin/ar/ar.h =================================================================== --- usr.bin/ar/ar.h +++ usr.bin/ar/ar.h @@ -115,7 +115,7 @@ }; void ar_mode_script(struct bsdar *ar); -int ar_read_archive(struct bsdar *ar, int mode); +int ar_read_archive(struct bsdar *ar, int mode, FILE *out); int ar_write_archive(struct bsdar *ar, int mode); void bsdar_errc(struct bsdar *, int _code, const char *fmt, ...) __dead2; void bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...); Index: usr.bin/ar/ar.c =================================================================== --- usr.bin/ar/ar.c +++ usr.bin/ar/ar.c @@ -330,7 +330,7 @@ exitcode = ar_write_archive(bsdar, bsdar->mode); break; case 'p': case 't': case 'x': - exitcode = ar_read_archive(bsdar, bsdar->mode); + exitcode = ar_read_archive(bsdar, bsdar->mode, stdout); break; default: bsdar_usage(); Index: usr.bin/ar/read.c =================================================================== --- usr.bin/ar/read.c +++ usr.bin/ar/read.c @@ -47,7 +47,7 @@ * Handle read modes: 'x', 't' and 'p'. */ int -ar_read_archive(struct bsdar *bsdar, int mode) +ar_read_archive(struct bsdar *bsdar, int mode, FILE *out) { struct archive *a; struct archive_entry *entry; @@ -123,18 +123,18 @@ size = archive_entry_size(entry); mtime = archive_entry_mtime(entry); (void)strmode(md, buf); - (void)fprintf(stdout, "%s %6d/%-6d %8ju ", + (void)fprintf(out, "%s %6d/%-6d %8ju ", buf + 1, uid, gid, (uintmax_t)size); tp = localtime(&mtime); (void)strftime(buf, sizeof(buf), "%b %e %H:%M %Y", tp); - (void)fprintf(stdout, "%s %s", buf, name); + (void)fprintf(out, "%s %s", buf, name); } else - (void)fprintf(stdout, "%s", name); + (void)fprintf(out, "%s", name); r = archive_read_data_skip(a); if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY || r == ARCHIVE_FATAL) { - (void)fprintf(stdout, "\n"); + (void)fprintf(out, "\n"); bsdar_warnc(bsdar, archive_errno(a), "%s", archive_error_string(a)); } @@ -142,14 +142,14 @@ if (r == ARCHIVE_FATAL) break; - (void)fprintf(stdout, "\n"); + (void)fprintf(out, "\n"); } else { /* mode == 'x' || mode = 'p' */ if (mode == 'p') { if (bsdar->options & AR_V) { - (void)fprintf(stdout, "\n<%s>\n\n", + (void)fprintf(out, "\n<%s>\n\n", name); - fflush(stdout); + fflush(out); } r = archive_read_data_into_fd(a, 1); } else { @@ -172,7 +172,7 @@ } if (bsdar->options & AR_V) - (void)fprintf(stdout, "x - %s\n", name); + (void)fprintf(out, "x - %s\n", name); /* Disallow absolute paths. */ if (name[0] == '/') { bsdar_warnc(bsdar, 0,