Skip to content

Instantly share code, notes, and snippets.

@HorlogeSkynet

HorlogeSkynet/skyClear.c

Last active Aug 21, 2017
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