A simple C function to clear the terminal without using the bad `system("clear")`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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