summaryrefslogtreecommitdiff
path: root/usr.bin/awk
diff options
context:
space:
mode:
authorkstailey <kstailey@cvs.openbsd.org>1997-01-29 18:33:57 +0000
committerkstailey <kstailey@cvs.openbsd.org>1997-01-29 18:33:57 +0000
commit2b30d39c3fd850820353b63447a24c7e75dc5ebc (patch)
tree044a05670cb2da790d426b532653775ce59ff954 /usr.bin/awk
parentd3392f87edb127e55cd899be6df1b58f26e5d3ad (diff)
calculate string length in format() more efficiently
Diffstat (limited to 'usr.bin/awk')
-rw-r--r--usr.bin/awk/run.c12
1 files changed, 4 insertions, 8 deletions
diff --git a/usr.bin/awk/run.c b/usr.bin/awk/run.c
index 21d8c79952e..5e03a0421ee 100644
--- a/usr.bin/awk/run.c
+++ b/usr.bin/awk/run.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: run.c,v 1.9 1997/01/25 03:24:52 kstailey Exp $ */
+/* $OpenBSD: run.c,v 1.10 1997/01/29 18:33:56 kstailey Exp $ */
/****************************************************************
Copyright (C) AT&T and Lucent Technologies 1996
All Rights Reserved
@@ -728,7 +728,7 @@ int format(char *buf, int bufsize, char *s, Node *a)
char fmt[RECSIZE];
char *p, *t, *os;
Cell *x;
- int flag = 0, len = 0, n;
+ int flag = 0, n;
os = s;
p = buf;
@@ -736,12 +736,10 @@ int format(char *buf, int bufsize, char *s, Node *a)
if (p - buf >= bufsize)
return -1;
if (*s != '%') {
- len++;
*p++ = *s++;
continue;
}
if (*(s+1) == '%') {
- len++;
*p++ = '%';
s += 2;
continue;
@@ -791,7 +789,6 @@ int format(char *buf, int bufsize, char *s, Node *a)
a = a->nnext;
switch (flag) {
case 0: sprintf((char *)p, "%s", fmt); /* unknown, so dump it too */
- len += strlen(p);
p += strlen(p);
sprintf((char *)p, "%s", getsval(x));
break;
@@ -810,19 +807,18 @@ int format(char *buf, int bufsize, char *s, Node *a)
isnum(x) ?
(getfval(x) ?
sprintf((char *)p, (char *)fmt, (int) getfval(x))
- : (*p++ = '\0', len++))
+ : (*p++ = '\0'))
: sprintf((char *)p, (char *)fmt, getsval(x)[0]);
break;
}
tempfree(x);
- len += strlen(p);
p += strlen(p);
s++;
}
*p = '\0';
for ( ; a; a = a->nnext) /* evaluate any remaining args */
execute(a);
- return (len);
+ return ((int)(p - buf));
}
Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */