]> git.pond.sub.org Git - empserver/commitdiff
(fname): New.
authorMarkus Armbruster <armbru@pond.sub.org>
Sat, 17 Nov 2007 09:08:57 +0000 (09:08 +0000)
committerMarkus Armbruster <armbru@pond.sub.org>
Sat, 17 Nov 2007 09:08:57 +0000 (09:08 +0000)
(doredir): Use it.  Check whether argument starts with '>'.  Simplify
convoluted logic.  Improve error messages.  Check value of fdopen().

src/client/servcmd.c

index 57e006bca6e6dc516ea61fe252d222a2adf38f52..40f28be6bb46107a567f2c249a45f7ebfc42751e 100644 (file)
@@ -36,6 +36,7 @@
 #include <config.h>
 
 #include <ctype.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -165,14 +166,23 @@ prompt(FILE *auxfi)
     }
 }
 
+static char *
+fname(char *s)
+{
+    char *beg, *end;
+
+    for (beg = s; isspace(*(unsigned char *)beg); beg++) ;
+    for (end = beg; !isspace(*(unsigned char *)end); end++) ;
+    *end = 0;
+    return beg;
+}
+
 /*
  * opens redir_fp if successful
  */
 static void
 doredir(char *p)
 {
-    char *how;
-    char *name;
     char *tag;
     int mode;
     int fd;
@@ -181,40 +191,41 @@ doredir(char *p)
        (void)fclose(redir_fp);
        redir_fp = NULL;
     }
-    how = p++;
-    if (*p && ((*p == '>') || (*p == '!')))
-       p++;
-    tag = gettag(p);
-    while (*p && isspace(*p))
-       p++;
-    name = p;
-    while (*p && !isspace(*p))
-       p++;
-    *p = 0;
-    if (tag == NULL) {
-       fprintf(stderr, "WARNING!  Server redirected output to file %s\n",
-               name);
+
+    if (*p++ != '>') {
+       fprintf(stderr, "WARNING!  Weird redirection %s", p);
        return;
     }
+
     mode = O_WRONLY | O_CREAT;
-    if (how[1] == '>')
+    if (*p == '>') {
        mode |= O_APPEND;
-    else if (how[1] == '!')
+       p++;
+    } else if (*p == '!') {
        mode |= O_TRUNC;
-    else
+       p++;
+    } else
        mode |= O_EXCL;
-    if (*name == 0) {
-       fprintf(stderr, "Null file name after redirect\n");
-       free(tag);
+
+    tag = gettag(p);
+    p = fname(p);
+    if (tag == NULL) {
+       fprintf(stderr, "WARNING!  Server redirected output to file %s\n",
+               p);
        return;
     }
-    if ((fd = open(name, mode, 0600)) < 0) {
-       fprintf(stderr, "Redirect open failed\n");
-       perror(name);
-    } else {
-       redir_fp = fdopen(fd, "w");
-    }
     free(tag);
+
+    if (*p == 0) {
+       fprintf(stderr, "Redirection lacks a file name\n");
+       return;
+    }
+    fd = open(p, mode, 0600);
+    redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
+    if (!redir_fp) {
+       fprintf(stderr, "Can't redirect to %s: %s\n",
+               p, strerror(errno));
+    }
 }
 
 /*