I get garbage output when printing the string s.
This is after obtaining the output from xrandr -q, which will list monitor devices and resolutions.
I use this to determine if VGA1 has diconnected, and if so turn on display LVDS1 and exit the program.
If you can tell me why s contains garbage i would be grateful.
Code:
#include <string.h>
#include <stdio.h>
//Searches a string for string in var 'me', returns the starting position of the found string +1 if successfull.
//On failure it returns 0.
unsigned int str_find(const char*str, const char* me, unsigned int s){
unsigned int x = 0, l = strlen(me) - 1;
while( (str[s] ^ '\0') ){
if ( !(str[s]^me[x]) ){
//cout<<"Equal at " << s << ',' << x << endl;
if ( !(x^l) )
return s - x + 1;
if ( (str[s+1]^me[x+1]) )
s -= x-2, x = 0;
else ++x;
}
++s;
}
return 0;
}
int main(){
FILE *f;
int c;
unsigned short n;
char s[1024];
while( (f=popen("xrandr -q", "r")) ){
for( n = 0; (c=fgetc(f) != EOF ); ++n )
s[n] = c;
s[n] = '\0';
pclose(f);
printf("%s",s);
if( str_find("VGA1 disconnected", s, 0) > 0 ){
printf("VGA1 Disconnected, igniting LVDS1 and exiting program\n");
system("xrandr --output LVDS1 --auto");
return 0;
}
}
return 1;
}