aboutsummaryrefslogtreecommitdiff
path: root/src/grab.c
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-01-20 18:53:21 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-01-20 18:53:21 +0100
commit99a3838242604a4cc888ea7f5019207cfb1d003a (patch)
tree9a5df14659271c7b1337e70a3de565ce6b15eaf9 /src/grab.c
parent0d3d39996597fcb9b8728f933c6c96ac2a1a8ff3 (diff)
Add the -s/--strip-newline flag
Diffstat (limited to 'src/grab.c')
-rw-r--r--src/grab.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/grab.c b/src/grab.c
index e709bfc..43d4cef 100644
--- a/src/grab.c
+++ b/src/grab.c
@@ -80,7 +80,7 @@ static bool xisspace(char);
static char *xstrchrnul(const char *, char);
static int filecnt, rv;
-static bool cflag, nflag, Uflag, zflag;
+static bool cflag, nflag, sflag, Uflag, zflag;
static bool fflag =
#if GIT_GRAB
true;
@@ -100,9 +100,9 @@ usage(const char *s)
{
fprintf(stderr,
#if GIT_GRAB
- "Usage: %s [-cnUz] pattern [glob ...]\n"
+ "Usage: %s [-s | -z] [-cnU] pattern [glob ...]\n"
#else
- "Usage: %s [-cfnUz] pattern [file ...]\n"
+ "Usage: %s [-s | -z] [-cfnU] pattern [file ...]\n"
#endif
" %s -h\n",
s, s);
@@ -115,15 +115,16 @@ main(int argc, char **argv)
int opt;
struct ops ops;
struct option longopts[] = {
- {"color", no_argument, 0, 'c'},
+ {"color", no_argument, nullptr, 'c'},
#if GIT_GRAB
- {"filenames", no_argument, 0, 'f'},
+ {"filenames", no_argument, nullptr, 'f'},
#endif
- {"help", no_argument, 0, 'h'},
- {"newline", no_argument, 0, 'n'},
- {"no-unicode", no_argument, 0, 'U'},
- {"zero", no_argument, 0, 'z'},
- {nullptr, 0, 0, 0 },
+ {"help", no_argument, nullptr, 'h'},
+ {"newline", no_argument, nullptr, 'n'},
+ {"strip-newline", no_argument, nullptr, 's'},
+ {"no-unicode", no_argument, nullptr, 'U'},
+ {"zero", no_argument, nullptr, 'z'},
+ {nullptr, 0, nullptr, 0 },
};
#if GIT_GRAB
@@ -131,9 +132,9 @@ main(int argc, char **argv)
size_t len;
ssize_t nr;
FILE *flist;
- const char *opts = "chnUz";
+ const char *opts = "chnsUz";
#else
- const char *opts = "cfhnUz";
+ const char *opts = "cfhnsUz";
#endif
argv[0] = basename(argv[0]);
@@ -155,6 +156,9 @@ main(int argc, char **argv)
case 'n':
nflag = true;
break;
+ case 's':
+ sflag = true;
+ break;
case 'U':
#if GRAB_DO_PCRE
Uflag = true;
@@ -173,6 +177,9 @@ main(int argc, char **argv)
}
}
+ if (sflag && zflag)
+ usage(argv[0]);
+
argc -= optind;
argv += optind;
filecnt = argc - 1;
@@ -441,7 +448,8 @@ putm(struct sv sv, const char *filename)
printf("%s%c", filename, zflag ? '\0' : ':');
}
fwrite(sv.p, 1, sv.len, stdout);
- putchar(zflag ? '\0' : '\n');
+ if (!(sflag && sv.p[sv.len - 1] == '\n'))
+ putchar(zflag ? '\0' : '\n');
}
bool