change the current working directory
#include <sys/types.h> #include <unistd.h> int chdir( const char *path );
The chdir() function changes the current working directory to the specified path. The path can be either relative to the current working directory, or it can be an absolute path name.
#include <stdio.h> #include <stdlib.h> void main( int argc, char *argv[] ) { if( argc != 2 ) { fprintf( stderr, "Use: cd <directory>\n" ); exit( 1 ); } if( chdir( argv[1] ) == 0 ) { printf( "Directory changed to %s\n", argv[1] ); exit( 0 ); } else { perror( argv[1] ); exit( 1 ); } }
POSIX 1003.1
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes, but modifies errno |
Thread | Yes |
chmod(), errno, getcwd(), mkdir(), mknod(), rmdir(), stat(), umask()