admin管理员组

文章数量:1516870

目录


目录操作

getcwd

函数功能:获取当前位置的绝对路径(相当于中断指令的pwd)

函数头文件:

#include <unistd.h>

函数原型:

char *getcwd(char *buf, size_t size);

函数参数:

buf -- 保存获取的位置

size -- 保存空间的长度

函数返回值:

char * -- 保存获取的当前位置的首地址

chdir

函数功能;

切换路径位置

函数头文件:

#include <unistd.h>

函数原型:

int chdir(const char *path);

函数参数;

path -- 切换的目录位置

函数返回值:

0--成功 -1 失败

mkdir

函数功能;

创建目录

函数头文件:

#include <sys/stat.h>

#include <sys/types.h>

函数原型:

int mkdir(const char *pathname, mode_t mode);

函数参数;

pathname -- 创建的目录名称

mode -- 权限 -- 必须给x权限 -- 0755

函数返回值:

0-- 成功   -1 -- 失败

chmod

函数功能;

更改权限

函数头文件:

#include <sys/stat.h>

函数原型:

int chmod(const char *pathname, mode_t mode);

函数参数;

pathname -- 要修改的文件名/目录名

mode -- 权限

函数返回值:

0-- 成功   -1 -- 失败

opendir

函数功能;

打开目录

函数头文件:

#include <sys/types.h>

#include <dirent.h>

函数原型:

DIR *opendir(const char *name);

函数参数;

name -- 目录名称(可以使用./当前目录或../上一级目录)

函数返回值:

DIR* -- 目录描述指针

readdir

函数功能;

读取目录中的文件(目录)信息

函数头文件:

#include <dirent.h>

函数原型:

struct dirent *readdir(DIR *dirp);

函数参数;

dirp -- 目录指针描述符

函数返回值:

成功 struct dirent* -- 目录信息结构体 失败:NULL

存储目录中的文件信息(文件名 , 拓展名等等)

#include <dirent.h>

struct dirent

{

long d_ino; /* inode number 索引节点号 */

off_t d_off; /* offset to this dirent 在目录文件中的偏移 */

unsigned short d_reclen; /* length of this d_name 文件名长 */

unsigned char d_type; /* the type of d_name 文件类型 */

char d_name [NAME_MAX+1]; /* file name

(null-terminated) 文件名,最长 255 字符 */

}

其实这个结构体还有其他的一些信息但在日常使用时这几个信息就够用了

closedir

函数功能;

关闭目录

函数头文件:

#include <sys/types.h>

#include <dirent.h>

函数原型:

int closedir(DIR *dirp);

函数参数;

drip -- 目录指针描述符

函数返回值:

0-- 成功   -1 -- 失败

文件属性

文件属性结构体

struct stat {

dev_t  st_dev; /*如果是设备,返回文件使用的设备号,否则为 0*/

ino_t  st_ino; /* 索引节点号 */

mode_t  st_mode; /* 文件类型和文件权限 */

nlink_t  st_nlink; /* 文件的硬连接数 */

uid_t  st_uid; /* 所有者用户识别号*/

gid_t  st_gid; /* 组识别号 */

dev_t  st_rdev; /* 设备类型*/

off_t  st_size; /* 文件大小,字节表示 */

blksize_t  st_blksize; /* 系统每次按块 Io 操作时块的大小(一般 是 512 或 1024)*/

blkcnt_t  st_blocks; /*块的索引号 */

time_t  st_atime; /* 最后访问时间,如 read*/

time_t  st_mtime; /* 最后修改时间*/

time_t  st_ctime; /* 创建时间 */

};

获取文件属性

函数名称:

stat -- 获取文件属性 ,但不包括链接文件的属性

lstat -- 获取文件属性 , 可以获取链接文件的信息

fstat -- 获取文件属性, 必须先打开文件 ,在获取文件信息

函数功能;

获取文件属性

函数头文件:

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

函数原型:

int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

函数参数;

pathname --- 文件名或目录

statbuf -- 文件属性结构体指针 -- 保存文件属性

函数返回值:

0-- 成功   -1 -- 失败

获取文件类型


char Get_File_Type1(mode_t mode)
{
        if(S_ISLNK(mode))
        {
                return 'l';
        }
        if(S_ISDIR(mode))
        {
                return 'd';
        }
        if(S_ISREG(mode))
        {
                return '-';
        }
        if(S_ISCHR(mode))
        {
                return 'c';
        }
        if(S_ISBLK(mode))
        {
                return 'b';
        }
        if(S_ISFIFO(mode))
        {
                return 'p';
        }
        if(S_ISSOCK(mode))
        {
                return 's';
        }
}
使用类型掩码获取文件类型

char File_type(mode_t st_mode)
{
	switch(st_mode&S_IFMT)
	{
		case S_IFSOCK:
			return 's';
		case S_IFLNK:
			return 'l';
		case S_IFREG :
			return '-';
		case S_IFBLK :
			return 'b';
		case S_IFDIR:
			return 'd';
		case S_IFCHR:
			return 'c';
		case S_IFIFO:
			return 'p';
	}
}

获取文件类型

char *File_Mode(mode_t st_mode)
{
	static char ch[9]="---------";
	if(st_mode&S_IRUSR)
	{
		ch[0]='r';
	}
	else
	{
		ch[0]='-';
	}
	if(st_mode&S_IWUSR)
	{
		ch[1]='w';
	}
	else
	{
		ch[1]='-';
	}
	if(st_mode&S_IXUSR)
	{
		ch[2]='x';
	}
	else
	{
		ch[2]='-';
	}
	if(st_mode&S_IRGRP)
	{
		ch[3]='r';
	}
	else
	{
		ch[3]='-';
	}
	if(st_mode&S_IWGRP)
	{
		ch[4]='w';
	}
	else
	{
		ch[4]='-';
	}
	if(st_mode&S_IXGRP)
	{
		ch[5]='x';
	}
	else
	{
		ch[5]='-';
	}
	if(st_mode&S_IROTH)
	{
		ch[6]='r';
	}
	else
	{
		ch[6]='-';
	}
	if(st_mode&S_IWOTH)
	{
		ch[7]='w';
	}
	else
	{
		ch[7]='-';
	}
	if(st_mode&S_IXOTH)
	{
		ch[8]='x';
	}
	else
	{
		ch[8]='-';
	}
	return ch;
}

结合目录操作和文件属性实现l -s的功能


#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
void Dir_Open();
void Dir_Print(char *name);
char File_type(mode_t st_mode);
char *File_Mode(mode_t st_mode);
int main(int argc,char *argv[])
{
	Dir_Open();
	return 0;
}
//打开目录
void Dir_Open()
{
	DIR *dir = opendir("./");
	struct dirent *p = NULL;
	while((p=readdir(dir))!=NULL)
	{
		//打印文件信息
		 Dir_Print(p->d_name);
	}
			
}
void Dir_Print(char *name)
{
	struct stat buf;//文件属性结构体
	lstat(name,&buf);
	//文件类型
	char Type = File_type(buf.st_mode);
	//文件权限
	char mode[10]={0};
	strcpy(mode,File_Mode(buf.st_mode));
	//文件大小
	int size = (int)buf.st_size;
	//修改时间
	char Time[1024]={0};
	struct tm *t=localtime(&buf.st_mtime);
	sprintf(Time,"%d月 %d %d:%d",t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min);
	//文件名--name
	printf("%c%s %d %s %s\n",Type,mode,size,Time,name);
}
char File_type(mode_t st_mode)
{
	switch(st_mode&S_IFMT)
	{
		case S_IFSOCK:
			return 's';
		case S_IFLNK:
			return 'l';
		case S_IFREG :
			return '-';
		case S_IFBLK :
			return 'b';
		case S_IFDIR:
			return 'd';
		case S_IFCHR:
			return 'c';
		case S_IFIFO:
			return 'p';
	}
}
char *File_Mode(mode_t st_mode)
{
	static char ch[9]="---------";
	if(st_mode&S_IRUSR)
	{
		ch[0]='r';
	}
	else
	{
		ch[0]='-';
	}
	if(st_mode&S_IWUSR)
	{
		ch[1]='w';
	}
	else
	{
		ch[1]='-';
	}
	if(st_mode&S_IXUSR)
	{
		ch[2]='x';
	}
	else
	{
		ch[2]='-';
	}
	if(st_mode&S_IRGRP)
	{
		ch[3]='r';
	}
	else
	{
		ch[3]='-';
	}
	if(st_mode&S_IWGRP)
	{
		ch[4]='w';
	}
	else
	{
		ch[4]='-';
	}
	if(st_mode&S_IXGRP)
	{
		ch[5]='x';
	}
	else
	{
		ch[5]='-';
	}
	if(st_mode&S_IROTH)
	{
		ch[6]='r';
	}
	else
	{
		ch[6]='-';
	}
	if(st_mode&S_IWOTH)
	{
		ch[7]='w';
	}
	else
	{
		ch[7]='-';
	}
	if(st_mode&S_IXOTH)
	{
		ch[8]='x';
	}
	else
	{
		ch[8]='-';
	}
	return ch;
}

本文标签: 系统函数原型编程