Skip to content

Instantly share code, notes, and snippets.

@HorlogeSkynet
Last active August 21, 2017 07:49
Show Gist options
  • Save HorlogeSkynet/e99159ed5ba1737a632c55d356f41889 to your computer and use it in GitHub Desktop.
Save HorlogeSkynet/e99159ed5ba1737a632c55d356f41889 to your computer and use it in GitHub Desktop.
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