[Gllug] [Somewhat OT] POSIX regex in C

Adrian McMenamin adrian at newgolddream.dyndns.info
Sat Jul 9 11:23:55 UTC 2011


I appreciate this is a somewhat specialist question, but it is Linux
related...

I am trying to write some C to read /proc/pid/maps and then read off the
addresses at the start of each line.

This is the code with various printf calls for debugging (once the regex
works:

#include <regex.h>

struct blocklist* getnextblock(void** lastblock, void** head, char* buf)
{
	printf("LINE:%s", buf);
	regex_t reg;
	regmatch_t addresses[3];
	char pattern[1024] = "^([0-9a-f]+)-([0-9a-f]+)";
	int xreg = regcomp(&reg, pattern, REG_EXTENDED|REG_NOSUB);
	printf("Regcomp returns %d\n", xreg);
	int match = regexec(&reg, buf, (size_t)3, addresses, 0);
	regfree(&reg);
	return *lastblock;
}

This produces bizarre behaviour. As it is above xreg reports 0 - a success
- but the code fails to match - more than that the printf line at the top
reports buf is empty - however if I comment out (the regcomp line alone is
enough to break this - the rest presumably has the same issue):

	int xreg = regcomp(&reg, pattern, REG_EXTENDED|REG_NOSUB);
	printf("Regcomp returns %d\n", xreg);
	int match = regexec(&reg, buf, (size_t)3, addresses, 0);
	regfree(&reg);

the printf reports the (correct) contents of buf - how can code executed
afterwards affect the results of what came before? Very odd and makes me
think it is a build problem. Is there some special library I should be
linking against? The GNU documentation says all this is in glibc...



--
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list