Delete an empty directory
#include <sys/types.h> #include <unistd.h> int rmdir( const char* path );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The rmdir() function removes (deletes) the specified directory. The directory must not contain any files or directories.
If the directory is the current working directory of any process, rmdir() returns -1 and sets errno to EINVAL. If the directory is the root directory, the effect of this function depends on the filesystem. |
The space occupied by the directory is freed, making it inaccessible, if its link count becomes zero and no process has the directory open (opendir()). If a process has the directory open when the last link is removed, the . and .. entries are removed and no new entries can be created in the directory. In this case, the directory will be removed when all references to it have been closed (closedir()).
When successful, rmdir() marks st_ctime and st_mtime for update in the parent directory.
To remove the directory called /home/terry:
#include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> int main( void ) { (void)rmdir( "/home/terry" ); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
chdir(), chmod(), errno, fchdir(), getcwd(), mkdir(), stat()