1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <errno.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13
14 #define WRITE_STRING(fd, string) if (write(fd, string, sizeof(string)-1)) {}
15
main(int argc,char * argv[])16 int main(int argc, char *argv[])
17 {
18 int i, pos = 0;
19 char *args[argc];
20 char *stdin_path = NULL, *stdout_path = NULL, *stderr_path = NULL;
21 int stderr_fd = 0;
22
23 if (argc <= 1) {
24 fprintf(stderr, "%s: Takes at least one agrument!\n", argv[0]);
25 return 1;
26 }
27
28 for (i = 1; i < argc; i++) {
29
30 if (argv[i][0] == '>') {
31 if (argv[i][1]) {
32 stdout_path = argv[i]+1;
33 } else {
34 if (++i >= argc) {
35 fprintf(stderr,
36 "%s: Missing filename after >\n",
37 argv[0]);
38 return 1;
39 }
40 stdout_path = argv[i];
41 }
42 continue;
43 }
44
45 if (argv[i][0] == '<') {
46 if (argv[i][1]) {
47 stdin_path = argv[i]+1;
48 } else {
49 if (++i >= argc) {
50 fprintf(stderr,
51 "%s: Missing filename after <\n",
52 argv[0]);
53 return 1;
54 }
55 stdin_path = argv[i];
56 }
57 continue;
58 }
59
60 if (argv[i][0] == '2' && argv[i][1] == '>') {
61 if (argv[i][2]) {
62 stderr_path = argv[i]+2;
63 } else {
64 if (++i >= argc) {
65 fprintf(stderr,
66 "%s: Missing filename after 2>\n",
67 argv[0]);
68 return 1;
69 }
70 stderr_path = argv[i];
71 }
72 continue;
73 }
74
75 args[pos++] = argv[i];
76 }
77
78 args[pos] = NULL;
79
80 if (stdin_path) {
81 if (close(0)) {
82 fprintf(stderr, "%s: Failed to close stdin: %s\n",
83 argv[0], strerror(errno));
84 return 1;
85 }
86 if (open(stdin_path, O_RDONLY) < 0) {
87 fprintf(stderr,
88 "%s: Failed to open '%s' for reading: %s\n",
89 argv[0], stdin_path, strerror(errno));
90 return 1;
91 }
92 }
93
94 if (stdout_path) {
95 if (close(1)) {
96 fprintf(stderr, "%s: Failed to close stdout: %s\n",
97 argv[0], strerror(errno));
98 return 1;
99 }
100 if (open(stdout_path, O_CREAT|O_WRONLY|O_TRUNC, 0777) < 0) {
101 fprintf(stderr,
102 "%s: Failed to open '%s' for writing: %s\n",
103 argv[0], stdout_path, strerror(errno));
104 return 1;
105 }
106 }
107
108 if (stderr_path) {
109 int fd = open(stderr_path, O_CREAT|O_WRONLY|O_TRUNC, 0777);
110 int stderr_fd = dup(2);
111
112 if (stderr_fd < 0) {
113 fprintf(stderr, "%s: Failed to dup() stderr: %s\n",
114 argv[0], strerror(errno));
115 return 1;
116 }
117
118 if (fd < 0) {
119 fprintf(stderr,
120 "%s: Failed to open '%s' for writing: %s\n",
121 argv[0], stderr_path, strerror(errno));
122 return 1;
123 }
124
125 if (dup2(fd, 2) < 0) {
126 fprintf(stderr, "%s: Failed to dup2 stderr: %s\n",
127 argv[0], strerror(errno));
128 WRITE_STRING(stderr_fd, "Failed to dup2 stderr\n");
129 return 1;
130 }
131 }
132
133 execvp(argv[1], args);
134
135 /* Fall back to shell if command wasn't found */
136 FILE *sin = popen("/bin/sh", "w");
137
138 if (!sin) {
139 if (stderr_fd) {
140 WRITE_STRING(stderr_fd, "Failed to popen /bin/sh\n");
141 } else {
142 fprintf(stderr, "%s: Failed to popen /bin/sh: %s\n",
143 argv[0], strerror(errno));
144 }
145 return 1;
146 }
147
148 //TODO: Should we escape args?
149 for (i = 0; args[i]; i++)
150 fprintf(sin, "%s ", args[i]);
151
152 return !!pclose(sin);
153 }
154