刚好把 Linux/UNIX 编程中的文件和IO部分学完了,就想编写个 ls 命令练习一下,本以为很简单,调用个 stat 就完事了,没想到前前后后弄了七八个小时,90%的时间都用在格式化(像 ls -l 中的对齐)输出了,反反复复改了好几遍。

一共实现了常用的四个选项:-a -h -l -d。可以从命令行参数中同时接受多个目录和文件,然后分开输出。

演示:

-a 命令:

-l 和 -h 命令:

-d 命令:

参数同时接受多个文件和目录名:

思路:

先使用 getopt 解析选项

然后判断参数有没有目录或文件(通过 operand 标志变量),没有的话仅输出进程当前目录

有的话将参数中的文件和目录分开(files 和 dirs数组),然后输出信息

因为要格式化输出,所以得将目录中所有项目读取完成后并转换成字符串形式(get_stat_str)才能获得长度最长的某个信息(如,文件名,尺寸),而目录中的文件数目又事先不可知,所以用链表结构(stat_str)将所有目录中所有文件的信息存储下来。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <limits.h>
#include <pwd.h>
#include <grp.h>
#define err_exit(func) {perror(func); exit(EXIT_FAILURE);} //打印出错函数,并结束程序
#define max(a, b) ((a) > (b) ? (a) : (b)) typedef struct stat_str{ //字符串形式的stat信息
char fname[NAME_MAX]; //大概长度,不严谨
char user[20];
char group[20];
char size[16];
char time[13];
char mode[11];
char nlink[5];
struct stat_str *next; //链表结构,因为需要预先获得目录里的所有项(为了控制格式化),但数目不确定,所以用链表保存
} st_str; int nlink_maxlen = 0, user_maxlen = 0, group_maxlen = 0, size_maxlen = 0; //字符串形式的最大长度,为了格式化构造字符串
int ARG_L = 0, ARG_A = 0, ARG_H = 0, ARG_D = 0; //参数是否定义 //解析选项
void analyse_opt(int argc, char* argv[]);
//将文件大小(字节数)转换成易读(human readable)的形式
void human_readable(off_t bytes, char *szbuf);
//构造文件的详细信息
void build_line(const st_str *stat_strbuf, char* fmtstrbuf);
//打印目录信息
void print_dir_info(const char *dir);
//获得文件信息的str形式
void get_stat_str(const struct stat* stbuf, const char* fname, st_str *strbuf); int main(int argc, char* argv[]) {
struct stat stbuf;
st_str st_strbuf;
int no_operand = 1; //是否没有操作数
analyse_opt(argc, argv); //解析选项 //分别获得目录和其他文件,为了格式化输出
char *files[argc-1], *dirs[argc-1];
int nf = 0, nd = 0;
for(int i = 1; i < argc; i++){
if(argv[i][0] == '-') //跳过选项
continue;
else
no_operand = 0;
if(-1 == lstat(argv[i], &stbuf))
err_exit("lstat");
if(S_ISDIR(stbuf.st_mode))
dirs[nd++] = argv[i];
else
files[nf++] = argv[i];
} if(no_operand){
//命令行没有输入路径
print_dir_info(".");
} else {
//先列出文件的信息
for(int i = 0; i < nf; i++){
char fmtstrbuf[256];
if(-1 == lstat(files[i], &stbuf))
err_exit("lstat");
get_stat_str(&stbuf, files[i], &st_strbuf);
if(ARG_L){
build_line(&st_strbuf, fmtstrbuf);
puts(fmtstrbuf);
} else {
puts(files[i]);
}
}
//再列出目录的信息
for(int i = 0; i < nd; i++){
if(nf > 0)
printf("\n%s:\n", dirs[i]);
print_dir_info(dirs[i]);
}
}
return 0;
} void analyse_opt(int argc, char* argv[]){
int opt;
while((opt = getopt(argc, argv, "lahd")) != -1){
switch (opt) {
case 'l':
ARG_L = 1;
break;
case 'a':
ARG_A = 1;
break;
case 'h':
ARG_H = 1;
break;
case 'd':
ARG_D = 1;
break;
}
}
} void human_readable(off_t nbytes, char *szbuf){
if(nbytes < 1024)
sprintf(szbuf, "%ld\0", nbytes);
else if(nbytes < 1024 * 1024)
sprintf(szbuf, "%.1lfK\0", (double)nbytes / 1024);
else if(nbytes < 1024 * 1024 * 1024)
sprintf(szbuf, "%.1lfM\0", (double)nbytes / 1024 / 1024);
else
sprintf(szbuf, "%.1lfG\0", (double)nbytes / 1024 / 1024 / 1024);
} void get_stat_str(const struct stat* stbuf, const char* fname, st_str *strbuf){
//mode
sprintf(strbuf->mode, "%c%c%c%c%c%c%c%c%c%c",
S_ISREG(stbuf->st_mode) ? '-' : (
S_ISDIR(stbuf->st_mode) ? 'd' : (
S_ISBLK(stbuf->st_mode) ? 'b' : (
S_ISCHR(stbuf->st_mode) ? 'c' : 'l'
)
)
),
(S_IRUSR & stbuf->st_mode ) ? 'r' : '-',
(S_IWUSR & stbuf->st_mode ) ? 'w' : '-',
(S_IXUSR & stbuf->st_mode ) ? 'x' : '-',
(S_IRGRP & stbuf->st_mode ) ? 'r' : '-',
(S_IWGRP & stbuf->st_mode ) ? 'w' : '-',
(S_IXGRP & stbuf->st_mode ) ? 'x' : '-',
(S_IROTH & stbuf->st_mode ) ? 'r' : '-',
(S_IWOTH & stbuf->st_mode ) ? 'w' : '-',
(S_IXOTH & stbuf->st_mode ) ? 'x' : '-' );
//nlink
sprintf(strbuf->nlink, "%ld\0", stbuf->st_nlink);
nlink_maxlen = max(nlink_maxlen, strlen(strbuf->nlink));
//user, group
sprintf(strbuf->user, "%s\0", getpwuid(stbuf->st_uid)->pw_name);
sprintf(strbuf->group, "%s\0", getgrgid(stbuf->st_gid)->gr_name);
user_maxlen = max(user_maxlen, strlen(strbuf->user));
group_maxlen = max(group_maxlen, strlen(strbuf->group));
//size
if(ARG_H){
char szbuf[16];
human_readable(stbuf->st_size, szbuf);
sprintf(strbuf->size, "%s\0", szbuf);
} else {
sprintf(strbuf->size, "%ld\0", stbuf->st_size);
}
size_maxlen = max(size_maxlen, strlen(strbuf->size));
//time
strftime(strbuf->time, 13, "%b %d %H:%M\0", localtime(&(stbuf->st_mtime)));
//fname
sprintf(strbuf->fname, "%s\0", fname); } void build_line(const st_str *stat_strbuf, char* fmtstrbuf){
char fmt[32];
sprintf(fmt, "%%s %%%ds %%-%ds %%-%ds %%%ds %%s %%s\0", nlink_maxlen, user_maxlen, group_maxlen, size_maxlen);
// puts(fmt);
if((stat_strbuf->mode)[0] == 'd')
strcat(stat_strbuf->fname, "/");
sprintf(fmtstrbuf, fmt, stat_strbuf->mode, stat_strbuf->nlink, stat_strbuf->user,
stat_strbuf->group, stat_strbuf->size, stat_strbuf->time, stat_strbuf->fname);
} void print_dir_info(const char *dir){
if(ARG_D){
//显式目录本身的信息
st_str stat_strbuf;
struct stat stbuf;
char fmtstrbuf[256];
if(-1 == lstat(dir, &stbuf))
err_exit("lstat");
get_stat_str(&stbuf, dir, &stat_strbuf);
if(ARG_L){
build_line(&stat_strbuf, fmtstrbuf);
puts(fmtstrbuf);
} else {
puts(stat_strbuf.fname);
}
} else {
group_maxlen = nlink_maxlen = user_maxlen = size_maxlen = 0;
//列出目录所有项
struct DIR *pdir = opendir(dir);
if(pdir == NULL)
err_exit("opendir");
struct dirent *pdirent;
struct stat stbuf;
st_str * head_st_str = (st_str*)(malloc(sizeof(st_str))), *p = head_st_str; //链表头(字符串形式的stat)
//循环都目录
errno = 0;
while((pdirent = readdir(pdir)) != NULL){
if((pdirent->d_name)[0] != '.' || ARG_A) {
//是否显示隐藏文件
if(ARG_L) {
char path[256];
strcpy(path, dir); //!!!!! 找了一个多小时才找出来这个错误
strcat(path, "/"); //!!!!! d_name仅是个文件名而已
strcat(path, pdirent->d_name); //!!!!! 需要加上完整路径
if(-1 == lstat(path, &stbuf)){
err_exit("lstat");
}
p->next = (st_str*)(malloc(sizeof(st_str)));
p = p->next;
p->next = NULL;
get_stat_str(&stbuf, pdirent->d_name, p);
} else {
puts(pdirent->d_name);
}
}
}
if(errno != 0)
err_exit("readdir"); //输出信息链表的格式化内容
p = head_st_str->next;
while(ARG_L && p){
char fmtstrbuf[256];
build_line(p, fmtstrbuf);
puts(fmtstrbuf);
p = p->next;
}
if(-1 == closedir(pdir))
err_exit("closedir");
st_str *q = head_st_str->next; //释放链表
while(q){
free(head_st_str);
head_st_str = q;
q = q->next;
}
free(head_st_str);
}
}

  

Linux/UNIX编程:使用C语言实现简单的 ls 命令的更多相关文章

  1. 学习linux/unix编程方法的建议(转)

    假设你是计算机科班出身,计算机系的基本课程如数据结构.操作系统.体系结构.编译原理.计算机网络你全修过 我想大概可以分为4个阶段,水平从低到高从安装使用=>linux常用命令=>linux ...

  2. 学习linux/unix编程方法的建议,学习Linux的四个步骤(转)

    解答:学习Linux的四个步骤假设你是计算机科班出身,计算机系的基本课程如数据结构.操作系统.体系结构.编译原理.计算机网络你全修过我想大概可以分为4个阶段,水平从低到高从安装使用=>linux ...

  3. Linux/UNIX编程如何保证文件落盘

    本文转载自Linux/UNIX编程如何保证文件落盘 导语 我们编写程序write数据到文件中时,其实数据不会立马写入磁盘,而是会经过层层缓存.每层缓存都有自己的刷新时机,每层缓存都刷新后才会写入磁盘. ...

  4. Linux用户应知应会的7个‘ls’命令的独特技巧

    在前面我们系列报道的两篇文章中,我们已经涵盖了关于‘ls’命令的绝大多数内容.本文时‘ls命令’系列的最后一部分.如果你还没有读过该系列的其它两篇文章,你可以访问下面的链接. Linux中的15个基本 ...

  5. Linux/UNIX编程:实现简单 tee 命令

    思路很简单,从标准输入文件描述符读入数据,然后同时向标准输出和参数指定的文件写出数据:如果加了 -a 选项,则以追加的方式向文件写出数据.还没了解 getopt() 函数就没判断参数是否合法. #in ...

  6. Linux/Unix编程中的线程安全问题【转】

    转自:http://blog.csdn.net/zhengzhoudaxue2/article/details/6432984 在目前的计算机科学中,线程是操作系统调度的最小单元,进程是资源分配的最小 ...

  7. linux socket 编程(C语言)

    转自:http://blog.csdn.net/piaojun_pj/article/details/5920888 最近看了一些网络编程的书籍,一直以来总感觉网络编程神秘莫测,其实网络编程入门还是很 ...

  8. linux socket 编程(C语言)[转]

    最近看了一些网络编程的书籍,一直以来总感觉网络编程神秘莫测,其实网络编程入门还是很容易学的,下面这些代码是我在linux下编写的,已经运行过了,编译之后就可以运行了.有不足之处希望大家多多指出,共同学 ...

  9. Linux网络编程:客户端/服务器的简单实现

    一. Socket的基本知识 1. socket功能 Socket层次 Socket实质上提供了进程通信的端点,进程通信之前,双方必须首先各自创建一个端点,否则是没有办法建立联系并相互通信的. 每一个 ...

随机推荐

  1. 记一次 qW3xT.4,解决挖矿病毒。

    最近感觉我的服务器特别卡,打开数据库都半天,刚开始以为网咯不好也没太在意. 利用top命令: 这时候问题出来了,最高cpu占用100%,那我用啥??? 根据进程id 一看究竟,ps -ef|grep ...

  2. 创建服务消费者(Feign)

    概述 Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单.使用 Feign,只需要创建一个接口并注解.它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS ...

  3. Linux常用实用命令

    Linux是我们开发人员必不可少的系统,也是经常接触到的.然而,Linux命令比较多,有些不常用也难记住.那么,我们如何更高效的使用Linux命令,而又不必全面地学习呢?今天就给大家分享一下我在开发过 ...

  4. python学习之路--python基础之列表操作

    本文主要介绍下python列表的的一些基本操作 列表是我们在python编程中常用的数据类型之一,通过列表我们可以对数据实现最方便的存储.修改等操作. 定义列表 names=['ZhangSan',' ...

  5. python trojan development 2nd —— use python to send mail and listen to the key board then combine them

    请勿用于非法用途!!!!!本人概不负责!!!原创作品,转载说明出处!!!!! from pynput.keyboard import Key,Listener import logging impor ...

  6. MySQL的登录与退出以及MySQL的目录结构

    一.MySQL的登录 1.利用语句mysql -uroot -proot 同时如果密码不想让别人看到,可以在-p处直接回车,再输入密码就是加密的了 2.远程登录 以连接本地为例 此处涉及到localh ...

  7. 系统学习 Java IO (十六)----这么多类,应该用哪个?

    目录:系统学习 Java IO---- 目录,概览 Java IO目的和功能 Java IO 包含 InputStream,OutputStream,Reader 和 Writer 类的许多子类. 原 ...

  8. SqlServer导入大文件Sql

    sqlcmd -S "192.168.1.218" -U "sa" -P "1qaz~xsw2" -d "SispMain&quo ...

  9. Selenium Grid分布式测试环境搭建

    Selenium Grid简介 Selenium Grid实际上是基于Selenium RC的,而所谓的分布式结构就是由一个hub节点和若干个node代理节点组成.Hub用来管理各个代理节点的注册信息 ...

  10. python数据分析与挖掘实战

    <python数据分析与挖掘实战>PDF&源代码&张良均 下载:链接:https://pan.baidu.com/s/1TYb3WZOU0R5VbSbH6JfQXw提取码: ...