File: redir.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,244 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (168 lines) | stat: -rw-r--r-- 2,987 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* $Id: redir.c,v 1.4 2001/05/12 18:44:37 mhe Exp $
 *
 * redir.h --
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See COPYING for more details.
 */

#include "syshdr.h"

static FILE *redir_fp = 0;
static bool ispipe = false;
static int oldout = -1;
static int olderr = -1;

bool reject_ampersand(char *cmd)
{
	char *e, *f;

	/* look for and reject '&' in command
	 * (asynchronous execution)
	 * allow '&&'
	 */
	e = cmd;
	while(e && *e && (f=strchr(e, '&')) != 0) {
		if(f == 0)
			break;;
		if(f[1] != '&') {
			fprintf(stderr, _("Asynchronous mode NOT supported"
							  " (found '&' in command)\n"));
			return true;
		} else {
			e = f+1;
			if(!*e)
				break;
			e++;
		}
	}
	return false;
}

typedef enum {REDIR_PIPE, REDIR_WRITE, REDIR_APPEND} redir_type;

int open_redirection(char *cmd)
{
	redir_type type;
	bool errtoo = false;

	redir_fp = 0;

	if(cmd[0] == '|') {
		type = REDIR_PIPE;
	} else if(cmd[0] == '>') {
		if(cmd[1] == '>') {
			cmd++;
			type = REDIR_APPEND;
		} else
			type = REDIR_WRITE;
	} else
		return -1;

	if(!*cmd) {
		fprintf(stderr, _("missing redirection argument\n"));
		return -1;
	}

	cmd++;
	if(*cmd == '&') {
		errtoo = true;
		cmd++;
	}

	while(*cmd == ' ')
		cmd++;

	if(!*cmd) {
		fprintf(stderr, _("missing redirection argument\n"));
		return -1;
	}

	if(reject_ampersand(cmd))
		return -1;

	if(type == REDIR_PIPE) {
		redir_fp = popen(cmd, "w");
		ispipe = true;
	} else  {
		redir_fp = fopen(cmd, type == REDIR_WRITE ? "w" : "a");
		ispipe = false;
	}

	if(redir_fp == 0) {
		perror(cmd);
		return -1;
	}

	oldout = dup(fileno(stdout));
	if(oldout == -1) {
		perror("dup");
		fclose(redir_fp);
		redir_fp = 0;
		return -1;
	}

	if(dup2(fileno(redir_fp), fileno(stdout)) == -1) {
		perror("dup2");
		fclose(redir_fp);
		redir_fp = 0;
		return -1;
	}

	if(errtoo) {
		olderr = dup(fileno(stderr));
		if(olderr == -1) {
			perror("dup");
			fclose(redir_fp);
			if(dup2(oldout, fileno(stdout)) == -1)
				perror("dup2");
			redir_fp = 0;
			oldout = -1;
			olderr = -1;
			return -1;
		}

		if(dup2(fileno(redir_fp), fileno(stderr)) == -1) {
			perror("dup2");
			fclose(redir_fp);
			redir_fp = 0;
			if(dup2(oldout, fileno(stdout)) == -1)
				perror("dup2");
			oldout = -1;
			olderr = -1;
			return -1;
		}
	}
	return 0;
}

int close_redirection(void)
{
	if(redir_fp == 0)
		return -1;

	if(oldout == -1)
		fprintf(stderr, _("Error, cannot restore stdout!\n"));
	else {
		if(dup2(oldout, fileno(stdout)) == -1)
			perror("dup2");
		oldout = -1;
	}
	if(olderr != -1) {
		if(dup2(olderr, fileno(stderr)) == -1)
			perror("dup2");
		olderr = -1;
	}

	if(ispipe)
		pclose(redir_fp);
	else
		fclose(redir_fp);
	redir_fp = 0;
	return 0;
}