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:
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:
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
From the man-page:
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.
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:
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?..
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)