Hi, > -----Original Message----- > From: Johannes Schindelin [mailto:[EMAIL PROTECTED] > Sent: Sunday, February 03, 2008 6:40 PM > Would this not be much nicer like this? > > static char *environment; > > if (!environment) { > char *old = GetEnvironmentStrings(); > int space = 0, path_index = -1, len = 0, len2; > > if (!msys_path()) > return environment = old; > > for (;;) { > if (!strncmp(old, "PATH=", 5)) > path_index = space; > while (old[space]) > space++; > if (!old[space]) > break; > } > > if (path_index == -1) > path_index = space; > else > len = strlen(old + path_index); > space += 2 * strlen(msys_path()) + 32; > > environment = malloc(space); > memcpy(environment, old, path_index + len); > len2 = sprintf(environment + path_index, > "PATH=%s\\bin;%s\\mingw\\bin%s", > msys_path(), msys_path(), len ? ";" : ""); > memcpy(environment + path_index + len2, > old + path_index + len, > space + 1 - path_index - len); > > FreeEnvironmentStrings(old); > } > > return environment; > } > > With this (completely untested; composed in the mail program), you > would not need to change msys_path() to never return NULL... Two things that don't let me to sleep peacefully: - seemingly complicated arguments of memcpy, especially the second one;
- hanging on the result of GetEnvironmentStrings for very long; I'd rather put the little dance, involving msys_path under if (!msys_path); do you mind? especially taken into account that you did not mind to indent the whole body under if (!environment) ;) The bottom line: your solution is indeed much more efficient, however, I'm having hard time to call it "nicer" (as in "prettier"). Thanks! -- Kirill.
