(fname): New.
(doredir): Use it. Check whether argument starts with '>'. Simplify convoluted logic. Improve error messages. Check value of fdopen().
This commit is contained in:
parent
d01aa85577
commit
17d6997d4e
1 changed files with 40 additions and 29 deletions
|
@ -36,6 +36,7 @@
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.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
|
* opens redir_fp if successful
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
doredir(char *p)
|
doredir(char *p)
|
||||||
{
|
{
|
||||||
char *how;
|
|
||||||
char *name;
|
|
||||||
char *tag;
|
char *tag;
|
||||||
int mode;
|
int mode;
|
||||||
int fd;
|
int fd;
|
||||||
|
@ -181,40 +191,41 @@ doredir(char *p)
|
||||||
(void)fclose(redir_fp);
|
(void)fclose(redir_fp);
|
||||||
redir_fp = NULL;
|
redir_fp = NULL;
|
||||||
}
|
}
|
||||||
how = p++;
|
|
||||||
if (*p && ((*p == '>') || (*p == '!')))
|
if (*p++ != '>') {
|
||||||
|
fprintf(stderr, "WARNING! Weird redirection %s", p);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mode = O_WRONLY | O_CREAT;
|
||||||
|
if (*p == '>') {
|
||||||
|
mode |= O_APPEND;
|
||||||
p++;
|
p++;
|
||||||
|
} else if (*p == '!') {
|
||||||
|
mode |= O_TRUNC;
|
||||||
|
p++;
|
||||||
|
} else
|
||||||
|
mode |= O_EXCL;
|
||||||
|
|
||||||
tag = gettag(p);
|
tag = gettag(p);
|
||||||
while (*p && isspace(*p))
|
p = fname(p);
|
||||||
p++;
|
|
||||||
name = p;
|
|
||||||
while (*p && !isspace(*p))
|
|
||||||
p++;
|
|
||||||
*p = 0;
|
|
||||||
if (tag == NULL) {
|
if (tag == NULL) {
|
||||||
fprintf(stderr, "WARNING! Server redirected output to file %s\n",
|
fprintf(stderr, "WARNING! Server redirected output to file %s\n",
|
||||||
name);
|
p);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mode = O_WRONLY | O_CREAT;
|
|
||||||
if (how[1] == '>')
|
|
||||||
mode |= O_APPEND;
|
|
||||||
else if (how[1] == '!')
|
|
||||||
mode |= O_TRUNC;
|
|
||||||
else
|
|
||||||
mode |= O_EXCL;
|
|
||||||
if (*name == 0) {
|
|
||||||
fprintf(stderr, "Null file name after redirect\n");
|
|
||||||
free(tag);
|
free(tag);
|
||||||
|
|
||||||
|
if (*p == 0) {
|
||||||
|
fprintf(stderr, "Redirection lacks a file name\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ((fd = open(name, mode, 0600)) < 0) {
|
fd = open(p, mode, 0600);
|
||||||
fprintf(stderr, "Redirect open failed\n");
|
redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
|
||||||
perror(name);
|
if (!redir_fp) {
|
||||||
} else {
|
fprintf(stderr, "Can't redirect to %s: %s\n",
|
||||||
redir_fp = fdopen(fd, "w");
|
p, strerror(errno));
|
||||||
}
|
}
|
||||||
free(tag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue