So the internet went down the other day and I got really bored. I made a program to beep at me when the internet connection came back on. Thought I would post it here in case someone could use it. I'm aware that the string doesn't need to be that big, I just never bothered to change it(it works all the same though).
/*coded by xeemo 2009 because the internet connection went out and it was boring.*/
#include <stdio.h> //standard input and output
#include <string.h> //needed for handling strings
#include <unistd.h> //needed to read pipes from files
int length; // variable for string length
int main(){
FILE * ping; //activates the ping file
char pingo[80]; //string for ping reply
int lastchar; //variable to send the characters to the string from the pipe
ping = popen("ping google.com", "r"); //command inserted for file in read mode through pipe
lastchar = fread(pingo, 1, 80, ping); //characters 1 through 80 are read from the pipe
pingo[lastchar] = '\0'; //pingo is a null terminated string through lastchar
length = strlen(pingo); //length of string piped in
if(length == 0){ //if no string was piped go to this select screen
int select; //option variable
printf("\ainternet is down. enter 1 to have system alert you when you have internet. enter 2 to exit.\n"); //beep and display message
scanf("%i",&select); //get input from user
switch(select){
case 1:
loop: //loop this until you get a reply
if(length == 0){
FILE * ping;
ping = popen("ping google.com", "r");
lastchar = fread(pingo, 1, 80, ping);
pingo[lastchar] = '\0';
pclose(ping); //close the ping file
length = strlen(pingo);
usleep(9000000); //spaces connections out so it doesn't crash
if(length == 0){ //checks for connection again
goto loop; //loops above if you're still offline
}
}
if(length != 0){
printf("\ayour internet connection has returned\n");//beeps and alerts user when connected
}
break;
case 2: //alternative exit option
return 0; //ends
default:
printf("\aerror"); //what are you stupid? enter 1 or 2 like the menu says.
}
}
if(length != 0){ //internet connection is up
printf("\ayour internet is working\n");
}
pclose(ping); //close ping file
return 0;
}