чување 29330f8f301c21a2448a14c7b4ef7879d46890b3
родитељ 5b72c0b002c22263e68e2ac585eb2315d4c773c4
Аутор: Страхиња Радић <sr@strahinja.org>
Датум: Mon, 29 Sep 2025 09:59:11 +0200
Clean up code; update email address and copyright year
Diffstat:
измењених датотека: 6, додавања: 51(+), брисања: 37(-)
diff --git a/README b/README
@@ -12,4 +12,3 @@ Why? Initially, to make formatting of text files in TLDP better.
Example?
$ reflow < /usr/share/doc/howto/txt/XFree86-HOWTO | fold | less
-
diff --git a/config.mk b/config.mk
@@ -1,10 +1,10 @@
#CC = cc
-CFLAGS = -Os -Wall -pedantic -std=c99
+CFLAGS = -Os -Wextra -Wall -pedantic -std=c99
CPPFLAGS = -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L \
- -D_XOPEN_SOURCE=700
+ -D_XOPEN_SOURCE=700 -DNDEBUG
# OpenBSD
#CPPFLAGS = -D_DEFAULT_SOURCE -D_POSIX_C_SOURCE=200809L \
-# -D_XOPEN_SOURCE=700 -D_BSD_SOURCE
+# -D_XOPEN_SOURCE=700 -D_BSD_SOURCE -DNDEBUG
INSTALL = install
LIBS =
SRC = reflow.c
diff --git a/defs.h b/defs.h
@@ -1,6 +1,6 @@
/* This program is licensed under the terms of GNU GPL v3 or (at your option)
- * any later version. Copyright (C) 2021-2024 Страхиња Радић.
+ * any later version. Copyright (C) 2021-2025 Страхиња Радић.
* See the file LICENSE for exact copyright and license details. */
-#define MAXBUF 4096
-#define MAXPATH 4096
+#define MAXBUF 4096
+#define MAXPATH 4096
diff --git a/reflow.1.in b/reflow.1.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) 2021-2024 Страхиња Радић.
+.\" any later version. Copyright (C) 2021-2025 Страхиња Радић.
.\" See the file LICENSE for exact copyright and license details.
.Dd %DATE%
.Dt REFLOW 1
@@ -38,8 +38,8 @@ Print program version and commit date.
.Sh EXIT STATUS
.Ex -std
.Sh AUTHORS
-.An Strahinya Radich Aq Mt contact@strahinja.org ,
-2021\-2024
+.An Strahinya Radich Aq Mt sr@strahinja.org ,
+2021\-2025
.Sh BUGS
Bugs can be reported using the ticket tracker at:
.Lk https://\:todo.sr.ht/\:~strahinja/\:text-tools
diff --git a/reflow.c b/reflow.c
@@ -1,7 +1,8 @@
/* This program is licensed under the terms of GNU GPL v3 or (at your option)
- * any later version. Copyright (C) 2021-2024 Страхиња Радић.
+ * any later version. Copyright (C) 2021-2025 Страхиња Радић.
* See the file LICENSE for exact copyright and license details. */
+#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
@@ -14,7 +15,7 @@
#define COPYRIGHT \
(" This program is licensed under the terms of GNU GPL v3" \
" or (at your option)\n" \
- " any later version. Copyright (C) 2021-2024 Strahinya Radich.\n" \
+ " any later version. Copyright (C) 2021-2025 Strahinya Radich.\n" \
" See the file LICENSE for exact copyright and license " \
"details.")
@@ -34,6 +35,7 @@ error(int code, char* format, ...)
{
char buf[MAXBUF];
va_list args;
+ assert(format != NULL);
va_start(args, format);
vsnprintf(buf, sizeof(buf), format, args);
va_end(args);
@@ -60,16 +62,20 @@ is_whitespace(char ch)
int
main(int argc, char** argv)
{
- size_t buffer_size = 0;
- char* buffer = NULL;
- char* pbuffer = NULL;
- size_t line_len = 0;
- char* line = calloc(MAXBUF, 1);
- char* pline = NULL;
- char* eol = NULL;
- FILE* input = NULL;
- int argn = 1;
- char* carg = NULL;
+ FILE* input = NULL;
+ char* buffer = NULL;
+ char* carg = NULL;
+ char* pbuffer = NULL;
+ char* line = NULL;
+ char* pline = NULL;
+ char* eol = NULL;
+ char* tempb = NULL;
+ ssize_t buffer_size = 0;
+ ssize_t line_len = 0;
+ ssize_t prev_size = 0;
+ int argn = 1;
+ int empty_line;
+ int initial_whitespace;
*filename = 0;
while (argn < argc)
@@ -95,6 +101,11 @@ main(int argc, char** argv)
if (!input)
return error(errno, "Cannot open file");
+ line = calloc(MAXBUF, 1);
+ if (!line)
+ exit(error(ENOMEM, "Memory allocation failed"));
+ prev_size = MAXBUF;
+
while (!feof(input))
{
if (!fgets(line, MAXBUF, input))
@@ -122,13 +133,14 @@ main(int argc, char** argv)
else
{
size_t pbuffer_start = pbuffer - buffer;
- buffer = realloc(buffer, buffer_size + 1);
- if (!buffer)
+ tempb = realloc(buffer, buffer_size + 1);
+ if (!tempb)
exit(error(ENOMEM, "Memory allocation failed"));
+ buffer = tempb;
pbuffer = buffer + pbuffer_start;
}
- pline = line;
- int initial_whitespace = is_whitespace(*pline);
+ pline = line;
+ initial_whitespace = is_whitespace(*pline);
while (*pline)
{
if (initial_whitespace)
@@ -149,13 +161,11 @@ main(int argc, char** argv)
if (pbuffer)
*pbuffer = 0;
- free(line);
-
- if (!buffer)
- return 0;
+ assert((buffer != NULL) && (pbuffer != NULL));
- int empty_line = 0;
- pbuffer = buffer;
+ empty_line = 0;
+ pbuffer = buffer;
+ tempb = NULL;
while (*pbuffer)
{
eol = strchr(pbuffer, '\n');
@@ -169,13 +179,17 @@ main(int argc, char** argv)
continue;
}
empty_line = line_len == 0;
- line = calloc(line_len + 1, 1);
- if (!line)
- exit(error(ENOMEM, "Memory allocation failed"));
+ if (!line || line_len + 1 > prev_size)
+ {
+ tempb = realloc(line, line_len + 1);
+ if (!tempb)
+ exit(error(ENOMEM, "Memory allocation failed"));
+ prev_size = line_len + 1;
+ line = tempb;
+ }
strncpy(line, pbuffer, line_len);
*(line + line_len) = 0;
printf("%s", line);
- free(line);
if (*(pbuffer + line_len + 1) == '\n' && !empty_line)
printf("\n\n");
else if (!empty_line
@@ -188,6 +202,7 @@ main(int argc, char** argv)
}
printf("\n");
free(buffer);
+ free(line);
return 0;
}
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) 2021-2024 Страхиња Радић.
+ * any later version. Copyright (C) 2021-2025 Страхиња Радић.
* See the file LICENSE for exact copyright and license details. */
#define PROGRAMNAME "reflow"