aboutsummaryrefslogtreecommitdiff
path: root/swaynag
diff options
context:
space:
mode:
authorNihal Jere <nihal@nihaljere.xyz>2022-03-17 13:00:08 -0500
committerSimon Ser <contact@emersion.fr>2022-05-07 15:57:18 +0200
commit5d924f2b12055761b8b155bce61925b6384f070b (patch)
treee1ad0f77c1f7f9600ad13f2d656570d99aad4e31 /swaynag
parenta7898637de43de1757f72393ef1caedf568f45e4 (diff)
swaynag: do error checking and rename read_from_stdin
read_from_stdin not only read from stdin, but trimming trailing newlines, so rename it to reflect this.
Diffstat (limited to 'swaynag')
-rw-r--r--swaynag/config.c34
1 files changed, 23 insertions, 11 deletions
diff --git a/swaynag/config.c b/swaynag/config.c
index b212a0c3..5ff00bdb 100644
--- a/swaynag/config.c
+++ b/swaynag/config.c
@@ -11,28 +11,40 @@
#include "util.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
-static char *read_from_stdin(void) {
- char *buffer = NULL;
- size_t buffer_len = 0;
- char *line = NULL;
- size_t line_size = 0;
- ssize_t nread;
- while ((nread = getline(&line, &line_size, stdin)) != -1) {
+static char *read_and_trim_stdin(void) {
+ char *buffer = NULL, *line = NULL;
+ size_t buffer_len = 0, line_size = 0;
+ while (1) {
+ ssize_t nread = getline(&line, &line_size, stdin);
+ if (nread == -1) {
+ if (feof(stdin)) {
+ break;
+ } else {
+ perror("getline");
+ goto freeline;
+ }
+ }
buffer = realloc(buffer, buffer_len + nread + 1);
if (!buffer) {
perror("realloc");
- return NULL;
+ goto freebuf;
}
- snprintf(&buffer[buffer_len], nread + 1, "%s", line);
+ memcpy(&buffer[buffer_len], line, nread + 1);
buffer_len += nread;
}
free(line);
- while (buffer && buffer[buffer_len - 1] == '\n') {
+ while (buffer_len && buffer[buffer_len - 1] == '\n') {
buffer[--buffer_len] = '\0';
}
return buffer;
+
+freeline:
+ free(line);
+freebuf:
+ free(buffer);
+ return NULL;
}
int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
@@ -222,7 +234,7 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
case 'l': // Detailed Message
if (swaynag) {
free(swaynag->details.message);
- swaynag->details.message = read_from_stdin();
+ swaynag->details.message = read_and_trim_stdin();
if (!swaynag->details.message) {
return EXIT_FAILURE;
}