Hi,

On Mon, 4 Feb 2008, Kirill wrote:

> > -----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;

Hehe.  I'm too used to memcpy(), I guess.  Probably a little comment would 
help.

> - hanging on the result of GetEnvironmentStrings for very long;

But it is not free()d in that case, right?

> The bottom line: your solution is indeed much more efficient, however, 
> I'm having hard time to call it "nicer" (as in "prettier").

;-)  Yeah, for me, more concise is almost always "nicer" (as long as it 
is not "obfuscated", but really "concise": just the code you need to do 
the job).  But hey, that's just me.

Ciao,
Dscho

Reply via email to