R
Rookie
I was just wondering whether there is any MAX_PATH macro in C\C++? If not
what is a safe value to assume for maximum path length?
what is a safe value to assume for maximum path length?
I was just wondering whether there is any MAX_PATH macro in C\C++? If not
what is a safe value to assume for maximum path length?
Rookie said:I was just wondering whether there is any MAX_PATH macro in C\C++? If not
what is a safe value to assume for maximum path length?
MAX_PATH is not defined in standard c library...
FILENAME_MAX is defined in bits/stdiolim.h in case of GNU c library
Martin Ambuhl said:FILENAME_MAX is a macro defined in <stdio.h> which expands to an
integer constant expression that is the size needed for an array of
char large enough to hold the longest file name string that the
implementation guarantees can be opened.
In said:MAX_PATH is not defined in standard c library...
FILENAME_MAX is defined in bits/stdiolim.h in case of GNU c library
#define FILENAME_MAX 4096
no one said:instead you can define MAX_PATH macro for yourself.
In said:Which doesn't guarantee that there aren't valid file names longer than
FILENAME_MAX.
OTOH, a decent implementation should provide a definition of FILENAME_MAX
that is adequate for most purposes.
Note that FILENAME_MAX is not relevant to any file name generated by
the implementation, so there are no buffer overrun issues involved.
tmpnam() generates file names that are guaranteed not to exceed
L_tmpnam - 1 bytes.
In said:Agreed, but it's important to keep in mind that it may not be adequate
for *all* purposes. A program that gets a file name from an external
source (command-line argument, user input, etc.) cannot safely assume
that the name will fit in FILENAME_MAX bytes, though it wouldn't be
entirely unreasonable to reject the name if it doesn't. (On the other
hand, it's not that hard to accept file names of any arbitrary
length.)
I don't actually see an explicit guarantee that L_tmpnam <= FILENAME_MAX,
but having L_tmpname > FILENAME_MAX would be perverse.
In said:(e-mail address removed) (Dan Pop) writes: [...]OTOH, a decent implementation should provide a definition of FILENAME_MAX
that is adequate for most purposes.
Agreed, but it's important to keep in mind that it may not be adequate
for *all* purposes. A program that gets a file name from an external
source (command-line argument, user input, etc.) cannot safely assume
that the name will fit in FILENAME_MAX bytes, though it wouldn't be
entirely unreasonable to reject the name if it doesn't. (On the other
hand, it's not that hard to accept file names of any arbitrary
length.)
If the thing already exists as a string (e.g. obtained from argv) there is
little point in bothering with checking its length. FILENAME_MAX is
supposed to help the programmer when allocationg memory for storing a
file name coming from a stream. In such contexts, there is little point
in trying to accomodate arbitrarily long strings: allocate FILENAME_MAX
characters and complain if the name doesn't fit (terminating null
character included).
Note that HPUX is (in)famous for misinterpreting the definition of
FILENAME_MAX and providing a next to useless value for it.
Who was talking about any guarantees here? The point was that, when the
file name comes from tmpnam(), FILENAME_MAX is entirely irrelevant.
In said:Which could be an argument against using FILENAME_MAX as an upper
bound on accepted file names.
On the contrary, this is the only way to convince HP to fix their
user unfriendly implementation: make as many users as possible complain.
In said:Complain away. In the meantime, I'm not going to write software that
doesn't work on HP-UX.
Users aren't going to care whether it's the
fault of the OS or of the application programmer.
Who says it doesn't work? It only suffers from a limitation imposed by
the vendor.
They will complain to the application programmer, and the application
programmer will explain them that the limitation is imposed by the vendor.
I'd rather test the length of a user supplied file name against
FILENAME_MAX and reject file names exceeding it than silently accepting
*anything* provided as a file name and run the risk that the OS truncates
it behind my back. I prefer to deal with the complaint "your program
is rejecting this file name because it is too long" than with "your
program is silently truncating my file name".
Rookie said:I was just wondering whether there is any MAX_PATH macro in C\C++? If not
what is a safe value to assume for maximum path length?
In said:(e-mail address removed) (Dan Pop) writes:
[...]Who says it doesn't work? It only suffers from a limitation imposed by
the vendor.
A Google search indicates that HP-UX defines FILENAME_MAX as 14, which
is absurd.
Given the existence of systems on which file names can be arbitrarily
long, I don't see that FILENAME_MAX is particularly useful.
But it isn't imposed by the vendor. An application programmer is not
required to reject file names longer than FILENAME_MAX.
I've been assuming that fopen(), given a string longer than
FILENAME_MAX, will either open the named file or fail to do so. It
didn't occur to me that it would silently truncate the name. I
suppose that's possible, but I don't know of any system that actually
does so. Do you?
In my (admittedly limited) experience, it's safer to assume that
fopen() will fail sensibly given an overly long filename that to
assume that FILENAME_MAX is defined sensibly.
When we're talking about *portable* programming, we cannot rely on our
personal experiences or intuitions: we have no clue about how systems
unknown to us respond when abused with file names longer than
FILENAME_MAX characters. And, although FILENAME_MAX is defined in the
context of fopen(), there are other standard functions that take file
names as arguments (rename, remove) and whose behaviour need not be
as sensible as we wish when abused.
Dag Viken said:Max filename size is system dependent. In VC _MAX_PATH (=260) is defined in
stdlib.h. If you need to support multiple OS's you will need some #ifdef
construct to differentiate them.
In said:"The fopen function opens the file whose name is the string pointed to
by filename, and associates a stream with it." (C99 7.19.5.3p2)
"The fopen function returns a pointer to the object controlling the
stream. If the open operation fails, fopen returns a null pointer."
(C99 7.19.5.3p8)
If I call fopen() with a file name "this_is_a_very_long_file_name.txt",
and it opens a file named "this_is_a_very", it's not opening the file
whose name is the string pointed to by filename.
As for HP-UX defining FILENAME_MAX as 14, I think HP has at least
committed an off-by-one error.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.