From a1ff719ba479d4dd71870911834ebf17094c5378 Mon Sep 17 00:00:00 2001 From: tg Date: Fri, 10 Nov 2006 04:03:59 +0000 Subject: [PATCH] thanks to the nice property of my reimplementation of getn to also return a value in case of an error (0 or the partial result, which is the full result in case of trailing junk even), using it to rid atoi() is possible, saving 9t 4d 1i --- exec.c | 4 ++-- histrap.c | 5 +++-- jobs.c | 6 +++--- misc.c | 17 ++++++++++------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/exec.c b/exec.c index 72424e6..e4cae40 100644 --- a/exec.c +++ b/exec.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.17 2006/11/10 01:44:39 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.18 2006/11/10 04:03:58 tg Exp $"); static int comexec(struct op *, struct tbl *volatile, char **, int volatile); @@ -1216,7 +1216,7 @@ do_selectargs(char **ap, bool print_menu) return NULL; s = str_val(global("REPLY")); if (*s) { - i = atoi(s); + getn(s, &i); return (i >= 1 && i <= argct) ? ap[i - 1] : null; } print_menu = 1; diff --git a/histrap.c b/histrap.c index 985d892..9be2611 100644 --- a/histrap.c +++ b/histrap.c @@ -3,7 +3,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.28 2006/11/10 03:50:05 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.29 2006/11/10 04:03:59 tg Exp $"); #if !defined(__sun__) #define DO_HISTORY @@ -481,8 +481,9 @@ findhistrel(const char *str) { int maxhist = histptr - history; int start = maxhist - 1; - int rec = atoi(str); + int rec; + getn(str, &rec); if (rec == 0) return -1; if (rec > 0) { diff --git a/jobs.c b/jobs.c index 570dd26..cce339c 100644 --- a/jobs.c +++ b/jobs.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.12 2006/11/09 22:08:07 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.13 2006/11/10 04:03:59 tg Exp $"); /* Order important! */ #define PRUNNING 0 @@ -1302,7 +1302,7 @@ j_lookup(const char *cp, int *ecodep) int len, job = 0; if (digit(*cp)) { - job = atoi(cp); + getn(cp, &job); /* Look for last_proc->pid (what $! returns) first... */ for (j = job_list; j != NULL; j = j->next) if (j->last_proc && j->last_proc->pid == job) @@ -1336,7 +1336,7 @@ j_lookup(const char *cp, int *ecodep) case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': - job = atoi(cp); + getn(cp, &job); for (j = job_list; j != NULL; j = j->next) if (j->job == job) return j; diff --git a/misc.c b/misc.c index 5f9e5cb..eec45f3 100644 --- a/misc.c +++ b/misc.c @@ -3,7 +3,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.31 2006/11/10 03:54:38 tg Exp $\t" +__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.32 2006/11/10 04:03:59 tg Exp $\t" MKSH_SH_H_ID); unsigned char chtypes[UCHAR_MAX + 1]; /* type bits for unsigned char */ @@ -416,7 +416,7 @@ parse_args(char **argv, int getn(const char *s, int *ai) { - int i, c; + int i, c, rv = 0; bool neg = false; do { @@ -427,19 +427,22 @@ getn(const char *s, int *ai) c = *s++; } else if (c == '+') c = *s++; - i = 0; + *ai = i = 0; do { - *ai = i; if (!isdigit(c)) - return (0); + goto getn_out; i *= 10; if (i < *ai) /* overflow */ - return (0); + goto getn_out; i += c - '0'; + *ai = i; } while ((c = *s++)); + rv = 1; - *ai = neg ? -i : i; + getn_out: + if (neg) + *ai = -*ai; return (1); }