用C++实现Linux中shell的ls功能
实现输出当前目录下的文件名
ls功能:
方法一:
- #include <iostream>
- #include <algorithm>
- #include <stdio.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <string.h>
- #include <string>
- using namespace std;
- bool cmp( string s1,string s2){
- return s1<s2;
- }
- int main()
- {
- DIR *dir;
- char s[];
- string data[];
- int tot=;
- struct dirent *rent;
- dir =opendir(".");
- while(rent=readdir(dir))
- {
- strcpy(s,rent->d_name);
- if(s[]!='.'){
- data[tot]=s;
- tot++;
- }
- }
- sort(data,data+tot,cmp);
- for(int i=;i<tot;i++)
- cout<<data[i]<<" ";
- puts("");
- closedir(dir);
- return ;
- }
方法二:
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <pwd.h>
- #include <grp.h>
- #include <time.h>
- #include <dirent.h>
- int do_ls(char *dir,char *filename,int lflag)
- {
- int n;
- struct stat buf;
- char out[];
- struct passwd *pw;
- struct group *gr;
- struct tm *t;
- if(lflag == ) //如果不带l参数,直接显示文件/目录名
- {
- printf("%s\t",filename);
- return ;
- }
- if(lstat(dir,&buf)<)
- {
- fprintf(stderr,"stat error:%s\n",strerror(errno));
- return -;
- }
- switch(buf.st_mode & S_IFMT) //获取字符串的属性:普通文件-、目录d、字符设备c、块设备b、管道文件p、连接文件l、套接字文件s
- {
- case S_IFREG:
- printf("-");
- break;
- case S_IFDIR:
- printf("d");
- break;
- case S_IFCHR:
- printf("c");
- break;
- case S_IFBLK:
- printf("b");
- break;
- case S_IFIFO:
- printf("p");
- break;
- case S_IFLNK:
- printf("l");
- break;
- case S_IFSOCK:
- printf("s");
- break;
- }
- for(n=; n>=; n--) //打印文件的读写属性:读r、写w、执行x、无权限-
- {
- if(buf.st_mode&(<<n))
- {
- switch(n%)
- {
- case :
- printf("r");
- break;
- case :
- printf("w");
- break;
- case :
- printf("x");
- break;
- default:
- break;
- }
- }
- else
- {
- printf("-");
- }
- }
- printf(" %d",buf.st_nlink); //硬链接数,此链接非彼链接,指(包含)目录的个数,文件为1,目录起始为2,再加上目录里包含的目录个数(不递归,只一层)
- pw = getpwuid(buf.st_uid); //所属用户名
- printf(" %s",pw->pw_name);
- gr = getgrgid(buf.st_gid); //所属组名
- printf(" %s",gr->gr_name);
- printf(" %ld",buf.st_size); //字节计总大小
- t = localtime(&buf.st_atime); //最后一次访问时间
- printf(" %d-%d-%d %d:%d"
- ,t->tm_year+
- ,t->tm_mon+
- ,t->tm_mday
- ,t->tm_hour
- ,t->tm_min);
- printf(" %s ",filename);
- if(S_ISLNK(buf.st_mode)) //判断是否为链接,是返回真
- {
- printf(" -> ");
- if(readlink(filename,out,)==-)
- {
- //printf("readlink error\n");
- }
- printf("%s",out);
- }
- printf("\n");
- return ;
- }
- int ls_prepare(char *w,int aflag,int lflag) //ls的准备工作
- {
- struct stat buf; //man lstat可以看到此结构
- char name[];
- DIR *dir; //类似打开文件的fd描述符
- struct dirent *pdr; //man readdir可以看到此结构
- if(lstat(w,&buf)<) //获取文件/目录属性并赋值给buf,该函数和lstat一样,只是当w为链接时,指代他本身,并不存在文件
- {
- fprintf(stderr,"stat error:%s\n",strerror(errno));
- return -;
- }
- if(S_ISDIR(buf.st_mode)) //判断是否为目录,是返回真
- {
- dir = opendir(w); //打开目录
- while ((pdr = readdir(dir))!=NULL) //读/遍历目录
- {
- if(aflag==) //如果不带a参数,越过以.开头的所有文件/目录
- {
- if(pdr->d_name[]=='.')
- continue;
- memset(name,,);
- strcpy(name,w); //拷贝
- strcat(name,"/"); //追加
- strcat(name,pdr->d_name);
- do_ls(name,pdr->d_name,lflag);
- }
- else //有a参数显示所有
- {
- memset(name,,);
- strcpy(name,w);
- strcat(name,"/");
- strcat(name,pdr->d_name);
- do_ls(name,pdr->d_name,lflag);
- }
- }
- closedir(dir);
- }
- else //为文件则直接显示
- {
- do_ls(w,w,lflag);
- }
- return ;
- }
- int main(int argc,char **argv)
- {
- int aflag =;
- int lflag =;
- char c;
- int i;
- while((c = getopt(argc,argv,"al"))!=-) //解析命令行参数,即-/--后面的字符串和给定的字符串匹配,有未解析字母返回字母或问号(取决于第3个参数),否则返回-1
- {
- switch(c) //此处仅匹配a(所有)和l(列表),即只支持参数a、l
- {
- case 'a':
- aflag =;
- break;
- case 'l':
- lflag =;
- break;
- default:
- break;
- }
- }
- if(argc == optind )
- {
- ls_prepare("./",aflag,lflag);
- }
- else
- {
- for(i=optind; i<argc; i++) //所有目录都传进去
- ls_prepare(argv[i],aflag,lflag);
- }
- printf("\n");
- return ;
- }
ls -l功能:
- #include <stdio.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <pwd.h>
- #include <grp.h>
- 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);
- printf(" %.12s", 4 + ctime(&info_p->st_mtime));
- 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_IRGRP)) {
- str[4] = 'r';
- }
- if ((mode & S_IWGRP)) {
- str[5] = 'w';
- }
- if ((mode & S_IXGRP)) {
- str[6] = 'x';
- }
- 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;
- 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* getgrgid(),* grp_ptr;
- static char numstr[10];
- if(( grp_ptr = getgrgid(gid)) == NULL){
- sprintf(numstr,"%d",gid);
- return numstr;
- }else{
- return grp_ptr->gr_name;
- }
- }
- void do_ls(char dirname[]) {
- DIR* dir_ptr;
- struct dirent* direntp;
- if ((dir_ptr = opendir(dirname)) == NULL) {
- fprintf(stderr, "ls2: cannot open %s \n", dirname);
- } else {
- while ((direntp = readdir(dir_ptr)) != NULL) {
- dostat(direntp->d_name);
- }
- close(dir_ptr);
- }
- }
- void dostat(char* filename) {
- struct stat info;
- if (stat(filename, &info) == -1) {
- perror(filename);
- } else {
- show_file_info(filename, &info);
- }
- }
- int main(int ac,char* av[]){
- if(ac == 1){
- do_ls(".");
- }else{
- while(--ac){
- printf("%s: \n",*++av);
- do_ls(*av);
- }
- }
- }
用C++实现Linux中shell的ls功能的更多相关文章
- Linux中Shell
Linux中Shell Shell是什么 Shell是一个命令行解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,可以用Shell来启动.挂起.停止.编写一些程序. S ...
- linux中shell变量$#,$@,$0,$1,$2的含义解释
linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
- linux中shell变量$#,$@,$0,$1,$2的含义解释
linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID ...
- Linux中shell变量$0,$?等含义
linux中shell变量$#,$@,$0,$1,$2的基本含义: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
- 【转】linux中shell变量$#,$@,$0,$1,$2的含义解释
原文网址:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html linux中shell变量$#,$@,$0,$1,$2的含义解释: ...
- 【Shell】linux中shell变量$#,$@,$0,$1,$2的含义解释 && set 关键字使用
linux中shell变量$#,$@,$0,$1,$2的含义解释 摘抄自:ABS_GUIDE 下载地址:http://www.tldp.org/LDP/abs/abs-guide.pdf linu ...
- linux中shell变量$#,$@,$0,$1,$2的含义
linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
- linux中shell变量$#,$@,$0,$1,$2
linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
- 浅谈linux中shell变量$#,$@,$0,$1,$2,$?的含义解释
浅谈linux中shell变量$#,$@,$0,$1,$2,$?的含义解释 下面小编就为大家带来一篇浅谈linux中shell变量$#,$@,$0,$1,$2的含义解释.小编觉得挺不错的,现在就分享给 ...
随机推荐
- 一起学 Java(三) 集合框架、数据结构、泛型
一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...
- 再讲IQueryable<T>,揭开表达式树的神秘面纱
接上篇<先说IEnumerable,我们每天用的foreach你真的懂它吗?> 最近园子里定制自己的orm那是一个风生水起,感觉不整个自己的orm都不好意思继续混博客园了(开个玩笑).那么 ...
- 学习ASP.NET Core, 怎能不了解请求处理管道[4]: 应用的入口——Startup
一个ASP.NET Core应用被启动之后就具有了针对请求的处理能力,而这个能力是由管道赋予的,所以应用的启动同时意味着管道的成功构建.由于管道是由注册的服务器和若干中间件构成的,所以应用启动过程中一 ...
- MVC如何使用开源分页插件shenniu.pager.js
最近比较忙,前期忙公司手机端接口项目,各种开发+调试+发布现在几乎上线无问题了:虽然公司项目忙不过在期间抽空做了两件个人觉得有意义的事情,一者使用aspnetcore开发了个人线上项目(要说线上其实只 ...
- WebApi基于Token和签名的验证
最近一段时间在学习WebApi,涉及到验证部分的一些知识觉得自己并不是太懂,所以来博客园看了几篇博文,发现一篇讲的特别好的,读了几遍茅塞顿开(都闪开,我要装逼了),刚开始读有些地方不理解,所以想了很久 ...
- MediatorPattern(中介者模式)
/** * 中介者模式 * @author TMAC-J * 研究了这么多设计模式,觉得无非就是几点: * 1.若两个类有耦合关系,设立一个中间类,处理两个类的关系,把两个类的耦合降低 * 2.面向接 ...
- Atitit.如何建立研发体系
Atitit.如何建立研发体系 组织,流程,prj..Mana oppm 发管理是一个完整的管理体系,从结构上来讲,它主要由四个方面的内容构架而成:组织结构与岗位设置 管理流程与工作流程..项目及管 ...
- Web前端需要熟悉大学里【高大上】的计算机专业课吗?
作为一名刚刚大学毕业,进入新的学习阶段的研究生,我必须说大学的专业课非常重要!不管你信不信,事实就是如此! 一.大学学习的专业课非常重要,它决定了我们能走到什么高度 前端的发展非常快,我常常觉得刚刚关 ...
- 第14章 Linux启动管理(1)_系统运行级别
1. CentOS 6.x 启动管理 (1)系统运行级别 ①运行级别 运行级别 含义 0 关机 1 单用户模式,可以想象为Windows的安全模式,主要用于系统修复.(但不是Linux的安全模式) 2 ...
- JAVA通信系列二:mina入门总结
一.学习资料 Mina入门实例(一) http://www.cnblogs.com/juepei/p/3939119.html Mina入门教程(二)----Spring4 集成Mina http:/ ...