Created
November 26, 2013 12:06
-
-
Save sdvcrx/7657315 to your computer and use it in GitHub Desktop.
learn how to use exec and what is file descriptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ctype.h> | |
#include <stdio.h> | |
int main(void) | |
{ | |
int ch; | |
while((ch = getchar()) != EOF) { | |
putchar(toupper(ch)); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
int main(int argc, char *argv[]) | |
{ | |
int fd; | |
if(argc != 2){ | |
fputs("usage: wrapper file\n", stderr); | |
exit(1); | |
} | |
// O_RDONLY = open file (read only) | |
fd = open(argv[1], O_RDONLY); | |
if(fd < 0){ | |
// failed to open file | |
perror("open"); | |
exit(1); | |
} | |
// duplicate a file descriptor | |
// These system calls create a copy of the file descriptor oldfd. | |
// dup2() makes newfd be the copy of oldfd, closing newfd first if necessary | |
// STDIN_FILENO means standard input | |
dup2(fd, STDIN_FILENO); | |
// so it will copy fd to standard input and close fd | |
close(fd); | |
execl("./upper", "upper", NULL); | |
perror("exec upper"); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment