#include
#include
#include
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);
use struct stat to get file information.
---------------------------------------------------------
#include
#include
int utime(const char *filename, const struct utimbuf *times);
#include
int utimes(const char *filename, const struct timeval times[2]);
We know a file(inode) has three time:
--last access time: last time file content is read.
--last modification time: last time file content is changed.
--last state changed time:last time file properity is changed,
that means inode is modified.
struct utimbuf {
time_t actime; /* access time */
time_t modtime; /* modification time */
};
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
-------------------------------------------------------------
#include
int access(const char *pathname, int mode);
R_OK: if can read
W_OK: if can write
X_OK: if can execute
F_OK: if file exists
---------------------------------------------------------
#include
#include
mode_t umask(mode_t mask);
change umask to mask.
Note: if a bit in mask is 1, the related bit in file
access permission bit will be 0.
Common umask:
0002: rwxrwxr-x only Others can not write.
0022: rwxr-xr-x Group and Others can not write.
0027: rwxr-x--- Group can not read and Others can not access.
Note:
This will effect only the process itself.
-----------------------------------------------------------
#include
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);
Change file access permission.
Note:
We should get mode via stat() first, then only
change bits we need.
------------------------------------------------------------
#include
int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, gid_t group);
if owner is -1, owner uid of file will not change, and so with
group.
------------------------------------------------------------
#include
#include
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
changed file size to the specified length.
If file size is shorted than length, '\0'(NULL) will be appended.
--------------------------------------------------------------
#include
int link(const char *oldpath, const char *newpath);
int unlink(const char *pathname);
create or delete a directory entry to a special file.
unlink doesn't follow symbol link.
--------------------------------------------------------------
#include
int remove(const char *pathname);
This handles file and directory both. When file it calls unlink(),
when directory, it calls rmdir().
--------------------------------------------------------------
#include
int rename(const char *oldpath, const char *newpath);
This function doesn't follow symbol link.
--------------------------------------------------------------
#include
int symlink(const char *oldpath, const char *newpath);
ssize_t readlink(const char *path, char *buf, size_t bufsiz);
create and read a symbol link content.
--------------------------------------------------------------
#include
#include
int mkdir(const char *pathname, mode_t mode);
#include
int rmdir(const char *pathname);
rmdir() only can remove an empty directory.
--------------------------------------------------------------
#include
#include
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
int closedir(DIR *dirp);
open and close a directory returning DIR *.
--------------------------------------------------------------
#include
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file; not supported
by all file system types */
char d_name[256]; /* filename */
};
---------------------------------------------------------------
#include
#include
void rewinddir(DIR *dirp);
long telldir(DIR *dirp);
void seekdir(DIR *dirp, long offset);
rewinddir() set the current positon to the begging of the directory.
telldir() return the current position in a DIR.
seekdir() set positon where next readdir() will begin to read
in directory.
----------------------------------------------------------------
#include
int chdir(const char *path);
int fchdir(int fd);
char *getcwd(char *buf, size_t size);
char *getwd(char *buf);
char *get_current_dir_name(void);
chdir() and fchdir() changed process working directory to path.
getcwd() and so on return the current working directory.
阅读(713) | 评论(0) | 转发(0) |