#include #include #include #include #include #include #include #include #include int main() { /* Skipping error checks for simplicity. The real file has error checks. */ /* All but the last two lines before the return statement are about parsing MAKEFLAGS. The parsing works fine, it's only the last two lines that aren't working the way I expected. */ size_t jobs; wordexp_t args; long pid=(long)getpid(); int i, read_fd=-1, write_fd=-1, tokencount=0; char *makecommand, *makeflags=getenv("MAKEFLAGS"); printf("MAKEFLAGS: %s\n", makeflags); makecommand=malloc(strlen(makeflags)+3); strcpy(makecommand, "a "); strcat(makecommand, makeflags); wordexp(makecommand, &args, 0); free(makecommand); opterr=0; while(1) { static struct option long_options[] = { {"jobserver-auth", optional_argument, 0, 0}, {0, 0, 0, 0} }; int option_index=0; i = getopt_long(args.we_wordc, args.we_wordv, "j::", long_options, &option_index); if(i==-1) break; switch(i) { case 0: sscanf(optarg, "%d,%d", &read_fd, &write_fd); break; case 'j': if(optarg==NULL) jobs=8; else sscanf(optarg, "%zu", &jobs); break; default: break; } } wordfree(&args); printf("jobs: %zu, read_fd: %d, write_fd: %d\n", jobs, read_fd, write_fd); ioctl(read_fd, FIONREAD, &tokencount); printf("token count: %d\n", tokencount); return 0; }