liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
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
/*
 * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
 *
 * 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.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
#define WRITE_STRING(fd, string) if (write(fd, string, sizeof(string)-1)) {}
 
int main(int argc, char *argv[])
{
   int i, pos = 0;
   char *args[argc];
   char *stdin_path = NULL, *stdout_path = NULL, *stderr_path = NULL;
   int stderr_fd = 0;
 
   if (argc <= 1) {
       fprintf(stderr, "%s: Takes at least one agrument!\n", argv[0]);
       return 1;
   }
 
   for (i = 1; i < argc; i++) {
 
       if (argv[i][0] == '>') {
           if (argv[i][1]) {
               stdout_path = argv[i]+1;
           } else {
               if (++i >= argc) {
                   fprintf(stderr,
                           "%s: Missing filename after >\n",
                           argv[0]);
                   return 1;
               }
               stdout_path = argv[i];
           }
           continue;
       }
 
       if (argv[i][0] == '<') {
           if (argv[i][1]) {
               stdin_path = argv[i]+1;
           } else {
               if (++i >= argc) {
                   fprintf(stderr,
                           "%s: Missing filename after <\n",
                           argv[0]);
                   return 1;
               }
               stdin_path = argv[i];
           }
           continue;
       }
 
       if (argv[i][0] == '2' && argv[i][1] == '>') {
           if (argv[i][2]) {
               stderr_path = argv[i]+2;
           } else {
               if (++i >= argc) {
                   fprintf(stderr,
                           "%s: Missing filename after 2>\n",
                           argv[0]);
                   return 1;
               }
               stderr_path = argv[i];
           }
           continue;
       }
 
       args[pos++] = argv[i];
   }
 
   args[pos] = NULL;
 
   if (stdin_path) {
       if (close(0)) {
           fprintf(stderr, "%s: Failed to close stdin: %s\n",
               argv[0], strerror(errno));
           return 1;
       }
       if (open(stdin_path, O_RDONLY) < 0) {
           fprintf(stderr,
                   "%s: Failed to open '%s' for reading: %s\n",
               argv[0], stdin_path, strerror(errno));
           return 1;
       }
   }
 
   if (stdout_path) {
       if (close(1)) {
           fprintf(stderr, "%s: Failed to close stdout: %s\n",
               argv[0], strerror(errno));
           return 1;
       }
       if (open(stdout_path, O_CREAT|O_WRONLY|O_TRUNC, 0777) < 0) {
           fprintf(stderr,
                   "%s: Failed to open '%s' for writing: %s\n",
               argv[0], stdin_path, strerror(errno));
           return 1;
       }
   }
 
   if (stderr_path) {
       int fd = open(stderr_path, O_CREAT|O_WRONLY|O_TRUNC, 0777);
       int stderr_fd = dup(2);
 
       if (stderr_fd < 0) {
           fprintf(stderr, "%s: Failed to dup() stderr: %s\n",
               argv[0], strerror(errno));
           return 1;
       }
 
       if (fd < 0) {
           fprintf(stderr,
                   "%s: Failed to open '%s' for writing: %s\n",
               argv[0], stdin_path, strerror(errno));
           return 1;
       }
 
       if (dup2(fd, 2) < 0) {
           fprintf(stderr, "%s: Failed to dup2 stderr: %s\n",
               argv[0], strerror(errno));
           WRITE_STRING(stderr_fd, "Failed to dup2 stderr\n");
           return 1;
       }
   }
 
   execvp(argv[1], args);
 
   /* Fall back to shell if command wasn't found */
   FILE *sin = popen("/bin/sh", "w");
 
   if (!sin) {
       if (stderr_fd) {
           WRITE_STRING(stderr_fd, "Failed to popen /bin/sh\n");
       } else {
           fprintf(stderr, "%s: Failed to popen /bin/sh: %s\n",
               argv[0], strerror(errno));
       }
       return 1;
   }
 
   //TODO: Should we escape args?
   for (i = 0; args[i]; i++)
       fprintf(sin, "%s ", args[i]);
 
   return !!pclose(sin);
}