• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (!strcmp(args[0], "cd") || !strcmp(args[0], "pushd") ||
81 		!strcmp(args[0], "popd")) {
82 		fprintf(stderr, "\"%s %s\" has no effect on parent shell\n",
83 			argv[0], args[0]);
84 		return 1;
85 	}
86 
87 	if (stdin_path) {
88 		if (close(0)) {
89 			fprintf(stderr, "%s: Failed to close stdin: %s\n",
90 				argv[0], strerror(errno));
91 			return 1;
92 		}
93 		if (open(stdin_path, O_RDONLY) < 0) {
94 			fprintf(stderr,
95 			        "%s: Failed to open '%s' for reading: %s\n",
96 				argv[0], stdin_path, strerror(errno));
97 			return 1;
98 		}
99 	}
100 
101 	if (stdout_path) {
102 		if (close(1)) {
103 			fprintf(stderr, "%s: Failed to close stdout: %s\n",
104 				argv[0], strerror(errno));
105 			return 1;
106 		}
107 		if (open(stdout_path, O_CREAT|O_WRONLY|O_TRUNC, 0777) < 0) {
108 			fprintf(stderr,
109 			        "%s: Failed to open '%s' for writing: %s\n",
110 				argv[0], stdout_path, strerror(errno));
111 			return 1;
112 		}
113 	}
114 
115 	if (stderr_path) {
116 		int fd = open(stderr_path, O_CREAT|O_WRONLY|O_TRUNC, 0777);
117 		int stderr_fd = dup(2);
118 
119 		if (stderr_fd < 0) {
120 			fprintf(stderr, "%s: Failed to dup() stderr: %s\n",
121 				argv[0], strerror(errno));
122 			return 1;
123 		}
124 
125 		if (fd < 0) {
126 			fprintf(stderr,
127 			        "%s: Failed to open '%s' for writing: %s\n",
128 				argv[0], stderr_path, strerror(errno));
129 			return 1;
130 		}
131 
132 		if (dup2(fd, 2) < 0) {
133 			fprintf(stderr, "%s: Failed to dup2 stderr: %s\n",
134 				argv[0], strerror(errno));
135 			WRITE_STRING(stderr_fd, "Failed to dup2 stderr\n");
136 			return 1;
137 		}
138 	}
139 
140 	execvp(args[0], args);
141 
142 	/* Fall back to shell if command wasn't found */
143 	FILE *sin = popen("/bin/sh", "w");
144 
145 	if (!sin) {
146 		if (stderr_fd) {
147 			WRITE_STRING(stderr_fd, "Failed to popen /bin/sh\n");
148 		} else {
149 			fprintf(stderr, "%s: Failed to popen /bin/sh: %s\n",
150 				argv[0], strerror(errno));
151 		}
152 		return 1;
153 	}
154 
155 	//TODO: Should we escape args?
156 	for (i = 0; args[i]; i++)
157 		fprintf(sin, "%s ", args[i]);
158 
159 	return !!pclose(sin);
160 }
161