table

Помоћни програм из командне линије за форматирање и приказ CSV-а
git clone https://git.sr.ht/~strahinja/table
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување aa4fdf8bd7342ef2f1850121d5a2733406c6f236
родитељ f302d73a13c5e197ddc462c6ca3b87914648350f
Аутор: Страхиња Радић <sr@strahinja.org>
Датум:   Sun, 22 Mar 2026 20:01:47 +0000

Replace sscanf with new function: strtonum, for more robust number parsing

Diffstat:
MMakefile | 2+-
Mconfig.Haiku | 6+++---
Mconfig.Linux | 6+++---
Mconfig.OpenBSD | 6+++---
Astrtonum.c | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astrtonum.h | 16++++++++++++++++
Mtable.c | 26++++++++++++++------------
измењених датотека: 7, додавања: 136(+), брисања: 22(-)

diff --git a/Makefile b/Makefile @@ -28,7 +28,7 @@ makedate: makeversion: sh lib/makeversion -l -table.o: table.c defs.h utf8.h version.h +table.o: table.c defs.h strtonum.h utf8.h version.h $(MANPAGE): $(MANPAGE).in date version diff --git a/config.Haiku b/config.Haiku @@ -5,9 +5,9 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L \ DESTDIR = /boot/home/config/non-packaged INSTALL = install LIBS = -SRC = table.c utf8.c -HEADERS = defs.h utf8.h version.h -OBJS = table.o utf8.o +SRC = strtonum.c table.c utf8.c +HEADERS = defs.h strtonum.h utf8.h version.h +OBJS = strtonum.o table.o utf8.o PREFIX = /usr/local PROG = table SYNONYMS = tablep tsvtable diff --git a/config.Linux b/config.Linux @@ -3,9 +3,9 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L \ -D_XOPEN_SOURCE=700 -DNDEBUG INSTALL = install LIBS = -SRC = table.c utf8.c -HEADERS = defs.h utf8.h version.h -OBJS = table.o utf8.o +SRC = strtonum.c table.c utf8.c +HEADERS = defs.h strtonum.h utf8.h version.h +OBJS = strtonum.o table.o utf8.o PREFIX = /usr/local PROG = table SYNONYMS = tablep tsvtable diff --git a/config.OpenBSD b/config.OpenBSD @@ -3,9 +3,9 @@ CPPFLAGS = -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L \ -D_XOPEN_SOURCE=700 -D_BSD_SOURCE -DNDEBUG INSTALL = install LIBS = -SRC = table.c utf8.c -HEADERS = defs.h utf8.h version.h -OBJS = table.o utf8.o +SRC = strtonum.c table.c utf8.c +HEADERS = defs.h strtonum.h utf8.h version.h +OBJS = strtonum.o table.o utf8.o PREFIX = /usr/local PROG = table SYNONYMS = tablep tsvtable diff --git a/strtonum.c b/strtonum.c @@ -0,0 +1,96 @@ +/* This program is licensed under the terms of GNU GPL v3 or (at your option) + * any later version. Copyright (C) 2026 Страхиња Радић. + * See the file LICENSE for exact copyright and license details. */ + +#include <errno.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "strtonum.h" + +/* Returns 0 on no error, otherwise returns 1 after outputting error message */ +int +stn_handle_error(const char* str, const long long minval, + const long long maxval, const int errornum) +{ + switch (errornum) + { + case STN_NOTNUM: + fprintf(stderr, "strtonumber: Not a number: %s\n", str); + return 1; + case STN_INVLIMITS: + fprintf(stderr, "strtonumber: Invalid limit specification\n"); + return 1; + case STN_UNDERFLOW: + fprintf(stderr, "strtonumber: Underflow: %s < %lld\n", str, + minval); + return 1; + case STN_OVERFLOW: + fprintf(stderr, "strtonumber: Overflow: %s > %lld\n", str, + maxval); + return 1; + default:; + } + return 0; +} + +/* - Returns the converted number or 0 on error (*not* indicative of error; see + * below) + * - Clears errno (discards any previous value) because of strtoll + * - Instead, returns any errors in errornum (if non-NULL); + * *errornum == STN_NONE indicates success, any other value indicates error */ +long long +strtonumber(const char* str, const long long minval, const long long maxval, + int* errornum) +{ + long long n; + char* end = NULL; + + if (minval > maxval) + { + if (errornum) + *errornum = STN_INVLIMITS; + return 0; + } + errno = 0; + if (errornum) + *errornum = STN_NONE; + n = strtoll(str, &end, 10); + if (!*str || *end) + { + errno = 0; + if (errornum) + *errornum = STN_NOTNUM; + return 0; + } + else if (errno == ERANGE) + { + errno = 0; + if (errornum) + *errornum + = n == LLONG_MIN ? STN_UNDERFLOW : STN_OVERFLOW; + return 0; + } + else + { + errno = 0; + if (n > maxval) + { + if (errornum) + *errornum = STN_OVERFLOW; + return 0; + } + else if (n < minval) + { + if (errornum) + *errornum = STN_UNDERFLOW; + return 0; + } + else + return n; + } + /* Not reached */ + return n; +} diff --git a/strtonum.h b/strtonum.h @@ -0,0 +1,16 @@ +/* This program is licensed under the terms of GNU GPL v3 or (at your option) + * any later version. Copyright (C) 2026 Страхиња Радић. + * See the file LICENSE for exact copyright and license details. */ + +enum { + STN_NONE, + STN_NOTNUM, + STN_INVLIMITS, + STN_UNDERFLOW, + STN_OVERFLOW, +}; + +int stn_handle_error(const char* str, const long long minval, + const long long maxval, const int errornum); +long long strtonumber(const char* str, const long long minval, + const long long maxval, int* errornum); diff --git a/table.c b/table.c @@ -14,6 +14,7 @@ #include <unistd.h> #include "defs.h" +#include "strtonum.h" #include "version.h" #define COPYRIGHT \ @@ -296,7 +297,7 @@ set_align(const char* arg, Alignment** align, size_t* align_size) free(*align); *align = NULL; *align_size = 0; - error_code = ERR_BAD_ALIGN; + error_code = ERR_BAD_ALIGN; return 1; } assert((*parg == 'l') || (*parg == 'c') || (*parg == 'r')); @@ -309,7 +310,7 @@ static int set_columns(const char* arg, ssize_t* cols) { ssize_t c; - int result; + int errornum; assert((arg != NULL) && (cols != NULL)); errno = 0; if (!*arg) @@ -317,8 +318,10 @@ set_columns(const char* arg, ssize_t* cols) error_code = ERR_NO_CL_ARG_PARAM; return 1; } - result = sscanf(arg, "%zd", &c); - if (result == EOF || result < 1) + c = strtonumber(arg, -((unsigned long long)SSIZE_MAX + 1), + (long long)SSIZE_MAX, &errornum); + if (stn_handle_error(arg, -((unsigned long long)SSIZE_MAX + 1), + (long long)SSIZE_MAX, errornum)) { error_code = ERR_BAD_NUMBER; return 1; @@ -347,7 +350,7 @@ set_format(const char* arg, int** format, size_t* format_size) char* token = NULL; char* ptoken = NULL; size_t token_size = BUF_DEFAULT; - int num, result; + int num, errornum; assert((arg != NULL) && (format != NULL) && (format_size != NULL)); CALLOC(token, char, token_size); @@ -377,8 +380,8 @@ set_format(const char* arg, int** format, size_t* format_size) } *ptoken = 0; errno = 0; - result = sscanf(token, "%d", &num); - if (result == EOF || result < 1) + num = strtonumber(token, INT_MIN, INT_MAX, &errornum); + if (stn_handle_error(token, INT_MIN, INT_MAX, errornum)) { free(token); free(*format); @@ -398,8 +401,8 @@ set_format(const char* arg, int** format, size_t* format_size) { *ptoken = 0; errno = 0; - result = sscanf(token, "%d", &num); - if (result == EOF || result < 1) + num = strtonumber(token, INT_MIN, INT_MAX, &errornum); + if (stn_handle_error(token, INT_MIN, INT_MAX, errornum)) { free(token); free(*format); @@ -820,9 +823,8 @@ do_arg: goto do_arg; done_arg: - if (cmd == CMD_COLUMNS || cmd == CMD_DELIMITER - || cmd == CMD_FORMAT || cmd == CMD_SYMBOLS - || cmd == CMD_QUOTES) + if (cmd == CMD_COLUMNS || cmd == CMD_DELIMITER || cmd == CMD_FORMAT + || cmd == CMD_SYMBOLS || cmd == CMD_QUOTES) { error_code = ERR_NO_CL_ARG_PARAM; goto main_done;