Skip to content

Instantly share code, notes, and snippets.

@fmitha
Created October 22, 2021 20:26
Show Gist options
  • Save fmitha/d0579cdb26ab56a0a70fa41e68080f54 to your computer and use it in GitHub Desktop.
Save fmitha/d0579cdb26ab56a0a70fa41e68080f54 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;
int waitreturnval = waitpid(pid, &status, 0);
if(waitreturnval > 0)
printf("wait exited with process ID of the terminated child %d\n", waitreturnval);
else if(waitreturnval == -1)
printf("wait exited with error");
else if(waitreturnval == 0)
printf("wait exited with code %d\n", waitreturnval);
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 *)NULL);
//execl("/bin/sh", "bin/sh", "-c", "./nopath", "NULL");
//execlp("sh", "sh", "-c", "./nopath", NULL);
//execlp("sh", "sh", "-c", "exit 17", (char *)NULL)
execlp("lsx", "lsx", "-la", "zarko.tex", (char *)NULL);
printf("Continues past execlp call.\n");
//_exit(EXIT_FAILURE); // exec never returns
//_exit(21930); // exec never returns
_exit(132); // exec never returns
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment