Wed Jan 3 20:24:16 2024 UTC (151d)
make: do not add newline to an empty buffer

When LoadFile reads from /dev/null the buffer will be empty,
appending "\n" just results in an unnecessary extra call
to ParseRawLine.

Reviewed by: rillig


(sjg)
diff -r1.713 -r1.714 src/usr.bin/make/parse.c

cvs diff -r1.713 -r1.714 src/usr.bin/make/parse.c (expand / switch to unified diff)

--- src/usr.bin/make/parse.c 2023/12/29 20:43:58 1.713
+++ src/usr.bin/make/parse.c 2024/01/03 20:24:16 1.714
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: parse.c,v 1.713 2023/12/29 20:43:58 rillig Exp $ */ 1/* $NetBSD: parse.c,v 1.714 2024/01/03 20:24:16 sjg Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -95,27 +95,27 @@ @@ -95,27 +95,27 @@
95 */ 95 */
96 96
97#include <sys/types.h> 97#include <sys/types.h>
98#include <sys/stat.h> 98#include <sys/stat.h>
99#include <errno.h> 99#include <errno.h>
100#include <stdarg.h> 100#include <stdarg.h>
101 101
102#include "make.h" 102#include "make.h"
103#include "dir.h" 103#include "dir.h"
104#include "job.h" 104#include "job.h"
105#include "pathnames.h" 105#include "pathnames.h"
106 106
107/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */ 107/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
108MAKE_RCSID("$NetBSD: parse.c,v 1.713 2023/12/29 20:43:58 rillig Exp $"); 108MAKE_RCSID("$NetBSD: parse.c,v 1.714 2024/01/03 20:24:16 sjg Exp $");
109 109
110/* Detects a multiple-inclusion guard in a makefile. */ 110/* Detects a multiple-inclusion guard in a makefile. */
111typedef enum { 111typedef enum {
112 GS_START, /* at the beginning of the file */ 112 GS_START, /* at the beginning of the file */
113 GS_COND, /* after the guard condition */ 113 GS_COND, /* after the guard condition */
114 GS_DONE, /* after the closing .endif */ 114 GS_DONE, /* after the closing .endif */
115 GS_NO /* the file is not guarded */ 115 GS_NO /* the file is not guarded */
116} GuardState; 116} GuardState;
117 117
118/* 118/*
119 * A file being read. 119 * A file being read.
120 */ 120 */
121typedef struct IncludedFile { 121typedef struct IncludedFile {
@@ -373,27 +373,27 @@ LoadFile(const char *path, int fd) @@ -373,27 +373,27 @@ LoadFile(const char *path, int fd)
373 assert(buf.len < buf.cap); 373 assert(buf.len < buf.cap);
374 n = read(fd, buf.data + buf.len, buf.cap - buf.len); 374 n = read(fd, buf.data + buf.len, buf.cap - buf.len);
375 if (n < 0) { 375 if (n < 0) {
376 Error("%s: read error: %s", path, strerror(errno)); 376 Error("%s: read error: %s", path, strerror(errno));
377 exit(2); /* Not 1 so -q can distinguish error */ 377 exit(2); /* Not 1 so -q can distinguish error */
378 } 378 }
379 if (n == 0) 379 if (n == 0)
380 break; 380 break;
381 381
382 buf.len += (size_t)n; 382 buf.len += (size_t)n;
383 } 383 }
384 assert(buf.len <= buf.cap); 384 assert(buf.len <= buf.cap);
385 385
386 if (!Buf_EndsWith(&buf, '\n')) 386 if (buf.len > 0 && !Buf_EndsWith(&buf, '\n'))
387 Buf_AddByte(&buf, '\n'); 387 Buf_AddByte(&buf, '\n');
388 388
389 return buf; /* may not be null-terminated */ 389 return buf; /* may not be null-terminated */
390} 390}
391 391
392/* 392/*
393 * Print the current chain of .include and .for directives. In Parse_Fatal 393 * Print the current chain of .include and .for directives. In Parse_Fatal
394 * or other functions that already print the location, includingInnermost 394 * or other functions that already print the location, includingInnermost
395 * would be redundant, but in other cases like Error or Fatal it needs to be 395 * would be redundant, but in other cases like Error or Fatal it needs to be
396 * included. 396 * included.
397 */ 397 */
398void 398void
399PrintStackTrace(bool includingInnermost) 399PrintStackTrace(bool includingInnermost)