Module Name:    src
Committed By:   rillig
Date:           Sun Jul 26 13:39:30 UTC 2020

Modified Files:
        src/usr.bin/make: buf.c buf.h var.c

Log Message:
make(1): add Buf_AddInt to make the calling code simpler


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/usr.bin/make/buf.c
cvs rdiff -u -r1.19 -r1.20 src/usr.bin/make/buf.h
cvs rdiff -u -r1.311 -r1.312 src/usr.bin/make/var.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/buf.c
diff -u src/usr.bin/make/buf.c:1.26 src/usr.bin/make/buf.c:1.27
--- src/usr.bin/make/buf.c:1.26	Fri Jul  3 08:02:55 2020
+++ src/usr.bin/make/buf.c	Sun Jul 26 13:39:30 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: buf.c,v 1.26 2020/07/03 08:02:55 rillig Exp $	*/
+/*	$NetBSD: buf.c,v 1.27 2020/07/26 13:39:30 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,25 +70,26 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: buf.c,v 1.26 2020/07/03 08:02:55 rillig Exp $";
+static char rcsid[] = "$NetBSD: buf.c,v 1.27 2020/07/26 13:39:30 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)buf.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: buf.c,v 1.26 2020/07/03 08:02:55 rillig Exp $");
+__RCSID("$NetBSD: buf.c,v 1.27 2020/07/26 13:39:30 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
 
 /*-
  * buf.c --
- *	Functions for automatically-expanded buffers.
+ *	Functions for automatically-expanded NUL-terminated buffers.
  */
 
-#include    "make.h"
-#include    "buf.h"
+#include <limits.h>
+#include "make.h"
+#include "buf.h"
 
 #ifndef max
 #define max(a,b)  ((a) > (b) ? (a) : (b))
@@ -142,6 +143,28 @@ Buf_AddBytes(Buffer *bp, int numBytes, c
 
 /*-
  *-----------------------------------------------------------------------
+ * Buf_AddInt --
+ *	Add the given number to the buffer.
+ *
+ *-----------------------------------------------------------------------
+ */
+void
+Buf_AddInt(Buffer *bp, int n)
+{
+    /*
+     * We need enough space for the decimal representation of an int.
+     * We calculate the space needed for the octal representation, and
+     * add enough slop to cope with a '-' sign and a trailing '\0'.
+     */
+    size_t bits = sizeof(int) * CHAR_BIT;
+    char buf[1 + (bits + 2) / 3 + 1];
+
+    int len = snprintf(buf, sizeof buf, "%d", n);
+    Buf_AddBytes(bp, len, buf);
+}
+
+/*-
+ *-----------------------------------------------------------------------
  * Buf_GetAll --
  *	Get all the available data at once.
  *

Index: src/usr.bin/make/buf.h
diff -u src/usr.bin/make/buf.h:1.19 src/usr.bin/make/buf.h:1.20
--- src/usr.bin/make/buf.h:1.19	Wed May 31 22:02:06 2017
+++ src/usr.bin/make/buf.h	Sun Jul 26 13:39:30 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: buf.h,v 1.19 2017/05/31 22:02:06 maya Exp $	*/
+/*	$NetBSD: buf.h,v 1.20 2020/07/26 13:39:30 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -110,6 +110,7 @@ typedef struct Buffer {
 
 void Buf_Expand_1(Buffer *);
 void Buf_AddBytes(Buffer *, int, const Byte *);
+void Buf_AddInt(Buffer *, int);
 Byte *Buf_GetAll(Buffer *, int *);
 void Buf_Empty(Buffer *);
 void Buf_Init(Buffer *, int);

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.311 src/usr.bin/make/var.c:1.312
--- src/usr.bin/make/var.c:1.311	Sun Jul 26 12:27:09 2020
+++ src/usr.bin/make/var.c	Sun Jul 26 13:39:30 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.311 2020/07/26 12:27:09 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.312 2020/07/26 13:39:30 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.311 2020/07/26 12:27:09 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.312 2020/07/26 13:39:30 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.311 2020/07/26 12:27:09 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.312 2020/07/26 13:39:30 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1776,10 +1776,9 @@ static char *
 VarRange(const char *str, int ac)
 {
     Buffer	  buf;		/* Buffer for new string */
-    char	  tmp[32];	/* each element */
     char 	**av;		/* List of words to affect */
     char 	 *as;		/* Word list memory */
-    int 	  i, n;
+    int 	  i;
 
     Buf_Init(&buf, 0);
     if (ac > 0) {
@@ -1789,10 +1788,7 @@ VarRange(const char *str, int ac)
 	av = brk_string(str, &ac, FALSE, &as);
     }
     for (i = 0; i < ac; i++) {
-	n = snprintf(tmp, sizeof(tmp), "%d", 1 + i);
-	if (n >= (int)sizeof(tmp))
-	    break;
-	Buf_AddBytes(&buf, n, tmp);
+	Buf_AddInt(&buf, 1 + i);
 	if (i != ac - 1)
 	    Buf_AddByte(&buf, ' ');
     }
@@ -2657,28 +2653,21 @@ ApplyModifier_Words(const char *mod, App
 	goto bad_modifier;	/* empty square brackets in ":[]". */
 
     if (estr[0] == '#' && estr[1] == '\0') { /* Found ":[#]" */
-	/*
-	 * We will need enough space for the decimal representation of an int.
-	 * We calculate the space needed for the octal representation, and add
-	 * enough slop to cope with a '-' sign (which should never be needed)
-	 * and a '\0' string terminator.
-	 */
-	int newStrSize = (sizeof(int) * CHAR_BIT + 2) / 3 + 2;
-
-	st->newStr = bmake_malloc(newStrSize);
 	if (st->oneBigWord) {
-	    strncpy(st->newStr, "1", newStrSize);
+	    st->newStr = bmake_strdup("1");
 	} else {
 	    /* XXX: brk_string() is a rather expensive
 	     * way of counting words. */
-	    char **av;
 	    char *as;
 	    int ac;
-
-	    av = brk_string(st->nstr, &ac, FALSE, &as);
-	    snprintf(st->newStr, newStrSize, "%d", ac);
+	    char **av = brk_string(st->nstr, &ac, FALSE, &as);
 	    free(as);
 	    free(av);
+
+	    Buffer buf;
+	    Buf_Init(&buf, 4);	/* 3 digits + '\0' */
+	    Buf_AddInt(&buf, ac);
+	    st->newStr = Buf_Destroy(&buf, FALSE);
 	}
 	goto ok;
     }

Reply via email to