sosij.morris@gmail.com wrote:
Quote:
On Jul 21, 7:06 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
Quote:
>[...]
> What's MAX_PATH?
>
Didn't know about FILENAME_MAX - thanks. In stdlib.h?
Do you have access to a C textbook or reference? Do you
have access to `man' pages or `info' or `help'? Do you have
access to Google? Do you have enough intellectual curiosity
to prompt you to use any of these? Or would you like a few
more grapes peeled for you? Sheesh ...
Quote:
Quote:
> Two problems here, both potentially fatal. First, if getenv()
>returned a NULL, passing that NULL to strtok() is an error.
>
Ok, but, I think that Windows will always haven something here - in
the PATH environment variable. So, I belive it's ok for a non-NULL
result for this.
A trusting soul, I see. Sosij, a person like you really
ought to own something of importance, something of monumental
scale. It happens that I own a bridge -- architectural marvel,
widely celebrated, the pride of its city and used by thousands
of vehicles every single day -- and since I myself hardly ever
visit Brooklyn to enjoy my bridge, I'm thinking of selling it.
Would you be interested? It can be yours for only -- well, how
much have you got?
Quote:
Quote:
>Second, strtok() may modify the pointed-to string,
>
So, strtok() may modify the original character array path pointed to?
But, as I don't use that anymore; I just assigned the variable 'path'
to whatever strtok returns; is this really a problem?
strtok() doesn't modify `path'; it modifies the string that
`path' points to. `path' is your own variable and you can do
with it whatever you like, but the string it points to is NOT
yours: it is part of the process' environment. The C Standard
is quite clear on this point: the program "shall not modify" the
string, and a program that violates a "shall" it invokes undefined
behavior. Once you indulge in U.B. there's no telling what might
happen, and the C Standard washes its hands of your program.
Quote:
Quote:
>and trying to modify the string returned by getenv() is also an error.
>
The getenv() function I have declared says that it returns a char *,
i.e., no const is given, so I thought I could pass that to strtok().
The documentation I have for strtok doesn't say it will try to alter
whatever its passed char * will point to -- in fact I'd have thought,
to be useful, it would make a copy so that it's more 'usuable'.
You're saying this isn't so?
If your documentation doesn't describe what strtok() does,
your documentation is deficient. If your documentation says that
strtok() makes a copy of the string, your documentation is wrong.
I suggest you re-read the documentation -- and if it's truly as
bad as you say, complain to the vendor.
Quote:
Quote:
>You've fallen for the "strncpy() is a safe strcpy()" myth.
>Note that strncpy() does not necessarily produce a string: it may
>leave `filepath' without a trailing '\0'.
>
So I should ensure that filepath is NULL terminated? What's the best
way to do that - make sure there's enough space, AND initialize to all-
zeros?
The "best" way, in my view, is not to rely on constant-sized
arrays for this sort of thing. Get the length of the PATH piece,
add one for the \, add the length of the appendage, add another 1
for the terminating zero, and use malloc(). Then use strcpy() and
strcat() with no 'n's.
Quote:
Quote:
> The third argument of strncat() doesn't mean what you think it means.
>
?
Did your documentation fail to describe strncat(), too? If
so, you should not just complain to the vendor: you should blog
at length about the vendor's obstinate nitwittedness until you
can shame them into doing a better job. If the documentation is
as bad as you imply, "half-hearted" would be an improvement.
--
Eric Sosman
esosman@ieee-dot-org.invalid