Skip to content

Instantly share code, notes, and snippets.

@fmitha
Created October 20, 2021 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmitha/02648a157ae34fd078810cc0a2714d71 to your computer and use it in GitHub Desktop.
Save fmitha/02648a157ae34fd078810cc0a2714d71 to your computer and use it in GitHub Desktop.
/*
Example from
https://chat.stackexchange.com/transcript/message/59383566#59383566
You may use tput. Examples: tput >/dev/null (status == 2, usage
error), TERM=foo tput >/dev/null (status == 3, no information for
the terminal type), tput foo >/dev/null (status == 4, unknown
capability). It can also return 1, which POSIX says is
"unspecified" and historically used to tell the terminal doesn't
have the specified capability (e.g. TERM=vt100 tput setaf
>/dev/null); notably, in this case the exit status is ≠ 0, but no
error message is printed out.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
int main(void)
{
pid_t parent = getpid();
pid_t pid = fork();
if (pid == -1)
{
// error, failed to fork()
}
else if (pid > 0)
{
int status;
waitpid(pid, &status, 0);
if(WIFEXITED(status))
printf("Child's exit code %d\n", WEXITSTATUS(status));
else
printf("Child did not terminate with exit\n");
}
else
{
//execl("/bin/sh", "bin/sh", "-c", "./nopath", "NULL");
//execlp("ls", "ls", "-la", "zarko.tex", (char *)0);
//execlp("lsxx", "lsxx", "-la", "zarko.tex", (char *)0);
//int fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
//int setenv(const char *name, const char *value, int rewrite);
/* setenv("TERM", "foo", 1); */
/* int fd = open("/dev/null", O_WRONLY | O_CLOEXEC); */
/* dup2(fd, 1); */
/* execlp("tput", "tput", "foo", (char *)0); */
execlp("sh", "sh", "-c", "./nopath", (char *)0);
_exit(EXIT_FAILURE); // exec never returns
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment