- 论坛徽章:
- 0
|
本章学习内容:
1. stat, fstat, and lstat functions
#include
int stat(const char *restrict pathname, struct stat *restrict buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *restrict pathname, struct stat *restrict buf);
All three ruturn: 0 if OK, -1 on error.
其中:
struct stat {
mode_t st_mode; /* file type & mode (permissions) */
ino_t st_ino; /* i-node number (serial number) */
dev_t st_dev; /* device number (file system) */
dev_t st_rdev; /* device number for special files */
nlink_t st_nlink; /* number of links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
off_t st_size; /* size in bytes, for regular files */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last file status change */
blksize_t st_blksize; /* best I/O block size */
blkcnt_t st_blocks; /* number of disk blocks allocated */
};
File type macros in :
S_ISREG()——regular file——普通文件;
S_ISDIR()——directory——目录文件;
S_ISCHR()——character special file——字符特殊文件;
S_ISBLK()——block special file——块特殊文件;
S_ISFIFO()——pipe or FIFO——管道或FIFO;
S_ISLNK()——symbolic link——符号连接;
S_ISSOCK()——socket——套接字。
Figure 4.3. Print type of file for each command-line argument
#include "apue.h"
#include
int main(int argc, char *argv[])
{
int i;
struct stat buf;
char *ptr;
for (i = 1; i
int access(const char *pathname, int mode);
Returns: 0 if OK, -1 on error
The mode constants for access function from
mode description
R_OK test for read permission
W_OK test for write permission
X_OK test for execute permission
F_OK test for existence of file
The nine file access permission bits, from
st_mode mask meaning
S_IRUSR user-read
S_IWUSR user-write
S_IXUSR user-execute
S_IRGRP group-read
S_IWGRP group-write
S_IXGRP group-execute
S_IROTH other-read
S_IWOTH other-write
S_IXOTH other-execute
figure4.8: access 函数的应用
#include "apue.h"
#include
int main(int argc, char *argv[])
{
if (argc != 2)
err_quit("usage: a.out ");
if (access(argv[1], R_OK)
mode_t umask(mode_t cmask);
Ruturns: previous file mode creation mask
figure4.9. umask 函数的应用
#include "apue.h"
#include
#include
#define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
int main(void)
{
umask(0);
if (creat("foo", RWRWRW)
int chmod(const char *pathname, mode_t mode);
int fchmod(int filedes, mode_t mode);
Both return: 0 if OK, -1 on error
Figure4.12. chmod函数的应用
#include "apue.h"
#include
int main(void)
{
struct stat statbuf;
/* turn on set-group-ID and turn off group-execute */
if (stat("foo", &statbuf)
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int filedes, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
All three return: 0 if OK, -1 on error.
6. truncate and ftruncate functions
#include
int truncate(const char *pathname, off_t length);
int ftruncate(int filedes, off_t length);
Both return: 0 if OK, -1 on error.
7. link, unlink, remove and rename functions
#include
int link(const char *existingpath, const char *newpath);
int unlink(const char *pathname);
Both ruturnL: 0 if OK, -1 on error;
#include
int remove(const char *pathname);
int rename(const char *oldname, const char *newname);
Both return: 0 if OK, -1 on error.
Figure 4.16. Open a file and then unlink it
#include "apue.h"
#include
int main(void)
{
if (open("test", O_RDWR)
int symlink(const char *actualpath const char *sympath);
Returns: 0 if OK, -1 on error;
ssize_t readlink(const char* restrict pathname, char *restrict buf, size_t bufsize);
Returns: number of bytes read if OK, -1 on error.
9. utime function
#inclue
int utime(const char *pathname, const struct utimbuf *times);
Returns: 0 if OKk, -1 on error.
struct utimbuf
{
time_t acttime; /* access time */
time_t modtime; /* modification time */
}
Figure 4.21. utime函数的应用
#include "apue.h"
#include
#include
#include
int main(int argc, char *argv[])
{
int i, fd;
struct stat statbuf;
struct utimbuf timebuf;
for (i = 1; i
int mkdir(const char *pathname, mode_t mode);
Returns: 0 if OK, -1 on error;
#include
int rmdir(const char *pathname);
Returns: 0 if OK, -1 on error.
11. reading directories functions
#include
DIR *opendir(const char *pathname);
Returns: pointer if OK, NULL on error
struct dirent *readdir(DIR *dp);
Returns: pointer if OK, NULL at end of directory or error
void rewinddir(DIR *dp);
int closedir(DIR *dp);
Returns: 0 if OK, 1 on error
long telldir(DIR *dp);
Returns: current location in directory associated with dp
void seekdir(DIR *dp, long loc);
struct dirent {
ino_t d_ino; /* i-node number */
char d_name[NAME_MAX + 1]; /* null-terminated filename */
}
Figure 4.22. Recursively descend a directory hierarchy, counting file types
12. chdir, fchdir and getcwd functions
#inclde
int chdir(const char *pathname);
int fchdir(int filedes);
Both return: 0 if OK, -1 on error;
cher *getcwd(char *buf, size_t size);
Return: buf if OK, NULL on error.
Figure 4.23. chdir函数的应用
#include "apue.h"
int main(void)
{
if (chdir("/tmp")
int main(int argc, char *argv[])
{
int i;
struct stat buf;
for (i = 1; i < argc; i++) {
printf("%s: ", argv);
if (stat(argv, &buf) < 0) {
err_ret("stat error");
continue;
}
printf("dev = %d/%d", major(buf.st_dev), minor(buf.st_dev));
if (S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode)) {
printf(" (%s) rdev = %d/%d",
(S_ISCHR(buf.st_mode)) ? "character" : "block",
major(buf.st_rdev), minor(buf.st_rdev));
}
printf("\n");
}
exit(0);
}
(1)
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/95093/showart_1993090.html |
|