//获取文件的属性
extern int stat (__const char *__restrict __file, struct stat *__restrict __buf) __THROW __nonnull ((1, 2));
extern int fstat (int __fd, struct stat *__buf) __THROW __nonnull ((2));
extern int lstat (__const char *__restrict __file, struct stat *__restrict __buf) __THROW __nonnull ((1, 2));
具体属性如下:
struct stat
{
__dev_t st_dev; /* Device. */ //设备号
unsigned short int __pad1;
#ifndef __USE_FILE_OFFSET64
__ino_t st_ino; /* File serial number. */ //文件的i-node节点号
#else
__ino_t __st_ino; /* 32bit file serial number. */
#endif
__mode_t st_mode; /* File mode. */ //文件的访问权限和文件类型等属性信息
__nlink_t st_nlink; /* Link count. */ //文件的链接数
__uid_t st_uid; /* User ID of the file's owner. */ //文件的用户id
__gid_t st_gid; /* Group ID of the file's group.*/ //文件的组id
__dev_t st_rdev; /* Device number, if device. */
unsigned short int __pad2;
#ifndef __USE_FILE_OFFSET64
__off_t st_size; /* Size of file, in bytes. */ //文件大小
#else
__off64_t st_size; /* Size of file, in bytes. */
#endif
__blksize_t st_blksize; /* Optimal block size for I/O. */ //文件块大小
#ifndef __USE_FILE_OFFSET64
__blkcnt_t st_blocks; /* Number 512-byte blocks allocated. */ //文件所占块个数
#else
__blkcnt64_t st_blocks; /* Number 512-byte blocks allocated. */
#endif
#if defined __USE_MISC || defined __USE_XOPEN2K8
/* Nanosecond resolution timestamps are stored in a format
equivalent to 'struct timespec'. This is the type used
whenever possible but the Unix namespace rules do not allow the
identifier 'timespec' to appear in the <sys/stat.h> header.
Therefore we have to handle the use of this header in strictly
standard-compliant sources special. */
struct timespec st_atim; /* Time of last access. */
struct timespec st_mtim; /* Time of last modification. */
struct timespec st_ctim; /* Time of last status change. */
# define st_atime st_atim.tv_sec /* Backward compatibility. */
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
#else
__time_t st_atime; /* Time of last access. */
unsigned long int st_atimensec; /* Nscecs of last access. */
__time_t st_mtime; /* Time of last modification. */
unsigned long int st_mtimensec; /* Nsecs of last modification. */
__time_t st_ctime; /* Time of last status change. */
unsigned long int st_ctimensec; /* Nsecs of last status change. */
#endif
#ifndef __USE_FILE_OFFSET64
unsigned long int __unused4;
unsigned long int __unused5;
#else
__ino64_t st_ino; /* File serial number. */
#endif
};
st_mode的文件类型宏:
#define __S_ISTYPE(mode, mask) (((mode) & __S_IFMT) == (mask))
#define S_ISDIR(mode) __S_ISTYPE((mode), __S_IFDIR)
#define S_ISCHR(mode) __S_ISTYPE((mode), __S_IFCHR)
#define S_ISBLK(mode) __S_ISTYPE((mode), __S_IFBLK)
#define S_ISREG(mode) __S_ISTYPE((mode), __S_IFREG)
#ifdef __S_IFIFO
# define S_ISFIFO(mode) __S_ISTYPE((mode), __S_IFIFO)
#endif
#ifdef __S_IFLNK
# define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
#endif
#if defined __USE_BSD && !defined __S_IFLNK
# define S_ISLNK(mode) 0
#endif
#if (defined __USE_BSD || defined __USE_UNIX98 || defined __USE_XOPEN2K) \
&& defined __S_IFSOCK
# define S_ISSOCK(mode) __S_ISTYPE((mode), __S_IFSOCK)
#elif defined __USE_XOPEN2K
# define S_ISSOCK(mode) 0
#endif
st_mode访问权限宏:
#define S_ISUID __S_ISUID /* Set user ID on execution. */
#define S_ISGID __S_ISGID /* Set group ID on execution. */
#if defined __USE_BSD || defined __USE_MISC || defined __USE_XOPEN
/* Save swapped text after use (sticky bit). This is pretty well obsolete. */
# define S_ISVTX __S_ISVTX
#endif
#define S_IRUSR __S_IREAD /* Read by owner. */
#define S_IWUSR __S_IWRITE /* Write by owner. */
#define S_IXUSR __S_IEXEC /* Execute by owner. */
/* Read, write, and execute by owner. */
#define S_IRWXU (__S_IREAD|__S_IWRITE|__S_IEXEC)
#if defined __USE_MISC && defined __USE_BSD
# define S_IREAD S_IRUSR
# define S_IWRITE S_IWUSR
# define S_IEXEC S_IXUSR
#endif
#define S_IRGRP (S_IRUSR >> 3) /* Read by group. */
#define S_IWGRP (S_IWUSR >> 3) /* Write by group. */
#define S_IXGRP (S_IXUSR >> 3) /* Execute by group. */
/* Read, write, and execute by group. */
#define S_IRWXG (S_IRWXU >> 3)
#define S_IROTH (S_IRGRP >> 3) /* Read by others. */
#define S_IWOTH (S_IWGRP >> 3) /* Write by others. */
#define S_IXOTH (S_IXGRP >> 3) /* Execute by others. */
/* Read, write, and execute by others. */
#define S_IRWXO (S_IRWXG >> 3)
//测试进程实际用户ID对文件的访问权限
/* Test for access to NAME using the real UID and real GID. */
extern int access (__const char *__name, int __type) __THROW __nonnull ((1));
__type可取值:
/* Values for the second argument to access.
These may be OR'd together. */
#define R_OK 4 /* Test for read permission. */
#define W_OK 2 /* Test for write permission. */
#define X_OK 1 /* Test for execute permission. */
#define F_OK 0 /* Test for existence. */
//设置文件访问权限屏蔽位,哪个权限为(如S_IRUID)置位了,就无法设置这个权限
extern __mode_t umask (__mode_t __mask) __THROW;
//修改文件的st_mode,也就是修改文件的访问权限
extern int chmod (__const char *__file, __mode_t __mode) THROW __nonnull ((1));
extern int fchmod (int __fd, __mode_t __mode) __THROW;
extern int lchmod (__const char *__file, __mode_t __mode) THROW __nonnull ((1));
//修改文件的用户ID和组ID,不修改则赋值为-1
extern int chown (__const char *__file, __uid_t __owner, __gid_t __group) THROW __nonnull ((1)) __wur;
extern int fchown (int __fd, __uid_t __owner, __gid_t __group) __THROW __wur;
extern int lchown (__const char *__file, __uid_t __owner, __gid_t __group) THROW __nonnull ((1)) __wur;
//文件截断为参数length所示的长度
extern int truncate (__const char *__file, __off_t __length) __THROW __nonnull ((1)) __wur;
extern int ftruncate (int __fd, __off_t __length) __THROW __wur;
extern int remove (__const char *__filename) __THROW;
extern int rename (__const char *__old, __const char *__new) __THROW;
extern int mkdir (__const char *__path, __mode_t __mode) __THROW __nonnull ((1));
extern int rmdir (__const char *__path) __THROW __nonnull ((1));
#include "unp.h"
#include "apue.h"
#include <stdio.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <ctype.h>
//#include <linux/init.h>
//#include <linux/module.h>
int main(int argc, char *argv[])
{
int rv;
struct tm f_tm;
struct stat f_stat;
char curr_dir[100];
if((rv = stat("/home/yang/exam_file/exam_file_op.dat", &f_stat)) < 0) {
puts("get file stat info error");
}
if(S_ISREG(f_stat.st_mode)) {
puts("this file is a regular file");
}
if(S_ISDIR(f_stat.st_mode)) {
puts("this file is a directory file");
}
if(__S_ISTYPE(f_stat.st_mode, S_ISUID)) {
puts("this file is set uid");
}
else {
puts("this file is not set uid");
}
if(__S_ISTYPE(f_stat.st_mode, S_ISGID)) {
puts("this file is set gid");
}
else {
puts("this file is not set gid");
}
if( chmod("/home/yang/exam_file/exam_file_op.dat", f_stat.st_mode | S_ISUID) < 0) {
puts("change mode error");
}
else {
puts("change mode ok");
}
if(localtime_r(&f_stat.st_atim, &f_tm) == NULL) {
puts("time format translationg failed");
}
printf("time: %d\n", f_tm.tm_year);
printf("file size: %d\n", (int)f_stat.st_size);
printf("file blk size: %d\n", (int)f_stat.st_blksize);
printf("file blk cnt: %d\n", (int)f_stat.st_blocks);
if(getcwd(curr_dir, sizeof(curr_dir)) == NULL ) {
puts("get cwd error");
}
else {
puts(curr_dir);
}
strcat(curr_dir, "/newdir");
puts(curr_dir);
if(rmdir("curr_dir") < 0) {
err_sys("mkdir error");
}
if(mkdir("/home/yang/exam_file/newdir",S_IRWXU | S_IRWXG) < 0) {
err_sys("mkdir error");
}
if(rename("/home/yang/exam_file/exam_file_op.dat", "/home/yang/exam_file/exam_file_op2.dat") < 0) {
err_sys("rename error");
}
exit(0);
}