Visit Our UNIX and Linux User Community


basename and dirname changes the value of argument???


 
Thread Tools Search this Thread
Top Forums Programming basename and dirname changes the value of argument???
# 1  
Old 07-22-2009
basename and dirname changes the value of argument???

Hi

I faced with some interesting behavior of basename and dirname functions from libgen.h: they changes the value of argument! Here is the declaration:
Code:
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:
Code:
...
#include <libgen.h>
...
    char ef[PATH_MAX], eftmp[PATH_MAX], efbn[PATH_MAX] ... ;
...
    strcpy(eftmp, ef);
    strcpy(efbn, basename(eftmp));
...

In this example eftmp before basename() call doesn't equal to eftmp after! For me it looks a little bit strange...

If anyone share some experience on using them in more convenient fashion it will be great! Any comments also appreciated

Thanks in advance
# 2  
Old 07-22-2009
From the man-page:
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.
# 3  
Old 07-22-2009
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...

cheers
# 4  
Old 07-22-2009
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.
# 5  
Old 07-23-2009
Quote:
Originally Posted by shamrock
What platform are you on...
Well, you can check it here - Solaris manual pages say nothing about changing the value of argument...

Quote:
Originally Posted by shamrock
it's a good idea to use strdup on the pathname before passing it as an argument to those functions.
I still need to get some experience on pointer operations... This code returns some warnings:
Code:
#include <libgen.h>
...
    char ef[PATH_MAX], efbn[PATH_MAX] ... ;
...
    strcpy(efbn, basename(strdup(ef)));

Code:
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?..

Previous Thread | Next Thread
Test Your Knowledge in Computers #10
Difficulty: Easy
Charles Babbage was a British mathematician and inventor, known as the 'Father of the Computer'. He designed a mechanical computer called the Analytical Engine which was an early forerunner of the modern computer.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using dirname on a file before uniq

Hello, I have a list of files generated like this: find dir -type f > file_list I want to get a list of just the unique directories. I can't create a temporary file. So the idea is to do a working equivalent to this: cat file_list | dirname | uniq But of course that doesn't... (4 Replies)
Discussion started by: brsett
4 Replies

2. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

3. Shell Programming and Scripting

help with dirname

We are using #!/bin/sh From a command line this command returns the correct list of files (without going into any subdirectories) find /vol.prod/saptrans/common/test/pa/* -prune -type f -print We have a script which takes the same path as $1 (without the * ) ... (2 Replies)
Discussion started by: 15a0
2 Replies

4. UNIX for Dummies Questions & Answers

$dirname ??????

Hi, okay, the following command was given to me in a script, but it's not working and there's little to no help on dirname. What is wrong with the following line? I'm just trying to save the current directory to use later in subsequent scripts. MYAPPDIR=$(dirname $(dirname $0)) Thanks. (2 Replies)
Discussion started by: rebazon
2 Replies

5. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

find & dirname:problems with white spaces in Directories

Hi all, my problem: (little extract from my bash-script) I want to move each file (.mov) from one directory (and many Subdirectories) to another directory (only one); after moving i want to create hardlinks to the old directories. Thatīs no problem, but now: source-directories... (4 Replies)
Discussion started by: tubian
4 Replies

7. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

8. Shell Programming and Scripting

dirname, save cut portion to variable, clean up

Hi guys, last cry for help for today. I appreciate the help so far. ok so I have a program that dumps a path into my script as a variable ($1) This path is an example /home/xbmc/sab_downloads/video/tv/grey's anatomy/season 3 So in order to search thetvdb.com for a show, I need to extract... (6 Replies)
Discussion started by: tret
6 Replies

9. Shell Programming and Scripting

strip hostname from dirname?

I need to get the full path of a file minus the hostname... anyone have an easy way to do this? What I have is: //ourhostname/ourfullpath/filename What I need is: /ourfullpath/filename hostname evaluates to 'ourhostname' dirname evaluates to '//ourhostname/ourfullpath' basename... (2 Replies)
Discussion started by: tink
2 Replies

10. Shell Programming and Scripting

Setting basename and dirname variable to simply script.

Hello all, Can somebody explain to me how set up a basename and dirname variable to simplify this script. I currently have a 'infile' with the contents of FTTPDataPVC_ & BaaisDSLFeed. I need to add a basename and or dirname variable so that any additions can be made through the infile and not... (1 Reply)
Discussion started by: liketheshell
1 Replies

Featured Tech Videos