dtree

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

чување 5a6496462da0d99eb34eb45f44f23e85f632324f
родитељ 456cfb75132e3a851f718e561feb97ff6298518f
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Sun, 10 Mar 2024 19:33:21 +0100

Add pledge(2)/unveil(2) calls; add strlcpy and ctrlcat support

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

Diffstat:
Mconfig.mk | 4++--
Mdtree.c | 29+++++++++++++++++++++++++++--
Astrlcat.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astrlcpy.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
измењених датотека: 4, додавања: 132(+), брисања: 4(-)

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 = dtree.c utf8.c +SRC = dtree.c strlcat.c strlcpy.c utf8.c HEADERS = utf8.h version.h -OBJS = dtree.o utf8.o +OBJS = dtree.o strlcat.o strlcpy.o utf8.o PREFIX = /usr/local PROG = dtree diff --git a/dtree.c b/dtree.c @@ -36,6 +36,8 @@ void insert_node(struct Node** root, const int level, const char* label); void print_indent(void); void print_node(const struct Node* node); void set_node(struct Node* node, const int level, const char* label); +size_t strlcat(char* dst, const char* src, size_t dsize); +size_t strlcpy(char* dst, const char* src, size_t dsize); int string_to_symbol_set(const char* s); void usage(void); void version(void); @@ -243,16 +245,19 @@ print_node(const struct Node* node) void set_node(struct Node* node, const int level, const char* label) { + size_t size; + size = strlen(label) + 1; if (!node->label) { - node->label = (char*)calloc(1, strlen(label) + 1); + node->label = (char*)calloc(1, size); if (!node->label) { perror("calloc"); exit(error(1, "Cannot alloc")); } } - strcpy(node->label, label); + if (strlcpy(node->label, label, size) >= size) + exit(error(1, "strlcpy:%d: Overflow", __LINE__)); node->level = level; } @@ -308,6 +313,14 @@ main(int argc, char** argv) int ch; FILE* input = NULL; +#ifdef __OpenBSD__ + if (pledge("stdio rpath unveil", NULL) < 0) + { + perror(PROGRAMNAME ": pledge"); + exit(1); + } +#endif + if ((ch = getopt(argc, argv, ":hs:v")) != -1) { switch (ch) @@ -338,6 +351,18 @@ main(int argc, char** argv) input = stdin; else { +#ifdef __OpenBSD__ + if (unveil(argv[optind], "r") < 0) + { + perror(PROGRAMNAME ": unveil"); + exit(1); + } + if (unveil(NULL, NULL) < 0) + { + perror(PROGRAMNAME ": unveil"); + exit(1); + } +#endif if (!(input = fopen(argv[optind], "r"))) { perror("fopen"); diff --git a/strlcat.c b/strlcat.c @@ -0,0 +1,54 @@ +/* $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 @@ -0,0 +1,49 @@ +/* $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 */ +}