linux -- open /acess/ftruncate/lstat 函数

open

头文件:#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>

定义函数:

    int open(const char * pathname, int flags);
    int open(const char * pathname, int flags, mode_t mode);

函数说明:

参数 pathname 指向欲打开的文件路径字符串. 下列是参数flags 所能使用的旗标:

O_RDONLY 以只读方式打开文件
O_WRONLY 以只写方式打开文件
O_RDWR 以可读写方式打开文件. 上述三种旗标是互斥的, 也就是不可同时使用, 但可与下列的旗标利用OR(|)运算符组合.
O_CREAT 若欲打开的文件不存在则自动建立该文件.
O_EXCL 如果O_CREAT 也被设置, 此指令会去检查文件是否存在. 文件若不存在则建立该文件, 否则将导致打开文件错误. 此外, 若O_CREAT 与O_EXCL 同时设置, 并且欲打开的文件为符号连接, 则会打开文件失败.
O_NOCTTY 如果欲打开的文件为终端机设备时, 则不会将该终端机当成进程控制终端机.
O_TRUNC 若文件存在并且以可写的方式打开时, 此旗标会令文件长度清为0, 而原来存于该文件的资料也会消失.
O_APPEND 当读写文件时会从文件尾开始移动, 也就是所写入的数据会以附加的方式加入到文件后面.
O_NONBLOCK 以不可阻断的方式打开文件, 也就是无论有无数据读取或等待, 都会立即返回进程之中.
O_NDELAY 同O_NONBLOCK.
O_SYNC 以同步的方式打开文件.
O_NOFOLLOW 如果参数pathname 所指的文件为一符号连接, 则会令打开文件失败.
O_DIRECTORY 如果参数pathname 所指的文件并非为一目录, 则会令打开文件失败。 注:此为Linux2. 2 以后特有的旗标, 以避免一些系统安全问题. 

参数mode 则有下列数种组合, 只有在建立新文件时才会生效, 此外真正建文件时的权限会受到umask 值所影响, 因此该文件权限应该为 (mode-umaks).

S_IRWXU00700 权限, 代表该文件所有者具有可读、可写及可执行的权限.
S_IRUSR 或S_IREAD, 00400 权限, 代表该文件所有者具有可读取的权限.
S_IWUSR 或S_IWRITE, 00200 权限, 代表该文件所有者具有可写入的权限.
S_IXUSR 或S_IEXEC, 00100 权限, 代表该文件所有者具有可执行的权限.
S_IRWXG 00070 权限, 代表该文件用户组具有可读、可写及可执行的权限.
S_IRGRP 00040 权限, 代表该文件用户组具有可读的权限.
S_IWGRP 00020 权限, 代表该文件用户组具有可写入的权限.
S_IXGRP 00010 权限, 代表该文件用户组具有可执行的权限.
S_IRWXO 00007 权限, 代表其他用户具有可读、可写及可执行的权限.
S_IROTH 00004 权限, 代表其他用户具有可读的权限
S_IWOTH 00002 权限, 代表其他用户具有可写入的权限.
S_IXOTH 00001 权限, 代表其他用户具有可执行的权限.

返回值:若所有欲核查的权限都通过了检查则返回0 值, 表示成功, 只要有一个权限被禁止则返回-1.

错误代码:

EEXIST 参数pathname 所指的文件已存在, 却使用了O_CREAT 和O_EXCL 旗标.
EACCESS 参数pathname 所指的文件不符合所要求测试的权限.
EROFS 欲测试写入权限的文件存在于只读文件系统内.
EFAULT 参数pathname 指针超出可存取内存空间.
EINVAL 参数mode 不正确.
ENAMETOOLONG 参数 pathname 太长.
ENOTDIR 参数pathname 不是目录.
ENOMEM 核心内存不足.
ELOOP 参数pathname 有过多符号连接问题.
EIO I/O 存取错误.
1pread()

形式:#include<unistd.h>

      ssize_t  pread (int filedes,   void *buf,  size_t  nbytes,  off_t  offset );

  成功:返回读到的字节数;出错:返回-1;到文件结尾:返回0

原因:由于lseek和read 调用之间,内核可能会临时挂起进程,所以对同步问题造成了问题,调用pread相当于顺序调用了lseek 和 read,这两个操作相当于一个捆绑的原子操作。

实现:文件(由filedes所指)-读nbytes字节->内存buf中。

补充:调用pread时,无法中断其定位和读操作,另外不更新文件指针。

(2pwrite()

形式:#include<unistd.h>

      ssize_t  pwrite (int filedes,   const void *buf,  size_t  nbytes,  off_t  offset );

  成功:返回已写的字节数;出错:返回-1;

原因:由于lseek和write 调用之间,内核可能会临时挂起进程,所以对同步问题造成了问题,调用pwrite相当于顺序调用了lseek 和 write,这两个操作相当于一个捆绑的原子操作。

实现:文件(由filedes所指)<-写nbytes字节-内存buf中。

补充:调用pwrite时,无法中断其定位和读操作,另外不更新文件指针。

附加说明:使用 access()作用户认证方面的判断要特别小心, 例如在access()后再作open()空文件可能会造成系统安全上的问题.

范例

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{
    
    
    int fd, size;
    char s[] = "Linux Programmer!\n", buffer[80];
    fd = open("/tmp/temp", O_WRONLY|O_CREAT);
    write(fd, s, sizeof(s));
    close(fd);
    fd = open("/tmp/temp", O_RDONLY);
    size = read(fd, buffer, sizeof(buffer));
    close(fd);
    printf("%s", buffer);
}

执行

Linux Programmer!

acess

access函数功能描述:检查调用进程是否可以对指定的文件执行某种操作。用法:
#include<unistd.h>
#include<fcntl.h>
int access(constchar*path…)
access函数
功能描述: [喝小酒的网摘]http://blog.const.net.cn/a/17126.htm
检查调用进程是否可以对指定的文件执行某种操作。

用法:
#include <unistd.h>
#include <fcntl.h>

int access(const char *pathname, int mode);

参数:
pathname: 需要测试的文件路径名。
mode: 需要测试的操作模式,可能值是一个或多个R_OK(可读?), W_OK(可写?), X_OK(可执行?) 或 F_OK(文件存在?)组合体。

返回说明:
成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EINVAL: 模式值无效
EACCES: 文件或路径名中包含的目录不可访问
ELOOP : 解释路径名过程中存在太多的符号连接
ENAMETOOLONG:路径名太长
ENOENT: 路径名中的目录不存在或是无效的符号连接
ENOTDIR: 路径名中当作目录的组件并非目录
EROFS: 文件系统只读
EFAULT: 路径名指向可访问的空间外
EIO: 输入输出错误
ENOMEM: 不能获取足够的内核内存
ETXTBSY:对程序写入出错

linux 判断文件是否存在的access()示例:

#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <fcntl.h>  
  
int main()  
{
    
      
    if((access("test.c",F_OK))!=-1)  
    {
    
      
        printf("文件 test.c 存在.\n ");  
    }  
    else  
    {
    
      
        printf("test.c 不存在!\n ");  
    }  
  
    if(access("test.c",R_OK)!=-1)  
    {
    
      
        printf("test.c 有可读权限\n ");  
    }  
    else  
    {
    
      
        printf("test.c 不可读. \n");  
    }  
  
    if(access("test.c",W_OK)!=-1)  
    {
    
      
        printf("test.c 有可写权限\n ");  
    }  
    else  
    {
    
      
        printf("test.c 不可写.\n ");  
    }  
    if(access("test.c",X_OK)!=-1)  
    {
    
      
        printf("test.c 有可执行权限\n ");  
    }  
    else  
    {
    
      
        printf("test.c 不可执行.\n ");  
    }  
  
    return 0;  
}  

在这里插入图片描述

ftruncate

首先 man ftruncate 看下帮助手册

NAME
       truncate, ftruncate - truncate a file to a specified length

SYNOPSIS
       int truncate(const char *path, off_t length);
       int ftruncate(int fd, off_t length);

DESCRIPTION
       The truncate() and ftruncate() functions cause the regular file named by path or referenced by fd to be truncated to a size of precisely length bytes.
       If the file previously was larger than this size, the extra data is lost.  If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0').
       The file offset is not changed.
       If  the  size  changed,  then the st_ctime and st_mtime fields (respectively, time of last status change and time of last modification; see stat(2)) for the file are updated, and the set-user-ID and
       set-group-ID permission bits may be cleared.
       With ftruncate(), the file must be open for writing; with truncate(), the file must be writable.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
int main(void)
{
    
    
	int fd;
 
	const char *s1 = "0123456789";
	const char *s2 = "abcde";
 
	fd = open("test.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
	/* if error */
 
	write(fd, s1, strlen(s1));
 
	ftruncate(fd, 0);
	// lseek(fd, 0, SEEK_SET);
	
	write(fd, s2, strlen(s2));
 
	close(fd);
 
	return 0;
}

在这里插入图片描述
在这里插入图片描述
在使用ftruncate后,在重新写入需要使用lseek重新定位

lstat

 #include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysmacros.h>

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

    if (argc != 2) {
    
    
        fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (lstat(argv[1], &sb) == -1) {
    
    
        perror("lstat");
        exit(EXIT_FAILURE);
    }

    printf("ID of containing device:  [%lx,%lx]\n",(long) major(sb.st_dev), (long) minor(sb.st_dev));

    printf("File type: ");

    switch (sb.st_mode & S_IFMT) {
    
    
        case S_IFBLK:  printf("block device\n");            break;
        case S_IFCHR:  printf("character device\n");        break;
        case S_IFDIR:  printf("directory\n");               break;
        case S_IFIFO:  printf("FIFO/pipe\n");               break;
        case S_IFLNK:  printf("symlink\n");                 break;
        case S_IFREG:  printf("regular file\n");            break;
        case S_IFSOCK: printf("socket\n");                  break;
        default:       printf("unknown?\n");                break;
    }

    printf("I-node number:            %ld\n", (long) sb.st_ino);
    printf("Mode:                     %lo (octal)\n",(unsigned long) sb.st_mode);
    printf("Link count:               %ld\n", (long) sb.st_nlink);
    printf("Ownership:                UID=%ld   GID=%ld\n",(long) sb.st_uid, (long) sb.st_gid);
    printf("Preferred I/O block size: %ld bytes\n",(long) sb.st_blksize);
    printf("File size:                %lld bytes\n",(long long) sb.st_size);
    printf("Blocks allocated:         %lld\n",(long long) sb.st_blocks);
    printf("Last status change:       %s", ctime(&sb.st_ctime));
    printf("Last file access:         %s", ctime(&sb.st_atime));
    printf("Last file modification:   %s", ctime(&sb.st_mtime));

    exit(EXIT_SUCCESS);
}

在这里插入图片描述

参考资料
1.https://blog.csdn.net/wuxinyanzi/article/details/13620741
2.https://blog.csdn.net/a_ran/article/details/43562429

猜你喜欢

转载自blog.csdn.net/alpha_love/article/details/113838193
今日推荐