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

📄 demo_ls-l.c

📁 a demo of command of "ls -l"
💻 C
字号:
//a demo of command of "ls -l"
//this is my internet program homework, if any question please send E-mail to: jiangyutang@163.com

#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>

int selectdir(const struct dirent *dir){
//filter the current director and parent director
	if (bcmp(dir->d_name,".",1)==0) return 0;
	if (bcmp(dir->d_name,"..",2)==0) return 0;
	return 1;
}
int main(int argc,char *argv[]){
struct dirent **g_dirlist;
struct stat statbuf;
struct tm *tm;
struct passwd *pwd;
struct group *group;
int i,total;
static char *dateform="%D---%R";
char pri[11],timeformbuf[30];
//no para means list current director's context; two para means list the seconed director's context
if (argc==1){
	total=scandir(".",&g_dirlist,selectdir,alphasort);}
else {total=scandir(argv[1],&g_dirlist,selectdir,alphasort);}
if (total<0){
	printf("error: scandir");
	return 1;}
pri[10]='\0';
for (i=0;i<total;i++){
	stat(g_dirlist[i]->d_name,&statbuf);//get stat of director or file
	pri[0]='-';
	pri[1]='-';
	pri[2]='-';
	pri[3]='-';
	pri[4]='-';
	pri[5]='-';
	pri[6]='-';
	pri[7]='-';
	pri[8]='-';
	pri[9]='-';
	//kind of file or dir
	if ((statbuf.st_mode & S_IFMT)==040000) pri[0]='s';
	if ((statbuf.st_mode & S_IFMT)==0120000) pri[0]='l';
	if ((statbuf.st_mode & S_IFMT)==0100000) pri[0]='-';
	if ((statbuf.st_mode & S_IFMT)==0060000) pri[0]='b';
	if ((statbuf.st_mode & S_IFMT)==0040000) pri[0]='d';
	if ((statbuf.st_mode & S_IFMT)==0020000) pri[0]='c';
	if ((statbuf.st_mode & S_IFMT)==0010000) pri[0]='p';
	//private of owner
	if (statbuf.st_mode & S_IRUSR) pri[1]='r';
	if (statbuf.st_mode & S_IWUSR) pri[2]='w';
	if (statbuf.st_mode & S_IXUSR) pri[3]='x';
	//private of group
	if (statbuf.st_mode & S_IRGRP) pri[4]='r';
	if (statbuf.st_mode & S_IWGRP) pri[5]='r';
	if (statbuf.st_mode & S_IXGRP) pri[6]='x';
	//private of other
	if (statbuf.st_mode & S_IROTH) pri[7]='r';
	if (statbuf.st_mode & S_IWOTH) pri[8]='w';
	if (statbuf.st_mode & S_IXOTH) pri[9]='x';
	//get owner and group
	pwd=getpwuid(statbuf.st_uid);
	group=getgrgid(statbuf.st_gid);
	//get time
	tm=localtime(&(statbuf.st_mtime));
	strftime(timeformbuf,29,nl_langinfo(D_T_FMT),tm);
	printf("%s\t%d\t%s\t%s\t%d\t%s\t%s\n", pri, (int)statbuf.st_nlink, pwd->pw_name, group->gr_name, statbuf.st_size, timeformbuf, g_dirlist[i]->d_name);
	}
return 0;
}

⌨️ 快捷键说明

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