⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myls_linux.c

📁 ls 命令可以说是linux下最常用的命令之一。它有众多的选项
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>

int main(int argc,char* argv[])
{
 
    DIR   *dp;
	struct dirent * dirp;//目录结构	
	struct stat stat_info;
	
	char pathname[100];//当前路径
    getcwd(pathname,100);

	char command[10];//输入命令

    printf(" Please Input ls or ls -l or quit,and don't use other blanks.\n\n");
    printf("%s :",pathname);//一点控制程序
	gets(command);
	while(strcmp(command,"quit")!=0)
	{
		if(strcmp(command,"ls")==0)//显示 ls
		{
				if(( dp = opendir(".")) != NULL)
				{
					while((dirp = readdir(dp))!= NULL)
					{
						 if(dirp->d_name[0]== '.' ) 
							 continue;/* 忽略 . 和 ..*/
						 printf("%s  ",dirp->d_name);
					}
                    printf("\n");
					if(closedir(dp)<0)//关闭
					{
						printf("closedir Error\n");
					}
				}
				else
				{
				  printf("opendir Error\n");
				}
			
		}//ls 命令结束
		else if(strcmp(command,"ls -l")==0 )//显示 ls -l
		{
			char uid_name[30];
			char gid_name[30];
			
             
              if(( dp = opendir(".")) != NULL)
				{
					while((dirp = readdir(dp))!= NULL)
					{
						 if(dirp->d_name[0]== '.' )
							 continue;/* 忽略 . 和 ..*/
						 lstat(dirp->d_name,&stat_info);//获得结构信息

			             char mode[10];
						 strcpy(mode,"----------");
						 if(S_ISDIR(stat_info.st_mode)) mode[0]= 'd';//解码得到许可权限
						 if(S_ISCHR(stat_info.st_mode)) mode[0]= 'c';
						 if(S_ISBLK(stat_info.st_mode)) mode[0]= 'b';
						 #ifdef S_ISLNK
                                else if(S_ISLNK(stat_info.st_mode)) mode[0]= 'l';
                         #endif
                         if(stat_info.st_mode & S_IRUSR) mode[1]= 'r';
						 if(stat_info.st_mode & S_IWUSR) mode[2]= 'w';
						 if(stat_info.st_mode & S_IXUSR) mode[3]= 'x';

						 if(stat_info.st_mode & S_IRGRP) mode[4]= 'r';
						 if(stat_info.st_mode & S_IWGRP) mode[5]= 'w';
						 if(stat_info.st_mode & S_IXGRP) mode[6]= 'x';

						 if(stat_info.st_mode & S_IROTH) mode[7]= 'r';
						 if(stat_info.st_mode & S_IWOTH) mode[8]= 'w';
						 if(stat_info.st_mode & S_IXOTH) mode[9]= 'x';

                         int i=0;
						 for(i=0;i<10;i++)
						 { 
							 printf("%c",mode[i]);//打印许可权限
						 }
						 

                         printf(" %4d ",stat_info.st_nlink);//打印链接数

						 strcpy(uid_name,getpwuid(stat_info.st_uid)->pw_name); //获得用户名
						 printf("%-8s ",uid_name);

                         strcpy(gid_name,getgrgid(stat_info.st_gid)->gr_name); //获得组名
                          printf("%-8s ",gid_name);

                         printf("%8ld ",stat_info.st_size);//打印字节数

                         struct tm *ptime;
						 char createtime[30];
						 ptime = gmtime(&stat_info.st_mtime);//格式化日期
		                 if(strftime(createtime, 20 , " %b %d %H:%M ", ptime));
						 printf(" %s\t",createtime);//获得最后修改时间

                         printf(" %s",dirp->d_name);//打印文件名
                        
                         printf("\n");
					}
                   
					if(closedir(dp)<0)//关闭
					{
						printf("closedir Error\n");
					}
				}
				else
				{
				  printf("opendir Error\n");
				}			
		}

		else{//错误输入
			printf("Input Error!, Please Input ls or ls -l or quit,and don't use other blanks.\n");
		}

        printf("%s :",pathname);//在while循环中的处理
		gets(command);
	}

   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -