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