mkdir system call can be used to create new directory
definition:
ubuntu@ip-172-31-23-227:~$ less /usr/include/unistd.h
......
/* Remove the directory PATH. */
extern int rmdir (const char *__path) __THROW __nonnull ((1));
......
ubuntu@ip-172-31-23-227:~$ less /usr/include/x86_64-linux-gnu/sys/stat.h
......
/* Create a new directory named PATH, with permission bits MODE. */
extern int mkdir (const char *__path, __mode_t __mode)
__THROW __nonnull ((1));
......
fileio.c:
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<unistd.h>
int main(int argc, char* argv[])
{
if(mkdir(argv[1], S_IRWXU) < 0) {
printf("mkdir error!\n");
exit(1);
}
exit(0);
}
shell:
1)Create the new directory with rwx------ permission.
2) List all files
ubuntu@ip-172-31-23-227:~$ ./makedir.out testdir
ubuntu@ip-172-31-23-227:~$ ls -lrt
total 24
-rw-rw-r-- 1 ubuntu ubuntu 202 Aug 24 20:28 fileio.c~
-rw-rw-r-- 1 ubuntu ubuntu 210 Aug 24 20:30 fileio.c
-rwxrwxr-x 1 ubuntu ubuntu 9717 Aug 24 20:30 makedir.out
drwx------ 2 ubuntu ubuntu 4096 Aug 24 20:30 testdir
2. rmdir
rmdir system call can be used to remove directory
definition: same as above
fileio.c:
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<unistd.h>
int main(int argc, char* argv[])
{
if(rmdir(argv[1]) < 0) {
printf("mkdir error!\n");
exit(1);
}
exit(0);
}
shell:
ubuntu@ip-172-31-23-227:~$ ./removedir.out testdir
ubuntu@ip-172-31-23-227:~$ ls -lrt testdir
ls: cannot access testdir: No such file or directory
No comments:
Post a Comment