table

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

чување ec0e4b95273bbc539b8c339b9d5a87b1893ebb3f
родитељ 23bf46ee9cc4f17dbcf58a935e3ce725395c9977
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Mon, 11 Mar 2024 21:20:55 +0100

Remove strlc{at,py}.c; sprintf -> snprintf; add pledge(2)/unveil(2); update
copyright years

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
MINSTALL | 18+++++-------------
MREADME | 2+-
Mconfig.mk | 5+++--
Mdefs.h | 2+-
Dstrlcat.c | 54------------------------------------------------------
Dstrlcpy.c | 49-------------------------------------------------
Mtable.1.in | 2+-
Mtable.c | 58++++++++++++++++++++++++++++++++++++++++++----------------
Mutf8.c | 2+-
Mutf8.h | 2+-
Mversion.h.in | 2+-
измењених датотека: 11, додавања: 56(+), брисања: 140(-)

diff --git a/INSTALL b/INSTALL @@ -1,7 +1,7 @@ Prerequisites ============= -* C compiler defaults in config.redo +* C compiler defaults in config.mk Optional @@ -15,23 +15,15 @@ Install $ git clone https://git.sr.ht/~strahinja/table $ cd table - $ su - -If you have djb redo: - - # redo install - -if you don't: - - # ./do install + # make install Persistent build configuration file ----------------------------------- -The file config.redo will be sourced when building targets which also accept +The file config.mk will be sourced when building targets which also accept configuration through environment variables. For example, CC and PREFIX can be -specified in config.redo. +specified in config.mk. OpenBSD @@ -41,7 +33,7 @@ Replace gcc in CC with cc and add -D_BSD_SOURCE, eg: # CC='cc -g -Wall -pedantic -std=c99 -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 -D_BSD_SOURCE}' \ - ./do install + make -e install and change MANPREFIX to diff --git a/README b/README @@ -113,7 +113,7 @@ License ------- table - Command line utility to format and display CSV -Copyright (C) 2020-2023 Страхиња Радић +Copyright (C) 2020-2024 Страхиња Радић This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software diff --git a/config.mk b/config.mk @@ -6,9 +6,9 @@ CFLAGS = -g -Wall -pedantic -std=c99 -D_POSIX_C_SOURCE=200809L \ # -D_XOPEN_SOURCE=700 -D_BSD_SOURCE INSTALL = install LIBS = -SRC = strlcat.c strlcpy.c table.c utf8.c +SRC = table.c utf8.c HEADERS = defs.h utf8.h version.h -OBJS = strlcat.o strlcpy.o table.o utf8.o +OBJS = table.o utf8.o PREFIX = /usr/local PROG = table SYNONYMS = tablep tsvtable @@ -16,6 +16,7 @@ SYNONYMS = tablep tsvtable # Derived macros BINDIR = $(PREFIX)/bin DOCDIR = $(PREFIX)/share/doc/$(PROG) +EXDIR = $(DOCDIR)/examples MANPREFIX = $(PREFIX)/share/man # OpenBSD #MANPREFIX = $(PREFIX)/man diff --git a/defs.h b/defs.h @@ -1,5 +1,5 @@ /* This program is licensed under the terms of GNU GPL v3 or (at your option) - * any later version. Copyright (C) 2020-2023 Страхиња Радић. + * any later version. Copyright (C) 2020-2024 Страхиња Радић. * See the file LICENSE for exact copyright and license details. */ #include "utf8.h" diff --git a/strlcat.c b/strlcat.c @@ -1,54 +0,0 @@ -/* $OpenBSD: strlcat.c,v 1.9 2019/01/25 00:19:26 millert Exp $ */ - -/* - * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <string.h> - -/* - * Appends src to string dst of size dsize (unlike strncat, dsize is the - * full size of dst, not space left). At most dsize-1 characters - * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). - * Returns strlen(src) + MIN(dsize, strlen(initial dst)). - * If retval >= siz, truncation occurred. - */ -size_t -strlcat(char *dst, const char *src, size_t dsize) -{ - const char *odst = dst; - const char *osrc = src; - size_t n = dsize; - size_t dlen; - - /* Find the end of dst and adjust bytes left but don't go past end. */ - while (n-- != 0 && *dst != '\0') - dst++; - dlen = dst - odst; - n = dsize - dlen; - - if (n-- == 0) - return(dlen + strlen(src)); - while (*src != '\0') { - if (n != 0) { - *dst++ = *src; - n--; - } - src++; - } - *dst = '\0'; - - return(dlen + (src - osrc)); /* count does not include NUL */ -} diff --git a/strlcpy.c b/strlcpy.c @@ -1,49 +0,0 @@ -/* $OpenBSD: strlcpy.c,v 1.9 2019/01/25 00:19:26 millert Exp $ */ - -/* - * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <string.h> - -/* - * Copy string src to buffer dst of size dsize. At most dsize-1 - * chars will be copied. Always NUL terminates (unless dsize == 0). - * Returns strlen(src); if retval >= dsize, truncation occurred. - */ -size_t -strlcpy(char *dst, const char *src, size_t dsize) -{ - const char *osrc = src; - size_t nleft = dsize; - - /* Copy as many bytes as will fit. */ - if (nleft != 0) { - while (--nleft != 0) { - if ((*dst++ = *src++) == '\0') - break; - } - } - - /* Not enough room in dst, add NUL and traverse rest of src. */ - if (nleft == 0) { - if (dsize != 0) - *dst = '\0'; /* NUL-terminate dst */ - while (*src++) - ; - } - - return(src - osrc - 1); /* count does not include NUL */ -} diff --git a/table.1.in b/table.1.in @@ -64,7 +64,7 @@ is a synonym for .YS . .SH COPYRIGHT -table Copyright \(co 2020-2023 Strahinya Radich. +table Copyright \(co 2020-2024 Strahinya Radich. .br This program is licensed under GNU GPL v3 or later. See the file .I LICENSE diff --git a/table.c b/table.c @@ -1,5 +1,5 @@ /* This program is licensed under the terms of GNU GPL v3 or (at your option) - * any later version. Copyright (C) 2020-2023 Страхиња Радић. + * any later version. Copyright (C) 2020-2024 Страхиња Радић. * See the file LICENSE for exact copyright and license details. */ #include <errno.h> @@ -7,6 +7,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#ifdef __OpenBSD__ +#include <unistd.h> +#endif #include "defs.h" #include "version.h" @@ -83,7 +86,7 @@ int border_mode = 0; int no_ansi = 0; int in_sgr = 0; int msdos = 0; -int pause = 0; +int opause = 0; int expand_tabs = 0; int quotes = Q_PROCESS; char alignment_char = ' '; @@ -590,6 +593,14 @@ main(int argc, char** argv) char* sep = NULL; char* progname = NULL; +#ifdef __OpenBSD__ + if (pledge("stdio rpath unveil", NULL) < 0) + { + perror(PROGRAMNAME ": pledge"); + exit(1); + } +#endif + sep = strrchr(argv[0], '/'); progname = argv[0]; if (sep) @@ -601,7 +612,7 @@ main(int argc, char** argv) quotes = Q_IGNORE; } else if (!strcmp(progname, "tablep")) - pause = 1; + opause = 1; do_arg: arg = *++argv; @@ -678,7 +689,7 @@ do_arg: else if (!strcmp(arg, "pause")) { arg += strlen("pause"); - pause = 1; + opause = 1; } else if (startswith(arg, "quotes=")) { @@ -742,7 +753,7 @@ do_arg: no_ansi = 1; break; case 'p': - pause = 1; + opause = 1; break; case 'q': cmd = CMD_QUOTES; @@ -820,6 +831,18 @@ done_arg: FILE* input = NULL; if (filename) { +#ifdef __OpenBSD__ + if (unveil(filename, "r") < 0) + { + perror(PROGRAMNAME ": unveil"); + exit(1); + } + if (unveil(NULL, NULL) < 0) + { + perror(PROGRAMNAME ": unveil"); + exit(1); + } +#endif input = fopen(filename, "r"); if (!input) return error(ENOENT, "File not found: %s", filename); @@ -1036,14 +1059,16 @@ continue_line: { in_sgr = 1; if (!no_ansi) - poutbuf += sprintf((char*)poutbuf, "%c", *pline); + poutbuf += snprintf((char*)poutbuf, BUFSIZE, "%c", + *pline); pline++; colno++; } else if (uch == (u32)L'm') { if (!no_ansi || !in_sgr) - poutbuf += sprintf((char*)poutbuf, "%c", *pline); + poutbuf += snprintf((char*)poutbuf, BUFSIZE, "%c", + *pline); if (!in_sgr) outbufrunelen++; in_sgr = 0; @@ -1056,8 +1081,8 @@ continue_line: { pch_end = pline + ch_len; while (pline != pch_end) - poutbuf += sprintf((char*)poutbuf, "%c", - *pline++); + poutbuf += snprintf((char*)poutbuf, BUFSIZE, + "%c", *pline++); } else pline += ch_len; @@ -1069,7 +1094,7 @@ continue_line: size_t colwidth = current_column_width(); if (!no_ansi && lineno == 0) - poutbuf += sprintf((char*)poutbuf, "%s", + poutbuf += snprintf((char*)poutbuf, BUFSIZE, "%s", ANSI_SGR_BOLD_OFF); print_aligned(outbuf, outbufrunelen, colwidth, @@ -1107,7 +1132,7 @@ continue_line: } if (!no_ansi && lineno == 0) - poutbuf += sprintf((char*)poutbuf, "%s", + poutbuf += snprintf((char*)poutbuf, BUFSIZE, "%s", ANSI_SGR_BOLD_ON); pline += ch_len; @@ -1115,7 +1140,7 @@ continue_line: } else if (!ch_len) /* UTF-8 error */ { - sprintf((char*)poutbuf, "%c", *pline); + snprintf((char*)poutbuf, BUFSIZE, "%c", *pline); poutbuf++; outbufrunelen++; pline++; @@ -1125,7 +1150,7 @@ continue_line: { while ((current_rune_column + outbufrunelen) % tab_length != 0) { - sprintf((char*)poutbuf, "%c", alignment_char); + snprintf((char*)poutbuf, BUFSIZE, "%c", alignment_char); poutbuf++; outbufrunelen++; } @@ -1136,7 +1161,8 @@ continue_line: { pch_end = pline + ch_len; while (pline != pch_end) - poutbuf += sprintf((char*)poutbuf, "%c", *pline++); + poutbuf += snprintf((char*)poutbuf, BUFSIZE, "%c", + *pline++); outbufrunelen++; colno += ch_len; } @@ -1153,7 +1179,7 @@ done_line:; : format_value; if (!no_ansi && lineno == 0) - poutbuf += sprintf((char*)poutbuf, "%s", + poutbuf += snprintf((char*)poutbuf, BUFSIZE, "%s", ANSI_SGR_BOLD_OFF); print_aligned(outbuf, outbufrunelen, colwidth, @@ -1263,7 +1289,7 @@ skip_bottom_border: free(format); free(align); - if (pause) + if (opause) { char buf[BUFSIZE]; printf("Press Enter to continue: "); diff --git a/utf8.c b/utf8.c @@ -1,5 +1,5 @@ /* This program is licensed under the terms of GNU GPL v3 or (at your option) - * any later version. Copyright (C) 2020-2023 Страхиња Радић. + * any later version. Copyright (C) 2020-2024 Страхиња Радић. * See the file LICENSE for exact copyright and license details. */ #include "utf8.h" diff --git a/utf8.h b/utf8.h @@ -1,5 +1,5 @@ /* This program is licensed under the terms of GNU GPL v3 or (at your option) - * any later version. Copyright (C) 2020-2023 Страхиња Радић. + * any later version. Copyright (C) 2020-2024 Страхиња Радић. * See the file LICENSE for exact copyright and license details. */ #include <stdint.h> diff --git a/version.h.in b/version.h.in @@ -1,5 +1,5 @@ /* This program is licensed under the terms of GNU GPL v3 or (at your option) - * any later version. Copyright (C) 2020-2023 Страхиња Радић. + * any later version. Copyright (C) 2020-2024 Страхиња Радић. * See the file LICENSE for exact copyright and license details. */ #define PROGRAMNAME "table"