linux下实现my_ls

1. 功能

  1. 参数-a,-l,-R自由组合
  2. 在任意目录下使用
  3. 基本字体颜色显示
  4. 屏蔽ctrl+c
  5. 输出对齐

2.有关函数

1.信号处理

#include <signal.h>
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);

signum:信号编号
SIG_INT(ctrl+c),SIGQUIT(ctrl+/)
handler:函数指针,也可以是常数SIG_IGN(忽略),SIG_DFL(默认)


2.获取文件属性

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

struct stat {
   
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* Inode number */
               mode_t    st_mode;        /* File type and mode */
               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;     /* Block size for filesystem 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 */
}

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

S_ISLNK(buf.st_mode) 
S_ISREG(buf.st_mode)
S_ISDIR(buf.st_mode)
S_ISCHR(buf.st_mode)
S_ISBLK(buf.st_mode)
S_ISFIFO(buf.st_mode)
S_ISSOCK(buf.st_mode)

对于符号链接文件,stat返回符号链接文件本身信息,而lstat返回其所指向的文件信息。
POSIX标准定义了一系列宏来判断文件类型。

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

struct passwd {
   
               char   *pw_name;       /* username */
               char   *pw_passwd;     /* user password */
               uid_t   pw_uid;        /* user ID */
               gid_t   pw_gid;        /* group ID */
               char   *pw_gecos;      /* user information */
               char   *pw_dir;        /* home directory */
               char   *pw_shell;      /* shell program */
           };

struct passwd *getpwuid(uid_t uid);

得到用户名

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

struct group {
   
               char   *gr_name;        /* group name */
               char   *gr_passwd;      /* group password */
               gid_t   gr_gid;         /* group ID */
               char  **gr_mem;         /* NULL-terminated array of pointers
                                          to names of group members */
           };
struct group *getgrgid(gid_t gid);

得到用户组名


3.获取目录信息

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

struct dirent {
   
               ino_t          d_ino;       /* Inode number */
               off_t          d_off;       
               unsigned short d_reclen;    /* Length of this record */
               unsigned char  d_type;      
               char           d_name[256]; /* Null-terminated filename */
           };

DIR *opendir(const char *name);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dir);

readdir每执行一次返回一个目录下的信息,没有信息则返回NULL


4.字体颜色
\033[字体色;背景色m…\033[0m

printf("\033[%dm...\033[0m", color, ...);
字体色:30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋红)、36(青色)、37(白色)
背景色:40(黑色)、41(红色)、42(绿色)、 43(黄色)、44(蓝色)、45(洋红)、46(青色)、47(白色)

3. 代码分析

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/limits.h>
#include <dirent.h>
#include <grp.h>
#include <pwd.h>
#include <errno.h>
#include <signal.h>

#define _NONE 0 	//无参数下默认
#define _A 1 	
#define _L 2 	
#define MAX 1000 	//文件名大小
#define MAXROWLEN 80

int g_leave_len = MAXROWLEN;
int g_maxlen;
int _R = 0;

void my_err(const char* err_string, int line);  
void display(int flag_param, char* filename); 	
void display_attribute(struct stat buf, char * name, char* filename);	
void display_dir(int flag_param, char* path);	
void display_1(char* name, char* filename);	
void display_r(int flag_praram, char* filename); 
int get_color(char* 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值