Updated: April 19, 2023 |
Get the name of the current working directory
#include <unistd.h> char* getcwd( char* buffer, size_t size );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The getcwd() function returns the name of the current working directory. buffer is a pointer to a buffer of at least size bytes where the NUL-terminated name of the current working directory will be placed.
The maximum size that might be required for buffer is PATH_MAX bytes. See <limits.h>.
The address of the string containing the name of the current working directory, or NULL if an error occurred (errno is set).
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <limits.h> int main( void ) { char* cwd; char buff[PATH_MAX]; cwd = getcwd( buff, PATH_MAX ); if( cwd != NULL ) { printf( "My working directory is %s.\n", cwd ); } return EXIT_SUCCESS; }
produces the output:
My working directory is /home/bill.
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
There is only one current working directory per process. In a multithreaded application, any thread calling chdir() will change the current working directory for all threads in that process.