I faced with some interesting behavior of basename and dirname functions from libgen.h: they changes the value of argument! Here is the declaration:
char *basename(char *);
char *dirname(char *);
It makes some tiresome to use them... I am new to C and maybe I do something wrong, but to make these functions work I have to create some temporary variables... Here is the code:
Both dirname() and basename() may modify the contents of path, so it
may be desirable to pass a copy when calling one of these functions.
These functions may return pointers to statically allocated memory
which may be overwritten by subsequent calls. Alternatively, they may
return a pointer to some part of path, so that the string referred to
by path should not be modified or freed until the pointer returned by
the function is no longer required.
Meaning: SUSv2 only defined that the functions should return a pointer to a character array, but it doesn't define whether that pointer might be overwritten if the argument passed may be changed or not, but leaves that to the implementation.
You could change your code using malloc(), so that you wouldn't have to allocate memory statically but only if needed.
pludi
Thanks a lot for your answer! I'm using SunOS 5.10 and its manual pages say nothing about modifying function's arguments... So AFAICS, calling these function with duplicate arguments is the one way to preserve value of original variable...
I consider to start using malloc and stuff later, when I get some experience on pointer operations...
What platform are you on...both HPUX and AIX state in the man pages of basename/dirname saying that these functions may modify the pathname argument so it's a good idea to use strdup on the pathname before passing it as an argument to those functions.
check_end_file.c: In function `check_end_file':
check_end_file.c:46: warning: implicit declaration of function `strdup'
check_end_file.c:46: warning: passing arg 1 of `basename' makes pointer from integer without a cast
If basename(3C) takes pointer and strdup(3C) returns it, why does it "makes pointer from integer"?.. If I got it right, I need to pass to basename function a pointer to the beginning of some memory area, which hold char array, but strdup returns the same thing - "a pointer to a new string that is a duplicate of the string pointed to by s1"... I'm a little bit confused...
And what does mean "impicit declaration..."?
Thanks in advance
---------- Post updated at 01:12 AM ---------- Previous update was at 12:52 AM ----------
I've got it all. No further explanaition needed
thanks all
---------- Post updated at 01:30 AM ---------- Previous update was at 01:12 AM ----------
So after declaration of basename and strdup these warnings disappeared, but why some functions, returning non-integer values, such as readdir(3C) don't need to be declared?..