⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file_stat.c

📁 实现了linux系统对文件基本信息的获取功能
💻 C
字号:
/**
 * * file_stat.c
 * *
 * *To show file attributes  
 * *
 * * Dec 8, 2008
 * * DJ created. 
 * * 
 * *Note { usage : file_stat <FILE_NAME> }
 * *
 * * This program is free software; you can redistribute it and/or modify
 * * it under the terms of the GNU General Public License as published by
 * * the Free Software Foundation; either version 2 of the License, or
 * * (at your option) any later version.
 * *
 * * This program is distributed in the hope that it will be useful,
 * * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * * GNU General Public License for more details.
 * *
 * */

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 50

char *check_filetype(struct stat *buf, char *ptr);
char *check_premission(struct stat *buf, char *str);

int 
main(int argc, char *argv[]){

	int				fd;
	struct stat  	buf;
	char 			str[MAX];
	char 			*ptr;

	if (argc != 2){

		printf("usage: file_stat <FILE_NAME>\n");
		exit(1);
	}

	if (access(argv[1], R_OK) < 0){
		
		printf("%s: no such file or directory\n", argv[1]);
		exit(1);
	}

	if ((fd=open(argv[1], O_RDWR)) < 0){
		
		printf("open error!\n");
		exit(1);
	}

	if (fstat(fd, &buf) < 0){
		
		printf("fail to get file attributes"); 
		exit(1);
	}

	printf("file name: %s\n", argv[1]);
	printf("file type: %s\n", check_filetype(&buf, ptr));
	printf("access premission: %s\n", check_premission(&buf, str));
	printf("link count: %u\n", (unsigned int)buf.st_nlink);
	printf("user ID of owner: %u\n", (unsigned int)buf.st_uid);
	printf("group ID of owner: %u\n", (unsigned int)buf.st_gid);
	printf("time of last access: %s", ctime(&buf.st_atime));
	printf("time of last modification: %s", ctime(&buf.st_mtime));
	printf("time of last file status change: %s", ctime(&buf.st_ctime));
					
	exit(0);
}


char * 
check_filetype(struct stat *buf, char *ptr){

	
	ptr = NULL;

	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";
	
	return ptr;

}

char *
check_premission(struct stat *buf, char *str){

	
	if (buf->st_mode & S_IRUSR) 	
		strncat(str, "usr_R ", 6);
	if (buf->st_mode & S_IWUSR)
		strncat(str, "usr_W ", 6);
	if (buf->st_mode & S_IXUSR)
		strncat(str, "usr_X ", 6);
	if (buf->st_mode & S_IRGRP)
		strncat(str, "grp_R ", 6);
	if (buf->st_mode & S_IWGRP)
		strncat(str, "grp_W ", 6);
	if (buf->st_mode & S_IXGRP)
		strncat(str, "grp_X ", 6);
	if (buf->st_mode & S_IROTH)
		strncat(str, "oth_R ", 6);
	if (buf->st_mode & S_IWOTH) 
		strncat(str, "oth_W ", 6);
	if (buf->st_mode & S_IXOTH)
		strncat(str, "oth_X ", 6);
	
	return	str;	

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -