[tyndur-devel] [PATCH 2/2] libc: fgets-Rueckgabe enthaelt Zeilenumbruch
Kevin Wolf ·
! libc: Im von fgets() zurueckgegebenen String ist auch das abschliessende Leerzeichen enthalten. Signed-off-by: Kevin Wolf <kevin at tyndur.org> --- src/modules/c/shell/shell.c | 7 +++++++ src/modules/init/init.c | 6 ++++++ src/modules/lib/posix/socket.c | 6 ++++++ src/modules/lib/stdlibc/file.c | 7 +++++-- src/modules/servmgr/config.c | 10 ++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/modules/c/shell/shell.c b/src/modules/c/shell/shell.c index cb5de50..66f91c6 100644 --- a/src/modules/c/shell/shell.c +++ b/src/modules/c/shell/shell.c @@ -307,6 +307,7 @@ int shell_do_cmd(void) bool shell_script(const char* path) { FILE* script = fopen(path, "r"); + size_t i; if (!script) { return FALSE; @@ -314,6 +315,12 @@ bool shell_script(const char* path) while (feof(script) == 0) { fgets(shell_command_buffer, COMMAND_BUFFER_SIZE, script); + + i = strlen(shell_command_buffer); + if ((i > 0) && (shell_command_buffer[i - 1] == '\n')) { + shell_command_buffer[i - 1] = '\0'; + } + shell_do_cmd(); } diff --git a/src/modules/init/init.c b/src/modules/init/init.c index 80e7af0..03d6b98 100644 --- a/src/modules/init/init.c +++ b/src/modules/init/init.c @@ -531,10 +531,16 @@ pid_t start_program(char* path, char* args, pid_t parent_pid) char* new_path; char* new_args; int ret; + size_t i; fgets(interpreter, 256, f); fclose(f); + i = strlen(interpreter); + if ((i > 0) && (interpreter[i - 1] == '\n')) { + interpreter[i - 1] = '\0'; + } + if (strlen(interpreter) < 2) { return FALSE; } diff --git a/src/modules/lib/posix/socket.c b/src/modules/lib/posix/socket.c index f0b97e1..0616c2b 100644 --- a/src/modules/lib/posix/socket.c +++ b/src/modules/lib/posix/socket.c @@ -261,6 +261,7 @@ int accept(int sock, struct sockaddr* address, socklen_t* address_len) struct lostio_internal_file* conn; struct socket* socket = get_socket(sock); + size_t i; if (socket == NULL) { errno = EBADF; @@ -277,6 +278,11 @@ int accept(int sock, struct sockaddr* address, socklen_t* address_len) return -1; } + i = strlen(buf); + if ((i > 0) && (buf[i - 1] == '\n')) { + buf[i - 1] = '\0'; + } + clientsocket = calloc(1, sizeof(*clientsocket)); clientsocket->sa_family = AF_INET; clientsock = create_socket(socket); diff --git a/src/modules/lib/stdlibc/file.c b/src/modules/lib/stdlibc/file.c index 7d5e1a8..3157247 100644 --- a/src/modules/lib/stdlibc/file.c +++ b/src/modules/lib/stdlibc/file.c @@ -362,12 +362,15 @@ char* fgets(char* dest, int length, FILE *io_res) break; } else if (c == EOF) { continue; - } else if (c == '\n') { - break; } *dest = (char) c; dest++; + + if (c == '\n') { + i++; + break; + } } *dest = '\0'; diff --git a/src/modules/servmgr/config.c b/src/modules/servmgr/config.c index c722e41..b28ae8a 100644 --- a/src/modules/servmgr/config.c +++ b/src/modules/servmgr/config.c @@ -151,6 +151,11 @@ static void config_parse_deps(const char* path, // FIXME: Das koennte man noch etwas eleganter loesen ;-) while (fgets(buffer, sizeof(buffer), f)) { + size_t i = strlen(buffer); + if ((i > 0) && (buffer[i - 1] == '\n')) { + buffer[i - 1] = '\0'; + } + dep = config_service_get(buffer); if (dep == NULL) { printf("servmgr: Service %s haengt ab von %s; Dieser ist aber " @@ -184,6 +189,11 @@ static void config_parse_conf(const char* path, // FIXME: Das koennte man noch etwas eleganter loesen ;-) while (fgets(buffer, sizeof(buffer), f)) { + size_t i = strlen(buffer); + if ((i > 0) && (buffer[i - 1] == '\n')) { + buffer[i - 1] = '\0'; + } + if (!strcmp(buffer, "nowait")) { conf_serv->conf.wait = NOWAIT; } else if (!strcmp(buffer, "waitterminate")) { -- 1.5.6.5
Antworten
- [tyndur-devel] [PATCH 2/2] libc: fgets-Rueckgabe enthaelt Zeilenumbruch · Antoine Kaufmann