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.
master
Nihal Jere 3 years ago committed by Simon Ser
parent a7898637de
commit 5d924f2b12

@ -11,28 +11,40 @@
#include "util.h" #include "util.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h"
static char *read_from_stdin(void) { static char *read_and_trim_stdin(void) {
char *buffer = NULL; char *buffer = NULL, *line = NULL;
size_t buffer_len = 0; size_t buffer_len = 0, line_size = 0;
char *line = NULL; while (1) {
size_t line_size = 0; ssize_t nread = getline(&line, &line_size, stdin);
ssize_t nread; if (nread == -1) {
while ((nread = getline(&line, &line_size, stdin)) != -1) { if (feof(stdin)) {
break;
} else {
perror("getline");
goto freeline;
}
}
buffer = realloc(buffer, buffer_len + nread + 1); buffer = realloc(buffer, buffer_len + nread + 1);
if (!buffer) { if (!buffer) {
perror("realloc"); perror("realloc");
return NULL; goto freebuf;
} }
snprintf(&buffer[buffer_len], nread + 1, "%s", line); memcpy(&buffer[buffer_len], line, nread + 1);
buffer_len += nread; buffer_len += nread;
} }
free(line); free(line);
while (buffer && buffer[buffer_len - 1] == '\n') { while (buffer_len && buffer[buffer_len - 1] == '\n') {
buffer[--buffer_len] = '\0'; buffer[--buffer_len] = '\0';
} }
return buffer; return buffer;
freeline:
free(line);
freebuf:
free(buffer);
return NULL;
} }
int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, 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 case 'l': // Detailed Message
if (swaynag) { if (swaynag) {
free(swaynag->details.message); free(swaynag->details.message);
swaynag->details.message = read_from_stdin(); swaynag->details.message = read_and_trim_stdin();
if (!swaynag->details.message) { if (!swaynag->details.message) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }

Loading…
Cancel
Save