Hello everyone,
I personally often use 'fold' to break up some long lines, which works
well. Lately I have been in the need to cut of a line at a specific
length - removing the rest of the line. I made a small patch for 'fold'
to do just that. The line gets cut off at WIDTH (specified via -w WIDTH)
and three dots will be printed for a more appealing output. The new
option added is '-c' for 'cut'.
I checked out the newest code via Git and compiled 'fold' with the patch
attached to this mail. (No errors/warnings were output).
Attached you will find the patch for 'fold'.
Apologies if there is something missing/wrong - Never contributed to any
GNU software before. :)
Greetings
Julian "jhx"
--- fold.c.orig 2023-02-11 15:34:31.662689610 +0100
+++ fold.c 2023-02-11 15:36:18.567858338 +0100
@@ -41,14 +41,18 @@
/* If nonzero, count bytes, not column positions. */
static bool count_bytes;
+/* If nonzero, cut line off, don't break */
+static bool cut_line;
+
/* If nonzero, at least one of the files we read was standard input. */
static bool have_read_stdin;
-static char const shortopts[] = "bsw:0::1::2::3::4::5::6::7::8::9::";
+static char const shortopts[] = "bcsw:0::1::2::3::4::5::6::7::8::9::";
static struct option const longopts[] =
{
{"bytes", no_argument, NULL, 'b'},
+ {"cut", no_argument, NULL, 'c'},
{"spaces", no_argument, NULL, 's'},
{"width", required_argument, NULL, 'w'},
{GETOPT_HELP_OPTION_DECL},
@@ -206,6 +210,20 @@
continue;
}
+ if (cut_line)
+ {
+ /* Print three 'dots' at the end */
+ for(int i = 0; i < 3; i++)
+ line_out[offset_out++] = '.';
+
+ /* Read until the end of the line so the next line can be read */
+ while((c = getc (istream)) != '\n');
+
+ fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
+ column = offset_out = 0;
+ goto rescan;
+ }
+
line_out[offset_out++] = '\n';
fwrite (line_out, sizeof (char), (size_t) offset_out, stdout);
column = offset_out = 0;
@@ -264,6 +282,10 @@
count_bytes = true;
break;
+ case 'c': /* Cut off line */
+ cut_line = true;
+ break;
+
case 's': /* Break at word boundaries. */
break_spaces = true;
break;
@@ -307,4 +329,3 @@
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
-