Skip to content

Instantly share code, notes, and snippets.

@HorlogeSkynet
Last active August 21, 2017 07:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
A simple C function to clear the terminal without using the bad `system("clear")`
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
void sky_clear(void)
{
pid_t status = fork();
switch(status)
{
case -1:
printf("Couldn\'t \'fork()\' the current process into a child: %s\n", strerror(errno));
break;
case 0:
if(execvp("clear", (char *const[]){"clear", NULL}) == -1)
{
printf("Couldn\'t \'exec()\' the command: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
default:
wait(&status);
break;
}
}
int main(int argc, const char *argv[])
{
(void)argc;
(void)argv;
sky_clear();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment