error managment
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 13 May 2024 16:55:40 +0000 (18:55 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 13 May 2024 16:55:40 +0000 (18:55 +0200)
cmd.c
cmore.c

diff --git a/cmd.c b/cmd.c
index 95d1718134a88385a243184a670eba0605ba68d7..7c3054af40d0adba555d63cd68fec6c1ce53467b 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "debug.h"
 
@@ -33,7 +34,10 @@ char *exec_cmd (char *cmd)
     char *buffer = NULL;
 
     VERBOSE (DEBUG, fprintf (stdout, "exec command: %s\n", cmd));
+    int saved_stderr = dup (STDERR_FILENO);
+    close (STDERR_FILENO);
     FILE *fp = popen (cmd, "r");
+    dup2 (saved_stderr, STDERR_FILENO);
     if (fp != NULL) {
         buffer = _read_stream (fp);
         status = pclose(fp);
diff --git a/cmore.c b/cmore.c
index 475774b20fe35e5b7f7ca4283ffda6702519b256..460814bf0b2ad476751349eb0f2a9db1c3db878e 100644 (file)
--- a/cmore.c
+++ b/cmore.c
@@ -20,6 +20,8 @@ char *command = NULL;
 char **lines = NULL;
 int second = 0;
 
+#define MAX_LENGTH 48
+
 void display (char **lines, int xmax, int ymax, int keep_skip)
 {
     int stop = 0;
@@ -134,9 +136,10 @@ void process (void)
     }
 
     if (buffer == NULL) {
-        cleanup ();
-        VERBOSE (ERROR, fprintf (stderr, "%s: %s\n", command ? "can't execute:" : input ? "can't load:" : "can't read", command ? command : input ? input : "stdin"));
-        exit (1);
+        char msg[MAX_LENGTH + 13 + 2 + 1] = {0};
+        sprintf (msg, "%s: %s", command ? "can't execute" : input ? "can't load" : "can't read", command ? command : input ? input : "stdin");
+        errormsg (msg);
+        return;
     }
 
     free_lines (lines);
@@ -183,10 +186,14 @@ void about (void)
 
 void chcmd (void)
 {
-    char buffer[48 + 1] = {0};
+    char buffer[MAX_LENGTH + 1] = {0};
     char *desc[] = {"New command", NULL};
     char *buf[] = {buffer, NULL};
-    int c = getstrings (desc, buf, 48);
+
+    if (command) {
+        strncpy (buffer, command, MAX_LENGTH);
+    }
+    int c = getstrings (desc, buf, MAX_LENGTH);
 
     if ((c == '\n') && (strlen (buffer) > 0)) {
         free (command);
@@ -199,10 +206,14 @@ void chcmd (void)
 
 void chfile (void)
 {
-    char buffer[48 + 1] = {0};
+    char buffer[MAX_LENGTH + 1] = {0};
     char *desc[] = {"New file", NULL};
     char *buf[] = {buffer, NULL};
-    int c = getstrings (desc, buf, 48);
+
+    if (input) {
+        strncpy (buffer, input, MAX_LENGTH);
+    }
+    int c = getstrings (desc, buf, MAX_LENGTH);
 
     if ((c == '\n') && (strlen (buffer) > 0)) {
         free (command);