Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): add Buf_AddInt to make the calling cod...



details:   https://anonhg.NetBSD.org/src/rev/5d6d85e0e827
branches:  trunk
changeset: 936391:5d6d85e0e827
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Jul 26 13:39:30 2020 +0000

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

diffstat:

 usr.bin/make/buf.c |  35 +++++++++++++++++++++++++++++------
 usr.bin/make/buf.h |   3 ++-
 usr.bin/make/var.c |  35 ++++++++++++-----------------------
 3 files changed, 43 insertions(+), 30 deletions(-)

diffs (173 lines):

diff -r 5d2832993c1c -r 5d6d85e0e827 usr.bin/make/buf.c
--- a/usr.bin/make/buf.c        Sun Jul 26 13:09:53 2020 +0000
+++ b/usr.bin/make/buf.c        Sun Jul 26 13:39:30 2020 +0000
@@ -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_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.
  *
diff -r 5d2832993c1c -r 5d6d85e0e827 usr.bin/make/buf.h
--- a/usr.bin/make/buf.h        Sun Jul 26 13:09:53 2020 +0000
+++ b/usr.bin/make/buf.h        Sun Jul 26 13:39:30 2020 +0000
@@ -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 @@
 
 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);
diff -r 5d2832993c1c -r 5d6d85e0e827 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sun Jul 26 13:09:53 2020 +0000
+++ b/usr.bin/make/var.c        Sun Jul 26 13:39:30 2020 +0000
@@ -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 @@
 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 @@
        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 @@
        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;
     }



Home | Main Index | Thread Index | Old Index