Linux系统编程学习笔记(2)-文件状态查询与目录相关

什么是目录

在Linux系统下,目录是一个特殊的文件,他的内容是文件和目录的名字,每个目录下都有两个特殊的文件“.”与“..”,前者代表当前目录,后者代表上级目录。“ls”命令可以显示目录内容,根据后缀参数的不同,目录显示的内容也不同主要使用:

  • ls -l 显示详细信息,包括文件权限,最后修改时间,链接数等等。
  • ls -a 会显示以“.”开头的隐藏文件

下面使用C语音来模拟ls命令,包括读取文件夹,如何显示文件夹属性等等。

如何编写ls命令

直接上代码,根据代码解释

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>  //linux基本系统数据类型
#include <dirent.h>     //目录操作文件,定义目录结构体

void do_ls(char[]);
int main(int ac, char *av[])
{
  if(ac == 1)
    do_ls(".");
  else
    while(--ac){
        printf("%s:\n", *++av);
        do_ls(*av);
      }
}

void do_ls(char dirname[]){
  DIR* dir_ptr;
  struct dirent* direntp;
  if((dir_ptr = opendir(dirname)) == NULL){
    fprintf(stderr, "ls1:Connot open %s\n", dirname);
    perror("connot open files");
    }
  else
    {
      while((direntp = readdir(dir_ptr))!=NULL)
        printf("%s\n", direntp->d_name);
      closedir(dir_ptr);
    }
}

与文件的标准操作类似,对目录的读写操作有类似的函数

#include <sys/types.h>
#include <dirent.h>

头文件types.h包含了一些系统的基本数据类型,dirent.h包含了文件结构体与一些目录的基本操作接口:

opendir(char *) //打开目录,返回目录指针DIR*
readdir(DIR *)  //读取目录下的文件,返回文件描述结构体指针dirent*
closedir(DIR *) //关闭目录

运行此程序,基本的ls命令就可以执行了,下面编写ls -l命令来显示文件的详细信息

编写ls -l

首先应该知道,ls-l命令输出都包含了什么:

  • 模式(mode) 每行的第一个字符表示文件类型,“-”代表普通文件,“d”代表目录,可以根据此类型来判断文件的类型并进行具体的操作
  • 链接数(links) 链接数指的是文件被引用的次数,具体用处后续给出
  • 文件所有者(owner) 指出文件所有者的用户名
  • 组(group) 文件所有者所在的组
  • 大小(size) 文件的大小,值得注意的是,目录文件的大小是相同的,只有一般文件的大小不同
  • 最后修改时间(last-modified)
  • 文件名 (name) 显示文件名
获取文件描述函数

linux内的结构体stat表示了文件的属性,具体使用情况如下:

#include <sys/stat.h>
int result = stat(char *filename, struct stat* bufp)
return -1   //error
return 0    //success

结构体的成员如下:

st_mode     文件类型的许可权限
st_uid      用户所有者的ID
st_gid      所属组的ID
st_size     所占的字节数
st_nlink    文件链接数
st_mtime    文件最后修改时间
st_atime    文件最后访问时间
st_ctime    文件属性最后改变时间

此时直接调用stat函数,发现文件许可权限,用户名与组名的类型与ls命令并不相同,为数组显示下面将介绍如何转化。

将模式转化为字符串

在stat文件信息描述结构体中,st_mode为一个16为的二进制数,具体内容如下:
这里写图片描述
其中,前四个字节type表示文件类型,后三个为文件的特殊属性,其中

  • **suid位:**SUID位告诉内核,运行这个程序的时候认为是由文件的所有者在运行这个程序,在修改用户密码时起了很大的作用。
  • sgid位:与SUID位类似,运行程序的时候表示文件正在由组中的某一个用户运行
  • sticky位:防删除位, 一个文件是否可以被某用户删除, 主要取决于该文件所属的组是否对该用户具有写权限. 如果没有写权限, 则这个目录下的所有文件都不能被删除, 同时也不能添加新的文件. 如果希望用户能够添加文件但同时不能删除文件, 则可以对文件使用sticky bit位. 设置该位后, 就算用户对目录具有写权限, 也不能删除该文件.

chmod u+s temp:为temp文件设置suid位
chmod g+s temp:为temp文件设置guid位
chmod o+t temp:为temp文件设置sticky位

16位二进制数的后9位表示了不同用户的许可权限,分别为文件所有者,组用户,其他用户。“r”,“w”分别代表读写权限,user内的x放置suid位,group内的x放置guid位,other内的x放置sticky位。

Linux到底是如何将这样的二进制数解码成为具体权限的呢,这里使用了掩码的技术。将不需要的字段值0,需要的字段值不发生改变。使用按位与(&)操作即可完成。

  strcpy(str, "----------");
  if(S_ISDIR(mode)) str[0] = 'd';  //判断是否为mulu
  if(S_ISCHR(mode)) str[0] = 'c';
  if(S_ISBLK(mode)) str[0] = 'b';

  if(mode&S_IRUSR)  str[1] = 'r';
  if(mode&S_IWUSR)  str[2] = 'w';
  if(mode&S_IXUSR)  str[3] = 'x';
  //if(mode&S_ISUID) str[3] = 's';  判断SUID位

  if(mode&S_IRGRP)  str[4] = 'r';
  if(mode&S_IWGRP)  str[5] = 'w';
  if(mode&S_IXGRP)  str[6] = 'x';
  //if(mode&S_ISGID)  str[6] = 's';  判断GUID位

  if(mode&S_IROTH)  str[7] = 'r';
  if(mode&S_IWOTH)  str[8] = 'w';
  if(mode&S_IXOTH)  str[9] = 'x';
将用户/组转化为字符串

使用getpwuid函数即可根据一个用户ID得到用户的详细信息,函数返回一个指向struct passwd的指针,这些信息储存在passwd结构体中,结构体的内容如下:

struct passwd{
    char *pw_name;   //用户名
    char *pw_passwd; //用户密码
    _uid_t pw_uid;   //用户ID
    _gid_t pw_gid;   //组ID
    char *pw_gecos;  //真实姓名
    char *pw_dir;    //用户的home文件夹目录
    char *pw_shell;  //用户的shell程序
}
编写代码实现ls -l
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <ctime>
#include <time.h>
#include <stdlib.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>


void do_ls(char[]);
void dostat(char*);
void show_file_info(char*, struct stat *);
void mode_to_letters(int, char[]);
char* uid_to_name(uid_t);
char* gid_to_name(gid_t);

int main(int ac, char* av[]){
  if(ac == 1)
    do_ls(".");
  else
    while(--ac){
        printf("%s:\n", *++av);
        do_ls(*av);
      }
}

void do_ls(char dirname[]){
  DIR* dir_ptr;
  struct dirent* direntp;
  char fullname[1000];
  if((dir_ptr = opendir(dirname)) == NULL)
    fprintf(stderr, "ls1: cannot open %s\n", dirname);
  else{
      while((direntp = readdir(dir_ptr)) != NULL){
          if(strcmp(".", dirname) == 0)
            dostat(direntp->d_name);
          else{
            sprintf(fullname, "%s%s%s", dirname, "/", direntp->d_name);
            dostat(fullname);}
        }
      closedir(dir_ptr);
    }
}

void dostat(char* filename){
  struct stat info;
  if(stat(filename, &info) == -1)
    perror(filename);
  else
    show_file_info(filename, &info);
}

void show_file_info(char *filename, struct stat * info_p){
  //char* uid_to_name(), *ctime(), *gid_to_name(), *filemode();
  //void mode_to_letters();
  char modestr[11];
  mode_to_letters(info_p->st_mode, modestr);
  printf("%s", modestr);
  printf("%4d ", (int)info_p->st_nlink);
  printf("%-8s", uid_to_name(info_p->st_uid));
  printf("%-8s", gid_to_name(info_p->st_gid));
  printf("%8ld", (long)info_p->st_size);
  //struct timespec time1 = info_p->st_mtim;
  //struct _time_t st_time = time1;
  printf("%.12s", 4+ctime(&info_p->st_mtim.tv_sec));
  printf("%s\n", filename);
}

void mode_to_letters(int mode, char str[]){
  strcpy(str, "----------");
  if(S_ISDIR(mode)) str[0] = 'd';
  if(S_ISCHR(mode)) str[0] = 'c';
  if(S_ISBLK(mode)) str[0] = 'b';

  if(mode&S_IRUSR)  str[1] = 'r';
  if(mode&S_IWUSR)  str[2] = 'w';
  //if(mode&S_IXUSR)  str[3] = 'x';
  if(mode&S_ISUID) str[3] = 's';

  if(mode&S_IRGRP)  str[4] = 'r';
  if(mode&S_IWGRP)  str[5] = 'w';
  //if(mode&S_IXGRP)  str[6] = 'x';
  if(mode&S_ISGID)  str[6] = 's';

  if(mode&S_IROTH)  str[7] = 'r';
  if(mode&S_IWOTH)  str[8] = 'w';
  if(mode&S_IXOTH)  str[9] = 'x';
}

char* uid_to_name(uid_t uid){
  //struct passwd* getpwuid(), *pw_ptr;
  struct passwd* pw_ptr;
  static char numstr[10];

  if((pw_ptr = getpwuid(uid)) == NULL){
      sprintf(numstr, "%d", uid); //把整数打印到字符串中
      return numstr;
    }
  else
    return pw_ptr->pw_name;
}

char* gid_to_name(gid_t gid){
  struct group* grp_ptr;
  static char numstr[10];

  if((grp_ptr = getgrgid(gid)) == NULL){
      sprintf(numstr, "%d", gid);
      return numstr;
    }
  else
    return grp_ptr->gr_name;
}

修改文件所有者和组的系统函数

#include <unistd.h>
/*********************************
param path  文件名
param onwer 新的文件所有者ID
param group 新的组ID
return 0(success) -1(faile)
*********************************/
int chown(char *path, uid_t onwer, gid_t group)

修改文件最后访问时间和最后修改时间

#include <sys/time.h>
#include <utime.h>
#include <sys/types.h>
/********************************
param path 文件名
param newtimes 修改后的时间结构体
return 0(success) -1(faile)
********************************/
int utime(char *path, struct utimbuf *newtimes)

修改文件名函数

rename函数可以修改文件/目录的名字,还可以移动文件的位置。

#include <stdio.h>
int result = rename(char *old, char *new)

修改文件属性与权限

#include <sys/types.h>
#include <sys/stat.h>
int result = chmod(char *path, mode_t mode);

上述mode_t的类型与模式转换字符串时的文件类型相同。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值