How Does One Discover the Full Path Name of the Current Directory?

K

kvnsmnsn

I've written a piece of code that interfaces with Postgres. It needs
to write a Postgres table to disk, which it does with the <COPY> com-
mand. That command requires the absolute file name of the file being
written to. Right now I've got it hard coded to the exact location
where I want it, but that's not very portable. Is there some way in C
to retrieve the absolute path name of the current directory, so that I
could use that in my code and therefore have my code function right no
matter where it's executed? Any information on this would be greatly
appreciated.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
R

Richard Tobin

I've written a piece of code that interfaces with Postgres. It needs
to write a Postgres table to disk, which it does with the <COPY> com-
mand. That command requires the absolute file name of the file being
written to. Right now I've got it hard coded to the exact location
where I want it, but that's not very portable. Is there some way in C
to retrieve the absolute path name of the current directory, so that I
could use that in my code and therefore have my code function right no
matter where it's executed? Any information on this would be greatly
appreciated.

There's no portable way to do this in standard C: in fact, C itself
doesn't know anything at all about directories. But all operating
systems provide a way to do it. For example, unix systems have
getcwd() ("get current working directory") which is declared in
<unistd.h>.

-- Richard
 
R

Richard Bos

There's no portable way to do this in standard C: in fact, C itself
doesn't know anything at all about directories. But all operating
systems provide a way to do it.

All operating systems _where this is possible in the first place_. It is
not always possible; for example, under very early versions of MS-DOS,
there was no such thing as a directory. All common modern desktop OSes
have them, but I would not be surprised at all to learn of embedded ones
that have files, but no directories, and therefore no path names,
absolute or relative.

Richard
 
J

jacob navia

Richard Bos a écrit :
All operating systems _where this is possible in the first place_. It is
not always possible; for example, under very early versions of MS-DOS,
there was no such thing as a directory. All common modern desktop OSes
have them, but I would not be surprised at all to learn of embedded ones
that have files, but no directories, and therefore no path names,
absolute or relative.

Richard

Nobody cares about those systems. If you would have read what the
original poster wrote, you would have seen the first sentence:

"I've written a piece of code that interfaces with Postgres."

It would be astounding that Postgres runs in an embedded OS with
no directories or under "early MSDOS" isn't it?

But at each question we have to know apparently how we make the
program portable to MSDOS systems without directories, or whatever.

Living in the past is a favorite passtime of people in this group
apparently.
 
C

Chris Dollin

jacob said:
Richard Bos a écrit :

Nobody cares about those systems.

I bet that's not true.
If you would have read what the
original poster wrote, you would have seen the first sentence:

"I've written a piece of code that interfaces with Postgres."

It would be astounding that Postgres runs in an embedded OS with
no directories or under "early MSDOS" isn't it?

Doesn't matter -- /that/ question had already been answered.
But at each question we have to know apparently how we make the
program portable to MSDOS systems without directories, or whatever.

I think you're misreading the intention of the answer.
Living in the past is a favorite passtime of people in this group
apparently.

Some people have made money from "Living in the Past". It's not to
be sneezed at.
 
R

Richard Bos

Chris Dollin said:
I bet that's not true.

Well, nobody who is anybody cares: obviously, because jacob does not
care, and jacob is the only person who is anybody, and his (lack of)
experience defines the universe.
I think you're misreading the intention of the answer.
Quite.


Some people have made money from "Living in the Past". It's not to
be sneezed at.

Jethro Tull, for example :).

Richard
 
R

Richard Tobin

There's no portable way to do this in standard C: in fact, C itself
doesn't know anything at all about directories. But all operating
systems provide a way to do it.
[/QUOTE]
All operating systems _where this is possible in the first place_. It is
not always possible; for example, under very early versions of MS-DOS,
there was no such thing as a directory.

If you're going to be pedantic, it's equally true to say that such
systems have one directory. And the operating system certainly provides
a way to find out which of the one directories you're in, using the
following code:

In fact, CP/M (and presumably early MS-DOS, though I never used it)
had one directory per disk, and there was no doubt a way to find out
what the current disk was, which would be necessary to get the
equivalent of an "absolute path name".

-- Richard
 
L

llothar

have them, but I would not be surprised at all to learn of embedded ones
that have files, but no directories, and therefore no path names,
absolute or relative.

The Oberon system had no paths.

There was only a large BTree and file names looked like
"myapp.mydocs.letter-written-on-11-may.txt" and there was an API to
retrieve all file names at and after a given prefix. So it worked very
much like a hierachical system.
 
M

Mark McIntyre

I bet that's not true.

Safe bet: how many mobile phone systems programmers exist? Not to
mention vehicle management systems, EPOS and a zillion other
environments.

Speak for yourself - I have a funny feeling that embedded systems make
up a significant part of the future.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Kenneth Brody

Is there some way in C to retrieve the absolute path name of the
current directory,
[...]

In straight C, no. (C doesn't even know about "directories".)
However, if you want to expand from straight C into the POSIX
world (which is OT here) you can use getcwd(). If you need
more help, you'll need to ask somewhere else where POSIX is
topical. Perhaps comp.unix.programmer?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

jacob navia

Mark said:
Safe bet: how many mobile phone systems programmers exist? Not to
mention vehicle management systems, EPOS and a zillion other
environments.

Postgres data base in mobile phones?
Did you even READ what the original poster said?

His message started with:

"I've written a piece of code that interfaces with Postgres."

In this context it is stupid to assume he wants his code to run
in some embedded system that hasn't even directories!

But we are lectured about this at every question!
Speak for yourself - I have a funny feeling that embedded systems make
up a significant part of the future.


Future "embedded systems" will look more and more like
workstations. Linux has been embedded into a simple watch, for instance.

But embedded systems wasn't the question but the famous

getcwd() function
 
D

Dave Vandervies

jacob navia said:
But at each question we have to know apparently how we make the
program portable to MSDOS systems without directories, or whatever.

Living in the past is a favorite passtime of people in this group
apparently.

How about a modern OS under active development, then?

PalmOS (for which there are several mutually mostly-compatible
freestanding C implementations, and I believe at least one hosted one)
supports accessing files on an external memory card, but has no concept
of a directory. As far as the OS is concerned, 'card slot 1, file
"/path/to/file"' IS the filename.

How would you find the "current directory" in a PalmOS program?


dave
 
J

jacob navia

Dave said:
How about a modern OS under active development, then?

PalmOS (for which there are several mutually mostly-compatible
freestanding C implementations, and I believe at least one hosted one)
supports accessing files on an external memory card, but has no concept
of a directory. As far as the OS is concerned, 'card slot 1, file
"/path/to/file"' IS the filename.

How would you find the "current directory" in a PalmOS program?


dave

Hi dave

Two points:

FIRST:
-----

http://www.developer.com/ws/palm/article.php/3508341

Starting from Palm OS 4.0, applications can a utilize new set of
additional system extensions that provide a unified interface for
accessing various expansion capabilities.

....

The Virtual File System Manager is a software layer that manages all
installed file system libraries. The file system is implemented as a
shared library of type sysFileTFileSystem.

Common Operations

VFS Manager supports a standard set of common file operations. They are
listed below, grouped by operation:

Err VFSDirCreate(UInt16 volRefNum, const Char *dirNameP);
Err VFSGetDefaultDirectory(UInt16 volRefNum, const Char *fileTypeStr,
Char *pathStr, UInt16 *bufLenP);
Err VFSRegisterDefaultDirectory(const Char *fileTypeStr,
UInt32 mediaType,
const Char *pathStr);
Err VFSUnregisterDefaultDirectory(const Char *fileTypeStr,
UInt32 mediaType);

This is then, how you manage directories under Palm OS.
It is not good to live in the past :)

SECOND:
 
D

Dave Vandervies

Dave said:
How about a modern OS under active development, then?

PalmOS (for which there are several mutually mostly-compatible
freestanding C implementations, and I believe at least one hosted one)
supports accessing files on an external memory card, but has no concept
of a directory. As far as the OS is concerned, 'card slot 1, file
"/path/to/file"' IS the filename.

How would you find the "current directory" in a PalmOS program?


dave

Hi dave

Two points:

FIRST:
-----

http://www.developer.com/ws/palm/article.php/3508341

Starting from Palm OS 4.0, applications can a utilize new set of
additional system extensions that provide a unified interface for
accessing various expansion capabilities.
[...]

Err VFSDirCreate(UInt16 volRefNum, const Char *dirNameP);
Err VFSGetDefaultDirectory(UInt16 volRefNum, const Char *fileTypeStr,
Char *pathStr, UInt16 *bufLenP);
Err VFSRegisterDefaultDirectory(const Char *fileTypeStr,
UInt32 mediaType,
const Char *pathStr);
Err VFSUnregisterDefaultDirectory(const Char *fileTypeStr,
UInt32 mediaType);

This is then, how you manage directories under Palm OS.
It is not good to live in the past :)

If you had actually read the documentation and not just looked at the
names, you would have discovered that these don't actually correspond
to the notion of "directory" as a location in a "directory tree" that you
can move around in, in which you can store "files" that you seem to
be committed to; rather, they provide a way to construct filenames to
access a certain type of file without needing to know where on the card
they're stored when the code is written.
As far as the OS is concerned, all these do is access and manipulate
information about what valid and useful filenames look like. They have
nothing to do with how you access the file once you've figured out
the name.



I would be entirely unsurprised to find that somebody has written code
that will run on a Palm and interface with a database. (Note that the
database doesn't have to be running on the Palm for code running on the
Palm to interface with it.)


dave
 
M

Mark McIntyre

Postgres data base in mobile phones?
Did you even READ what the original poster said?

Sure, but what does postgres have to do with whether or not anyone
cares about systems that don't support directories?

If you had said "on a system which uses relational databases, its
unlikely there is not support for directories" you would have been
correct, (though someone would probably have pointed out that at least
one RDBMS zaps the partition and reformats with its own filesystem
that has no directories).
In this context it is stupid to assume he wants his code to run
in some embedded system that hasn't even directories!

Whats stupid is to say
"nobody cares about such systems".
when you mean
"the OP probably doesn't care about such systems".

The trick is to write what you mean, rather than arrogant-sounding
generalities that annoy people.
But we are lectured about this at every question!

Well, /you/ certainly are.
Future "embedded systems" will look more and more like
workstations.

And the future of desktop computing is diskless workstations.
If Larry can get it wrong, explain why I should believe you...

Myself, I think that future embedded systems will look less and less
like computers at all. Why waste memory and flash with an os that
supports directories, when you're programming the central heating
system?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

On Fri, 11 May 2007 18:45:13 +0000 (UTC), in comp.lang.c ,
I would be entirely unsurprised to find that somebody has written code
that will run on a Palm and interface with a database. (Note that the
database doesn't have to be running on the Palm for code running on the
Palm to interface with it.)

For what its worth, programming for Palm 4.x and below is almost
entirely like working inside a database. You write what are
effectively SPs to access and manipulate data stored in table-like
structures. There's a reason they're called PDBs...

Oh, and there's an ODBC driver for PalmOS.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
D

Dave Vandervies

For what its worth, programming for Palm 4.x and below is almost
entirely like working inside a database. You write what are
effectively SPs to access and manipulate data stored in table-like
structures. There's a reason they're called PDBs...

Yes. (That's still mostly the case for 5.x as well.) The concept of a
"file" doesn't exist in the Palm's internal storage; you only have files
if there's a memory card in an external expansion slot.
This is one of the reasons why freestanding implementations don't need
to have <stdio.h>.

I doubt that accessing a PDB is anything like interfacing with a Postgres
database, though.


dave
 
G

guru.jois

There's no portable way to do this in standard C: in fact, C itself
doesn't know anything at all about directories. But all operating
systems provide a way to do it. For example, unix systems have
getcwd() ("get current working directory") which is declared in
<unistd.h>.
This require dynamic memory to hold path length, tough to predict. If
less, lost. If more segfault.
Better try
char * get_current_dir_name(void);
which calls malloc by itself...
-- Richard

Bye
Gururaj.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top