The branch stable/13 has been updated by emaste:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=4cd49eda1f6e5b8094279394e41560e515c5ca55

commit 4cd49eda1f6e5b8094279394e41560e515c5ca55
Author:     Mark Johnston <ma...@freebsd.org>
AuthorDate: 2022-01-27 22:10:17 +0000
Commit:     Ed Maste <ema...@freebsd.org>
CommitDate: 2022-02-09 15:25:28 +0000

    ar: Avoid overwriting the stdout file stream pointer
    
    This doesn't work with musl, which defines stdout as FILE * const.
    Instead, explicitly pass the desired output stream to ar_read_archive().
    
    No functional change intended.
    
    Reviewed by:    emaste
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D34064
    
    (cherry picked from commit 41e6398f9bc1bba4ed872118e742096d692fdfec)
---
 usr.bin/ar/acpyacc.y | 14 ++++++--------
 usr.bin/ar/ar.c      |  2 +-
 usr.bin/ar/ar.h      |  2 +-
 usr.bin/ar/read.c    | 18 +++++++++---------
 4 files changed, 17 insertions(+), 19 deletions(-)

diff --git a/usr.bin/ar/acpyacc.y b/usr.bin/ar/acpyacc.y
index 34156e1d2d58..1ce7197ef222 100644
--- a/usr.bin/ar/acpyacc.y
+++ b/usr.bin/ar/acpyacc.y
@@ -406,7 +406,7 @@ arscp_extract(struct list *list)
        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 @@ arscp_list(void)
        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 @@ arscp_dir(char *archive, struct list *list, char *rlt)
        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 @@ arscp_dir(char *archive, struct list *list, char *rlt)
        }
        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);
diff --git a/usr.bin/ar/ar.c b/usr.bin/ar/ar.c
index 4353efcb0ff2..93887ebebcfb 100644
--- a/usr.bin/ar/ar.c
+++ b/usr.bin/ar/ar.c
@@ -331,7 +331,7 @@ main(int argc, char **argv)
                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();
diff --git a/usr.bin/ar/ar.h b/usr.bin/ar/ar.h
index bcccf93a6016..66a7888c3e1c 100644
--- a/usr.bin/ar/ar.h
+++ b/usr.bin/ar/ar.h
@@ -115,7 +115,7 @@ struct bsdar {
 };
 
 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, ...);
diff --git a/usr.bin/ar/read.c b/usr.bin/ar/read.c
index 81e0bfce1b7e..84b94a1f4d77 100644
--- a/usr.bin/ar/read.c
+++ b/usr.bin/ar/read.c
@@ -47,7 +47,7 @@ __FBSDID("$FreeBSD$");
  * 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 @@ ar_read_archive(struct bsdar *bsdar, int mode)
                                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 @@ ar_read_archive(struct bsdar *bsdar, int mode)
                        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 @@ ar_read_archive(struct bsdar *bsdar, int mode)
                                }
 
                                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,

Reply via email to