LINUX TIPS AND TRICKS --- August 17, 2001

Published by ITworld.com -- changing the way you view IT
http://www.itworld.com/newsletters
________________________________________________________________

Obtaining the Current Working Directory
By Danny Kalev

This week, I will demonstrate three techniques for obtaining a 
process's current working directory from a program.

The getcwd() Syscall
The getcwd() syscall that retrieves the current working 
directory is declared in <unistd.h> as follows:

    char * getcwd(char * buf, size_t max_size);

A getcwd() call fills the argument buf with the path of the 
current working directory. The path's length may not exceed 
max_size ?1 (remember that the last char holds the null 
character). If the path's length exceeds this value, then the 
function sets the errno flag to ERANGE and leaves buf empty. 
If an error occurs, then the function returns NULL.

Under certain conditions, the path's length may be extremely 
long. In this case, you need to allocate buf dynamically. 
Linux, like many other UNIX systems, defines an extension to 
getcwd(): when you pass NULL as the first argument. The 
function dynamically allocates a buffer large enough to hold 
the current path and returns its address. You must call free() 
to release the dynamically allocated buffer after using it. 
Here's an example:

    char *dir=getcwd(NULL, 0);
    printf("current working directory is: %s",dir);
    free(dir);

The PWD Environment Variable
A second method for obtaining the current working directory is 
using the PWD environment variable. Most modern shells store 
the current working directory in this variable. You can 
retrieve it using the getenv () function:

    char *cwd=getenv("PWD");

Note, however, that PWD may contain symbolic links. By contrast,
getcwd () will always return a path free from symbolic links.

The getwd() syscall
A third method for obtaining the current working directory is 
calling the function getwd(), which has the following prototype:

    char *getwd(char * buf);

As you can see, getwd() doesn't take a size limit; it assumes 
that buf is sufficiently large enough to hold the path. However,
it will not write more than PATH_MAX (this constant is defined 
in <unistd.h>) characters to buf, so you can avert potential 
buffer overflows by passing a buf of at least PATH_MAX 
characters. If the path is longer than PATH_MAX, then getwd() 
will not allow you to retrieve it. Due to this limitation, 
getwd() is deprecated and should be avoided although you may 
still find it in legacy code.


About the author(s)
-------------------
Danny Kalev is a system analyst and software engineer with more 
than 10 years of experience, specializing in C++ and 
object-oriented analysis and design on various platforms 
including VMS, DOS, Windows, Unix, and Linux. His technical 
interests involve code optimization, networking, and 
distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ 
Professional Programmer's Handbook (Que, 1999). Danny can be 
reached at linuxnl@excite.com.
________________________________________________________________

ADDITIONAL RESOURCES

Displaying the working directory
pwd
http://www.mkssoftware.com/docs/man1/pwd.1.asp

How Linux Organizes Data
http://antiweb.org/translation/debian/ch04_03.html

Introduction to Unix Shells
http://sdcd.gsfc.nasa.gov/NCCS/misc/unix.shell.html

Linux Tips and Tricks Newsletter Archive
http://www.itworld.com/nl/lnx_tip/
________________________________________________________________

ITWORLD.COM SERVICES

ITworld.com's RFP Exchange is the place to go for your 
outsourcing needs!

Post an RFP for FREE and receive proposals from qualified IT 
providers. Go to the RFP Exchange and get your projects started
today! http://itworld.newmediary.com/itw0608nwsltrb
________________________________________________________________

CUSTOMER SERVICE

SUBSCRIBE/UNSUBSCRIBE:
- Go to: http://www.itworld.com/newsletters
- Click on "View my newsletters" to log in and manage your
  account
- To subscribe, check the box next to the newsletter
- To unsubscribe, uncheck the box next to the newsletter 
- When finished, click submit

Questions? Please e-mail customer service at: 
mailto:support@itworld.com
________________________________________________________________

CONTACTS

* Editorial: Andrew Santosusso, Associate Editor, Newsletters, 
  andrew_santosusso@itworld.com

* Advertising: Clare O'Brien, Vice President of Sales, 
  clare_obrien@itworld.com

* Recruitment advertising: Jamie Swartz, Eastern, Regional Sales 
  Manager, jamie_swartz@itworld.com or Paul Duthie, Western
  Regional Sales Manager, paul_duthie@itworld.com

* Other inquiries: Jodie Naze, Senior Product Marketing Manager, 
  jodie_naze@itworld.com
________________________________________________________________

PRIVACY POLICY
http://www.itworld.com/Privacy/

Copyright 2001 ITworld.com, Inc., All Rights Reserved.
http://www.itworld.com
