LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Expanding file names in a script? ../blah -> /home/blah (https://www.linuxquestions.org/questions/linux-general-1/expanding-file-names-in-a-script-blah-home-blah-161504/)

Tinkster 03-24-2004 06:13 PM

Quote:

full_path.C
Code:

#include <iostream>

// includes for the qt-libraries
#include <qfileinfo.h>
#include <qstring.h>

int main( int argc, char *argv[])
{
  QString filename;
  QFileInfo fi = QString(argv[1]);
  if ( fi.exists())
    {
      filename = fi.absFilePath();
      cout << filename ;
      return 0;
    }
  return 1;
}

Compile with
Code:

g++ -I/usr/lib/qt/include -L/usr/lib/qt/lib -lqt-mt full_path.C -o full_path
or where ever Qt stuff lives on your system) ;}


Demo:
Code:

[tink@diggn:~/test]$ cd /usr/local/lib
[tink@diggn:/usr/local/lib]$ ~/test/full_path  ../share/ImageMagick/../aclocal
/usr/local/share/aclocal[tink@diggn:/usr/local/lib]$ ~/test/full_path  ~/test/../documents
/home/tink/documents[tink@diggn:/usr/local/lib]$


Note that "/home/tink/documents[tink@diggn:/usr/local/lib]$" in
the output is NOT an error ... and if you don't want it to
do this, fix it by appending << endl to the cout statement ;)

I think that's what you wanted? :D



Cheers,
Tink


P.S.: Qt rocks!! ;)

Bebo 03-25-2004 04:53 PM

Again, wow! :) Thanks Tink, that is what I wanted. But, oh I'm so stupid... Ever heard of the -ef test? It checks if two files are the same... Now I can solve VisionZ's problem :)

Tinkster 03-25-2004 05:09 PM

Ummm ... -ef checks for a hard link. Not for
file identity? In other words, whether to file
descriptors point to the same inode?


Cheers,
Tink

Bebo 03-25-2004 05:27 PM

Err, maybe? :) Good or bad? :scratch: ;)

Looking_Lost 03-26-2004 06:04 AM

You could just use

cmp


which works on binary and text files.

ruind 03-29-2004 04:44 PM

why not just use realpath?

-michael

Bebo 03-29-2004 04:50 PM

Does that work in shell scripts?

ruind 03-29-2004 04:53 PM

yes, assuming it's installed on your system.

-michael

Bebo 03-29-2004 05:30 PM

Thanks a lot for the suggestion. Hm, it seems I don't have it. But on the other hand, from the man page it seems like a C command.

LOL, look at this - from the man page:
Quote:

BUGS
Never use this function. It is broken by design since it
is impossible to determine a suitable size for the output
buffer.
OK, so... I guess I won't :)

ruind 03-29-2004 05:39 PM

Quote:

Originally posted by Bebo
Thanks a lot for the suggestion. Hm, it seems I don't have it. But on the other hand, from the man page it seems like a C command.

LOL, look at this - from the man page:

OK, so... I guess I won't :)

you are indeed looking at the man page for a c function of the same name. (man section 3, if i remember correctly). the command i am referring to is obviously not installed on your system, but its man page would be located in section 1, if it were.

on a side note, the reason that the c routine's man page says that it is broken is because it is impossible to know ahead of time that a particular path name will be small enough to alloc enough memory to hold it. thus, even if you alloc PATH_MAX (or whatever) space for the output of your path, you may overflow it, which is obviously a security risk. however, you can't avoid this problem, no matter which routine you choose for the purpose.

-michael


All times are GMT -5. The time now is 02:34 PM.