讯为4412开发板嵌入式学习(五)文件和目录

一、man手册

Linux提供的手册对我们的编程非常有帮助,善用它能够让我们事半功倍。
一般指令man命令的8个分页:

  • man 1 一般命令。常见的linux命令,例如ls,cat等等。
  • man 2 用来放内核提供的系统调用或者函数。例如time、fork等。
  • man 3 C库函数。
  • man 4 特殊文件,例如设备和驱动程序。
  • man 5 文件格式。包括完全使用文本配置文件定制系统的操作,大量的配置文件,网络服务列表,可用的shell列表等等。
  • man 6 游戏和屏幕保护程序。
  • man 7 杂类文件。
  • man 8 系统管理命令,超级用户可能需要用到它们。

manual的框架

  • name(名称)给出命令、函数或文件格式的名称,以及对软件作用的单行的准确描述。
  • synopsis(大纲)简要描述如何使用这个软件。
  • description(描述)讨论特性、使用方法和命令行上可以使用的所有选项。
  • examples(示例)部分给出实用程序的一般用途、常见的特殊情况和解释。
  • See Also(参见)提供相关资料的引用,比如其他相关命令、重要的系统文件、行业标准规范等等。
  • 其它特殊部分。

二、获取文件状态函数

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

struct stat {
   
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

示例:

#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char* argv[])
{
   
	struct stat groupstat1,groupstat2,groupstat3;
	int fd, ret;
	
	if(argc<2){
   
		printf("Please input file path\n");
		return 1;
	}
	
//stat test
	ret = stat(argv[1], &groupstat1);
	if(ret){
   
		printf("Please make sure path is correct\n");
		return 1;
	}
	else{
   
		printf("stat function test, %s indoe is %ld\n", argv[1], groupstat1.st_ino);
	}

//fstat test	
	fd = open(argv[1], O_RDWR|O_NOCTTY|O_NDELAY);
	if(fd<0){
   
		printf("Please make sure path is correct\n");
		return 1;
	}
	ret = fstat(fd, &groupstat2);
	if(ret){
   
		printf("Please make sure path is correct\n");
	}
	else{
   
		printf("fstat function test, %s indoe is %ld\n", argv[1], groupstat2.st_ino);
	}
	
//lstat test
	ret = lstat(argv[1], &groupstat3);
	if(ret){
   
		printf("Please make sure path is correct\n");
		return 1;
	}
	else{
   
		printf("lstat function test, %s indoe is %ld\n", argv[1], groupstat3.st_ino);
	}
	
	return 0;
}

三、设置文件权限函数

#include <sys/stat.h>

int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);

mode为权限说明

S_ISUID (04000) set-user-ID (set process effective user ID on
execve(2))

S_ISGID (02000) set-group-ID (set process effective group ID on
execve(2); mandatory locking, as described in
fcntl(2); take a new file’s group from parent direc‐
tory, as described in chown(2) and mkdir(2))

S_ISVTX (01000) sticky bit (restricted deletion flag, as described in unlink(2))

S_IRUSR (00400) read by owner

S_IWUSR (00200) write by owner

S_IXUSR (00100) execute/search by owner (“search” applies for directories, and means that entries within the directory can be accessed)

S_IRGRP (00040) read by group

S_IWGRP (00020) write by group

S_IXGRP (00010) execute/search by group

S_IROTH (00004) read by others

S_IWOTH (00002) write by others

S_IXOTH (00001) execute/search by others

示例:

#include <stdio.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char* argv[])
{
   
	int fd, ret;
	
	if(argc<3){
   
		printf("Please input file path\n");
		return 1;
	}
	
//chmod test
	ret = chmod(argv[1], 0777);
	if(ret<0){
   
		printf("Please make sure path is correct\n");
		return 1;
	}
	else{
   
		printf("chmod 0777 %s succeed!\n", argv[1]);
	}

//fchmod test	
	fd = open(argv[2], O_RDWR|O_NOCTTY|O_NDELAY);
	if(fd<0){
   
		printf("Please make sure path is correct\n");
		return 1;
	}
	ret = fchmod(fd, 0555)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值