Wed Jun 24 16:49:30 2020 UTC ()
Try not to lose error output with --error-output.

Try to avoid the trap we set up ourselves while avoiding freopen(3).
When exit flushes and closes open streams it may close sfp first and
when it comes about to flush and close stderr, the descriptor is
already gone and we lose any buffered error output.  This actually
happens on some hosts, breaking --trace output used by autoconf.


(uwe)
diff -r1.48 -r1.49 src/usr.bin/m4/main.c

cvs diff -r1.48 -r1.49 src/usr.bin/m4/main.c (expand / switch to unified diff)

--- src/usr.bin/m4/main.c 2019/03/26 16:41:06 1.48
+++ src/usr.bin/m4/main.c 2020/06/24 16:49:30 1.49
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1/* $OpenBSD: main.c,v 1.77 2009/10/14 17:19:47 sthen Exp $ */ 1/* $OpenBSD: main.c,v 1.77 2009/10/14 17:19:47 sthen Exp $ */
2/* $NetBSD: main.c,v 1.48 2019/03/26 16:41:06 christos Exp $ */ 2/* $NetBSD: main.c,v 1.49 2020/06/24 16:49:30 uwe Exp $ */
3 3
4/*- 4/*-
5 * Copyright (c) 1989, 1993 5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved. 6 * The Regents of the University of California. All rights reserved.
7 * 7 *
8 * This code is derived from software contributed to Berkeley by 8 * This code is derived from software contributed to Berkeley by
9 * Ozan Yigit at York University. 9 * Ozan Yigit at York University.
10 * 10 *
11 * Redistribution and use in source and binary forms, with or without 11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions 12 * modification, are permitted provided that the following conditions
13 * are met: 13 * are met:
14 * 1. Redistributions of source code must retain the above copyright 14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer. 15 * notice, this list of conditions and the following disclaimer.
@@ -32,27 +32,27 @@ @@ -32,27 +32,27 @@
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE. 33 * SUCH DAMAGE.
34 */ 34 */
35 35
36/* 36/*
37 * main.c 37 * main.c
38 * Facility: m4 macro processor 38 * Facility: m4 macro processor
39 * by: oz 39 * by: oz
40 */ 40 */
41#if HAVE_NBTOOL_CONFIG_H 41#if HAVE_NBTOOL_CONFIG_H
42#include "nbtool_config.h" 42#include "nbtool_config.h"
43#endif 43#endif
44#include <sys/cdefs.h> 44#include <sys/cdefs.h>
45__RCSID("$NetBSD: main.c,v 1.48 2019/03/26 16:41:06 christos Exp $"); 45__RCSID("$NetBSD: main.c,v 1.49 2020/06/24 16:49:30 uwe Exp $");
46#include <assert.h> 46#include <assert.h>
47#include <signal.h> 47#include <signal.h>
48#include <getopt.h> 48#include <getopt.h>
49#include <err.h> 49#include <err.h>
50#include <errno.h> 50#include <errno.h>
51#include <unistd.h> 51#include <unistd.h>
52#include <stdio.h> 52#include <stdio.h>
53#include <ctype.h> 53#include <ctype.h>
54#include <string.h> 54#include <string.h>
55#include <stddef.h> 55#include <stddef.h>
56#include <stdint.h> 56#include <stdint.h>
57#include <stdlib.h> 57#include <stdlib.h>
58#include <ohash.h> 58#include <ohash.h>
@@ -264,26 +264,37 @@ main(int argc, char *argv[]) @@ -264,26 +264,37 @@ main(int argc, char *argv[])
264 fatal_warnings++; 264 fatal_warnings++;
265 break; 265 break;
266 case 'e': 266 case 'e':
267 /* 267 /*
268 * Don't use freopen here because if it fails 268 * Don't use freopen here because if it fails
269 * we lose stderr, instead trash it. 269 * we lose stderr, instead trash it.
270 */ 270 */
271 if ((sfp = fopen(optarg, "w+")) == NULL) { 271 if ((sfp = fopen(optarg, "w+")) == NULL) {
272 warn("Can't redirect errors to `%s'", optarg); 272 warn("Can't redirect errors to `%s'", optarg);
273 break; 273 break;
274 } 274 }
275 fclose(stderr); 275 fclose(stderr);
276 memcpy(stderr, sfp, sizeof(*sfp)); 276 memcpy(stderr, sfp, sizeof(*sfp));
 277 /*
 278 * XXX: try to avoid the trap set up by the
 279 * kludge above. When exit flushes and closes
 280 * open streams it may close sfp first and
 281 * when it comes about to flush and close
 282 * stderr, the descriptor is already gone and
 283 * we lose any buffered output. This actually
 284 * happens on some hosts, breaking autoconf
 285 * tracing.
 286 */
 287 setvbuf(stderr, (char *)NULL, _IOLBF, 0);
277 break; 288 break;
278 case 'F': 289 case 'F':
279 freeze = optarg; 290 freeze = optarg;
280#ifndef REAL_FREEZE 291#ifndef REAL_FREEZE
281 if ((freezef = fopen(freeze, "w")) == NULL) 292 if ((freezef = fopen(freeze, "w")) == NULL)
282 err(EXIT_FAILURE, "Can't open `%s'", freeze); 293 err(EXIT_FAILURE, "Can't open `%s'", freeze);
283#endif 294#endif
284 break; 295 break;
285 case 'I': 296 case 'I':
286 addtoincludepath(optarg); 297 addtoincludepath(optarg);
287 break; 298 break;
288 case 'i': 299 case 'i':
289 setvbuf(stdout, NULL, _IONBF, 0); 300 setvbuf(stdout, NULL, _IONBF, 0);