Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2969634
  • 博文数量: 412
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 7374
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-25 15:15
个人简介

学习是一种信仰。

文章分类

全部博文(412)

文章存档

2014年(108)

2013年(250)

2010年(11)

2009年(43)

我的朋友

分类: LINUX

2009-07-11 14:14:47

本章学习内容:
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 < argc; i++)
        {
                printf("%s: ", argv[i]);
                if (lstat(argv[i], &buf) < 0)
                {
                        err_ret("lstat error");
                        continue;
                }
                if (S_ISREG(buf.st_mode))
                        ptr = "regular";
                else if (S_ISDIR(buf.st_mode))
                        ptr = "directory";
                else if (S_ISCHR(buf.st_mode))
                        ptr = "character special";
                else if (S_ISBLK(buf.st_mode))
                        ptr = "block special";
                else if (S_ISFIFO(buf.st_mode))
                        ptr = "fifo";
                else if (S_ISLNK(buf.st_mode))
                        ptr = "symbolic link";
                else if (S_ISSOCK(buf.st_mode))
                        ptr = "socket";
                else
                        ptr = "** unknown mode **";
                printf("%s\n", ptr);
        }
        exit(0);
}
(1)


2. access function

#include
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) < 0)
                err_ret("access error for %s", argv[1]);
        else
                printf("read access OK\n");
        if (open(argv[1], O_RDONLY) < 0)
                err_ret("open error for %s", argv[1]);
        else
                printf("open for reading OK\n");
        exit(0);
}
(1)

3. umask function
#include
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) < 0)
                err_sys("creat error for foo");
        umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
        if (creat("bar", RWRWRW) < 0)
                err_sys("creat error for bar");
        exit(0);
}
(1)

4. chmod and fchmod funtions

#include
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) < 0)
                err_sys("stat error for foo");
        if (chmod("foo", (statbuf.st_mode & ~S_IXGRP) | S_ISGID) < 0)
                err_sys("chmod error for foo");

        /* set absolute mode to "rw-r--r--" */

        if (chmod("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0)
                err_sys("chmod error for bar");

        exit(0);
}


5. chown, fchown and lchown functions
#include
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) < 0)
                err_sys("open error");
        if (unlink("test") < 0)
                err_sys("unlink error");
        printf("file unlinked\n");
        sleep(15);
        printf("done\n");
        exit(0);
}
(1)

8. symlink and readlink functions
#include
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 < argc; i++)
        {
                if (stat(argv[i], &statbuf) < 0)
                {       /* fetch current times */
                        err_ret("%s: stat error", argv[i]);
                        continue;
                }
                if ((fd = open(argv[i], O_RDWR | O_TRUNC)) < 0)
                { /* truncate */
                        err_ret("%s: open error", argv[i]);
                        continue;
                }
                close(fd);
                timebuf.actime  = statbuf.st_atime;
                timebuf.modtime = statbuf.st_mtime;
                if (utime(argv[i], &timebuf) < 0)
                 {              /* reset times */
                        err_ret("%s: utime error", argv[i]);
                        continue;
                }
        }
        exit(0);
}
(1)


10. mkdir and rmdir functions
#include
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") < 0)
                err_sys("chdir failed");
        printf("chdir to /tmp succeeded\n");
        exit(0);
}
(1)

Figure 4.24. getcwd函数的应用
#include "apue.h"

int main(void)
{
        char    *ptr;
        int     size;

        if (chdir("/usr/spool/uucppublic") < 0)
                err_sys("chdir failed");

        ptr = path_alloc(&size);        /* our own function */
        if (getcwd(ptr, size) == NULL)
                err_sys("getcwd failed");

        printf("cwd = %s\n", ptr);
        exit(0);
}
(1)

Figure 4.25. Print st_dev and st_rdev values
#include "apue.h"
#include

int main(int argc, char *argv[])
{
        int     i;
        struct stat     buf;

        for (i = 1; i < argc; i++) {
                printf("%s: ", argv[i]);
                if (stat(argv[i], &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)
 
阅读(2029) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~