Skip to content

Instantly share code, notes, and snippets.

@hyperair
Created November 24, 2016 01:37
Show Gist options
  • Save hyperair/0779fd7963991a0677e155c0061f334e to your computer and use it in GitHub Desktop.
Save hyperair/0779fd7963991a0677e155c0061f334e to your computer and use it in GitHub Desktop.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main (int argc, char **argv, char **envp) {
if (argc != 2) {
fprintf (stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
close (0);
int fd = open (argv[1], O_RDONLY);
if (fd < 0) {
perror ("Could not open file");
return 2;
} else if (fd != 0) {
fprintf (stderr,
"Tried to open %s as fd 0, got %d instead\n",
argv[1],
fd);
return 3;
}
char * const arglist[] = {
"/bin/cat",
NULL
};
execve ("/bin/cat", arglist, envp);
perror ("Could not exec");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment