This program I wrote is supposed to restart tor, wait 5 seconds, then look for a PID number using regex.
It compiles and runs fine but the regex logic never displays "good".
I'm confused.

#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <string.h>

// gcc -o tor_restart tor_restart.c
// chown root tor_restart
// chmod +s tor_restart

int main( int argc, char **argv ) {
	regex_t regex;
	int reti;
	FILE *in;
	extern FILE *popen();
	char buff[512];
	
	reti = regcomp(&regex, "[0-9]+", 0);
	if (reti) {
		fprintf(stderr, "Could not compile regex\n");
		return 1;
	}
	setuid(geteuid());
	system("/etc/init.d/tor restart > /dev/null 2>&1");
	
	sleep(5);
	
	if (!(in = popen("pidof tor", "r"))) {
		return 1;
	}
	while (fgets(buff, sizeof(buff), in) != NULL ) {
		//printf("Output: %s", buff);
		if (regexec(&regex, buff, 0, NULL, 0) == 0) {
			puts("good");
		}
	}
		
	regfree(&regex);
	pclose(in);
	return 0;
}

Which compiler have you used? I can not run this program in Dev c++.
If you tell your compiler's name I can try it.
Thank you.

Edited 5 Years Ago by Onlineshade: n/a

Which compiler have you used? I can not run this program in Dev c++.
If you tell your compiler's name I can try it.
Thank you.

Actually, the compiler information was provided in the comments at the top of the program: he is using gcc running under a Unix variant, probably either Linux or MacOS. The compiler is the same as used by Dev-C++ (though the one used by Dev-C++ is now woefully out of date); the issue is the operating system it runs under.

This last part is relevant because the program uses the POSIX regular expression library, which would not be present in a default Dev-C++ package. The system requirement is underscored by the fact that it also uses popen() and setuid() , Unix system calls which are not supported under Windows. In other words, this won't compile with any Windows compiler, even a port of the same compiler the OP is using.

Edited 5 Years Ago by Schol-R-LEA: n/a

I figured it out.

Here is my code for future reference.
I am using CentOS 6.0 x64

#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <string.h>

// gcc -o tor_restart tor_restart.c
// chown root tor_restart
// chmod +s tor_restart

int main( int argc, char **argv ) {
	regex_t regex;
	int reti;
	FILE *in;
	extern FILE *popen();
	char buff[512];
	
	reti = regcomp(&regex, "[0-9]+", REG_EXTENDED);
	if (reti) {
		fprintf(stderr, "Could not compile regex\n");
		return 1;
	}
	setuid(geteuid());
	system("/etc/init.d/tor restart > /dev/null 2>&1");
	
	sleep(5);
	
	if (!(in = popen("pidof tor", "r"))) {
		return 1;
	}
	while (fgets(buff, sizeof(buff), in) != NULL ) {
		//printf("Output: %s", buff);
		reti = regexec(&regex, buff, 0, NULL, 0);
		if (!reti) {
				puts("good");
		} else {
				puts("bad");
				//printf("Failed to match '%s' with '%s',returning %d.\n",  buff, "[0-9]+", reti);
		}
	}
		
	regfree(&regex);
	pclose(in);
	return 0;
}
This article has been dead for over six months. Start a new discussion instead.