Search Suggest

File and Directory Maintenance

Bìa viết liên quan:
+ Users and Groups
+ File Permissions

1. chmod
 #include <sys/stat.h>  
int chmod(const char *path, mode_t mode);

Hàm chmod trong C tương tự như lệnh chmod trong Shell.
+ path
Đường dẫn đến file/directory.
+ mode
Quyền sẽ cấp cho file/directory.
mode là phép OR của các quyền được define trong stat.h như dưới đây:
 S_IRUSR                    /* Read by owner. */  
S_IWUSR /* Write by owner. */
S_IXUSR /* Execute by owner. */

S_IRGRP /* Read by group. */
S_IWGRP /* Write by group. */
S_IXGRP /* Execute by group. */

S_IROTH /* Read by others. */
S_IWOTH /* Write by others. */
S_IXOTH /* Execute by others. */

+ return
Trả về 0 nếu thành công; trả về -1 nếu lỗi, errno chỉ ra lỗi.

Ex:
chmod.c
 #include <stdio.h>  
#include <sys/stat.h>

int main(int argc, char **argv)
{

const char *path="/home/ninhld/Github/eslinuxprogramming/testfile.txt";
chmod(path, S_IRUSR|S_IRGRP|S_IROTH);

return 0;
}


Execute
 $ ls -l  
total 68
-rwxr-xr-x. 1 ninhld ninhld 139 Jul 26 10:21 testfile.txt
$
$ gcc chmod.c
$ ./a.out
$ ls -l
total 68
-r--r--r--. 1 ninhld ninhld 139 Jul 26 10:21 testfile.txt

2. chown
 #include <sys/types.h>  
#include <unistd.h>
int chown(const char *path, uid_t owner, gid_t group);

Thay chủ sở hữu, chương trình cần chạy ở chế độ superuser.
+ path
Đương dẫn đễn file.
+ owner
ID của  chủ sở hữu mới.
+ group
ID của nhóm sở hữu mới.
+ return
Trả về 0 nếu thành công; trả về -1 nếu lỗi, errno chỉ ra lỗi.

Ex:
chown.c (thay đổi chủ sở hữu và nhóm sở hữu thành root và root)
 #include <stdio.h>  
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char **argv)
{


uid_t uid = getuid();
gid_t gid = getgid();

printf("current uid: %d, gid: %d \n", uid, gid);

int ret;
ret = chown("/home/ninhld/Github/eslinuxprogramming/testfile.txt",
0,
0);
printf("ret: %d \n", ret);


return 0;
}


Execute
 $ ls -l  
total 72
-rwxr-xr-x. 1 ninhld ninhld 139 Jul 26 10:21 testfile.txt
$
$ gcc chown.c
$ su
#
# ./a.out
current uid: 0, gid: 0
ret: 0
#
# ls -l
total 72
-rwxr-xr-x. 1 root root 139 Jul 26 10:21 testfile.txt


3. unlink, link and symlink
 #include <unistd.h>  
int unlink(const char *path);
int link(const char *path1, const char *path2);
int symlink(const char *path1, const char *path2);

link tạo ra một alias cho một file.
symlink tạo alias cho file/directory.
unlink làm ngược lại so với link và symlink.
+ path1
Đường dẫn file/directory đã tồn tại.
+ path2
Đường dẫn đến file/directory là alias của path1, path2 sẽ được tự động tạo ra.
+ path
Đường dẫn đến file/directory là alias cần được loại bỏ.
+ return
Trả về o nếu thành công.

Ex:
link.c
 #include <stdio.h>  
#include <unistd.h>

int main(int argc, char **argv)
{

const char *from = "/home/ninhld/Github/eslinuxprogramming/testfile.txt";
const char *to = "/home/ninhld/Github/eslinuxprogramming/test-file.txt";

int ret = link(from, to); //or symlink
printf("ret: %d \n", ret);

return 0;
}


4. mkdir and rmdir
 #include <sys/types.h>  
#include <sys/stat.h>
int mkdir(const char *path, mode_t mode);

 #include <unistd.h>  
int rmdir(const char *path);

Các hàm tạo và xóa thư mục.

5. chdir and getcwd
 #include <unistd.h>  
int chdir(const char *path);
char *getcwd(char *buf, size_t size);

chdir chuyển thư mục, tương đương lệnh "cd" trong Shell.
getcwd ghi tên thư mục hiện tại vào buf, size = sizeof(buf) kích thước buf, tương tự lệnh pwd trong Shell.

Đăng nhận xét