Linux-File attributes and directory-related system calls

Source: Internet
Author: User
Tags lstat symlink

Stat ()/fstat ()/lstat () system call
Feature Get file status header file#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>Function prototypesint Stat(const char*file_name, structStat *buf);intFstat (intFiledes, structStat *buf);int Lstat(const char*file_name, structStat *buf), andStat() Difference: When connecting to a symbol,Lstat() returns the return value of the connection's own state returned when successful0otherwise1
struct STAT structure definition
struct STAT {mode_t st_mode; /*fileType & mode*/ino_t St_ino; /*inode Number(Serial Number) */dev_t St_rdev; /*device Number(fileSystem) */nlink_t St_nlink; /*linkCount*/uid_t St_uid; /*user ID ofowner*/gid_t St_gid; /*group ID ofowner*/off_t st_size; /*size of file,inchbytes*/time_t st_atime; /* Time  of  Lastaccess*/time_t st_mtime; /* Time  of  Lastmodification*/time_t st_ctime; /* Time  of  Last fileStatus change*/long st_blksize; /*optimal Block Size fori/o*/long st_blocks; /* Number  +-byte blocks allocated*/};
Access () system calls
功能按实际用户ID和实际组ID测试文件存取权限头文件#include <unistd.h>函数原型int access(constcharint mode);返回值成功时返回0否则-1mode参数说明R_OKW_OKX_OKF_OK
Umask () system call
功能为进程设置文件存取权限屏蔽字,并返回以前的值头文件#include <sys/types.h>#include <sys/stat.h>函数原型mode_t umask(mode_t mask);
chmod ()/fchmod () system call
功能更改文件权限头文件#include <sys/types.h>#include <sys/stat.h>函数原型int chmod(constchar *path, mode_t mode);int fchmod(int fildes, mode_t mode);返回值成功时返回0失败返回-1
Chown ()/fchown ()/lchown () system call
功能变更文件宿主头文件#include <sys/types.h>#include <unistd.h>函数原型int chown(constchargroup);int fchown(intgroup);int lchown(constchargroup);若path为符号连接, lchown()改变连接本身的所有者或组,chown()则改变连接所指向文件的所有者或组返回值成功时返回0失败返回-1
Link ()/unlink () system call
功能创建/删除一个硬链接头文件#include <unistd.h>函数原型int link(constcharconstchar *newpath);int unlink(constchar *pathname);删除一个文件的目录项并减少它的链接数返回值成功时返回0失败返回-1
Symlink ()/readlink () system call
功能创建/删除一个符号链接头文件#include <unistd.h>函数原型int symlink(constcharconstchar *newpath);返回值成功时返回0否则-1int readlink(constcharchar *buf, size_t bufsiz);返回值成功时返回放置在buffer中的字符数否则-1
Directory Processing related system calls
创建/删除目录mkdir()/rmdir()更改/获取目录chdir()/fchdir(), getcwd()读取目录信息opendir()/closedir()readdir()telldir()seekdir()
mkdir ()/rmdir () system call
功能创建/删除一个空目录头文件#include <sys/stat.h>#include <sys/types.h>函数原型int mkdir(constchar *pathname, mode_t mode);int rmdir(constchar *pathname);返回值成功时返回0失败时返回-1
ChDir ()/fchdir () system call
功能更改工作目录头文件#include <unistd.h>函数原型int chdir(constchar *path);int fchdir(int fd);返回值成功时返回0失败时返回-1说明当前工作目录是进程的属性,所以该函数只影响调用chdir的进程本身
GETCWD () system call
功能获得当前工作目录的绝对路径头文件#include <unistd.h>函数原型char *getcwd(char *buf, size_t size);返回值成功时返回buf出错则为NULL
Reading directory information
 data structure dir , struct Dirent Basic Operations opendir   () /closedir   ()   Readdir   ()  telldir   ()  seekdir   ()   
DIR目录流对象头文件 <dirent.h>定义形式typedefstruct _dirstream DIR;struct dirent目录项头文件<dirent.h>定义ino_t d_ino;       /* inode号*/char  1];    /* 文件名*/
Directory Basic Operations
功能打开、关闭、读、定位头文件#include <sys/types.h>#include <dirent.h>*opendir*name);int*dir*readdir*dirtelldir*dir//seekdir*dir, off_t offset);
fopen () function
功能打开文件流头文件#include <stdio.h>函数原型FILE *fopen(constcharconstchar *mode);mode参数说明“r”:  以读方式打开文本文件“w”: 以写方式创建一个文件,覆盖老文件“a”: 以追加方式打开文件“r+”:  以读写方式打开一个现存文件“w+”:以读写方式打开一个文件若不存在,将创建之若存在,将覆盖原文件“a+”: 以读及追加方式打开文件,若不存在,将创建之“b”:以二进制模式打开文件“t”:以文本模式打开文件

Linux-File attributes and directory-related system calls

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.