new(main.c): Func to remove trailing newline

Make use of remove newline function

document the remove_trailing new line
This commit is contained in:
Sau P 2024-11-27 22:04:19 +00:00 committed by anon
parent c88a2aaba4
commit 741921d60c

View File

@ -170,6 +170,17 @@ void myexit(int sig) {
exit(sig);
}
/* Remove a singular newline from the end of a string (if any). */
static inline
void remove_trailing_newline(char *buf, size_t len) {
if ((len > 0) &&
(buf[len - 1] == '\n')) {
buf[len - 1] = '\0';
}
}
static inline
void linemode_event_loop(void) {
if (*input_line != '\0') { /* do any optional search */
@ -200,7 +211,8 @@ void linemode_event_loop(void) {
fflush(stdout);
if (fgets(buf, sizeof(buf), stdin) == NULL) { myexit(0); }
/* remove any trailing newline character */
if (*(s = buf + strlen(buf) - 1) == '\n') { *s = '\0'; }
remove_trailing_newline(buf, strlen(buf));
switch (*buf) {
case ASCII_DIGIT:
field = *buf - '0';