-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredir-sol.c
44 lines (36 loc) · 1018 Bytes
/
redir-sol.c
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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char **argv) {
// Expecting 2 arguments.
// The first is a program to execute
// The second is the name of a file to redirect input from
// open the file that is in argv[2]
// Use "open" instead of "fopen" to get the file descriptor
int fd = open(argv[2], O_RDONLY);
if(fd == -1) {
perror("open");
exit(1);
}
// Reset stdin so that it is coming from the open file
int result = dup2(fd, fileno(stdin));
if(result == -1) {
perror("dup2");
exit(1);
}
// Use exec to run the program specified in argv[1]
execlp(argv[1], argv[1], NULL);
perror("execlp");
return 1;
}