I'd like to write a function like this:
int validate_file_name(char *filename)
{
//...
}
which will:
- return 1 if there was no
\0
character in the filename, - 0 otherwise.
I thought it may be achieved using a simple for(size_t i = 0; i < strlen(filename); i++)
, but I don't know how to determine how much characters I've got to check?
I can't use strlen()
because it will terminate on the first occurrence of a \0
character.
How should I approach this problem?
Clarification:
I am trying to apply these guidelines to a filename I receive. If you should avoid putting a \0
in a filename, how could you validate this if you've got no size parameter.
Moreover, there are strings with multiple \0
characters, like here: http://www.gnu.org/software/libc/manual/html_mono/libc.html#Argz-and-Envz-Vectors. Still, I had no idea that it is impossible to determine their length if it is not explicitly provided.
Conclusion:
There is no way you can determine the length of string which is not NULL-terminated. Unless you know the length of course or you deploy some dirty hacks: Checking if a pointer is allocated memory or not.