The mkdir function is used to create a directory. The format is as follows:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int mkdir (const char *pathname,mode_t mode);
Where the parameter pathname is the directory name of the newly created directory, mode specifies the access rights for the directory that will be modified by the file creation Mask (umask).
This function creates an empty directory named Pathname, which automatically contains the "." and ".." 2 registered entries. The user ID of this newly created directory is set to the valid user ID of the calling process, with the group ID of the parent directory or the valid group ID of the process.
If the call succeeds, mkdir updates the St_atime, St_ctime, and St_mtime of the directory, updates the St_ctime and st_mtime of its parent directory, and then returns 0. If the call fails, MKDIR returns-1.
The parent directory of the new directory specified by pathname must exist, and the calling process must have write access to the parent directory and search permissions for each of the pathname involved in the path directory.
The Rndir function deletes an empty directory, which is formatted as follows:
#include <unistd.h>
int rmdir (const char *pathname);
When using the RmDir function, the directory must be empty or the call fails, and the function returns-1. When successful, the function returns 0.
Example: Create a new directory, and then delete the directory.
Copy Code code as follows:
#include <sys/types.h>
#include <unistd.h>
#include <limits.h>
#include <sys/stat.h>
int main (int argc,char *argv[])
{
Char path[1000];
Char file[1000];
/*
if (argc!=2)
{
printf ("Usage mk<pathname>\n");
return 1;
}
*/
ARGV[1] = "Test";
GETWD (path); Get Current working directory
printf ("Current dirctory is:%s\n", path);
if (mkdir (argv[1],s_irwxu| s_irgrp| s_ixgrp| S_iroth) <0)//Create a new directory
{
printf ("mkdir failed\n");
return 2;
}
if (ChDir (argv[1]) <0)//change current working directory to new directory
{
printf ("ChDir failed \ n");
return 3;
}
GETWD (path);
printf ("mkdir successed.\n New current directory is:%s\n", path);
RmDir (path); Delete new Directory
printf ("%s is removed\n", path);
return 0;
}