2013年7月21日日曜日
lsコマンドの真似事lss.c
lss.c
$ gcc -o lss lss.c
$ ./lss
$ ./lss .c (エルエスエス空白ポチシー)
$ ./lss dir
$ ./lss lss
などなど
#include <sys/syscall.h>
#include <unistd.h>
#ifdef __linux__
# include <linux/types.h>
/* # include <linux/dirent.h> */
# include <linux/unistd.h>
# include <errno.h>
#else
# include <dirent.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#define AMAX 256
struct dirent {
long d_ino;
off_t d_off;
unsigned short d_reclen;
char d_name[256];
};
struct node {
long fileno;
char filename[AMAX];
char atime[AMAX];
char rwxx[AMAX];
off_t asize;
struct node *back,*next;
};
char atime[AMAX];
char filename[AMAX];
char rwxx[AMAX];
off_t asize,sumsize1=0,sumsize2=0;
void timecpy(char a[AMAX],char b[AMAX]);
void rwx(char a[AMAX],long b);
long ic=0,sumcnt1=0,sumcnt2=0;
struct node *node_create();
struct node *shojun(struct node *temp);
struct node *sttc1,*stt;
struct node *startc1,*endc1;
struct node *info,*start,*last,*gcdata,*gc2;
struct stat buf2;
char *search(char *text,char *key);
int kensaku(char *ss);
long cnt=0;
char tt[100];
int j;
int ynflg;
int overflg=0;
int argc1;
char *argv1,p[AMAX];
int main(int argc,char *argv[]){
int i,im,d_size;
int fd;
char buf[4096],ch;
char ss[500],*p;
char yn[20];
argc1=argc;
argv1=argv[1];
strcpy(ss,".");
j=1;
kensaku(ss);
}
int kensaku(char *ss){
int i,im,d_size,k;
int fd;
char buf[4096],ss1[100][500];
char *p;
j++;
if((fd=open(ss,O_RDONLY))<0){
perror("open");
j--;
return 1;
}
while((d_size=syscall(SYS_getdents,fd,(struct dirent *)&buf,sizeof buf))>0){
for(i=0;i<d_size;i+=((struct dirent *)&buf[i])->d_reclen){
strcpy(ss1[j],ss);
strcat(ss1[j],"/");
strcat(ss1[j],((struct dirent *)&buf[i])->d_name);
if(stat(ss1[j],&buf2)<0){
perror("stat");
j--;
return 1;
}
if(ic==0){
startc1=sttc1=gcdata=gc2=node_create();
ic++;
strcpy(gcdata->filename,((struct dirent *)&buf[i])->d_name);
timecpy(gcdata->atime,ctime(&buf2.st_mtime));
rwx(gcdata->rwxx,buf2.st_mode);
gcdata->asize=buf2.st_size;
}
else{
gcdata->next=node_create();
gcdata=gcdata->next;
gcdata->back=gc2;
gc2=gc2->next;
strcpy(gcdata->filename,((struct dirent *)&buf[i])->d_name);
timecpy(gcdata->atime,ctime(&buf2.st_mtime));
rwx(gcdata->rwxx,buf2.st_mode);
gcdata->asize=buf2.st_size;
}
}
}
info=startc1;
last=NULL;
while(info){
startc1 =info->next;
info->next=NULL;
stt =shojun(info);
info =startc1;
}
startc1=stt;
endc1=last;
ic=1;
info=startc1;
while(info){
info->fileno=ic;
info=info->next;
ic++;
}
if(argc1==1){
info=startc1;
while(info){
sumcnt1++;
sumsize1+=info->asize;
printf("%5ld: %-10s %s %s %8ld\33[0m\n",info->fileno,info->filename,info->rwxx,info->atime,info->asize);
info=info->next;
}
printf("\nこのフォルダの総数は %ld でファイルの総和は %ld\n",sumcnt1,sumsize1);
}
if(argc1==2){
info=startc1;
while(info){
sumcnt1++;
sumsize1+=info->asize;
p=search(info->filename,argv1);
if(!strcmp(argv1,"dir")&&info->rwxx[0]=='d'){
sumcnt2++;
sumsize2+=info->asize;
printf("%5ld: %-10s %s %s %8ld\33[0m\n",info->fileno,info->filename,info->rwxx,info->atime,info->asize);
}
else if(!strcmp(argv1,"file")&&info->rwxx[1]=='f'){
sumcnt2++;
sumsize2+=info->asize;
printf("%5ld: %-10s %s %s %8ld\33[0m\n",info->fileno,info->filename,info->rwxx,info->atime,info->asize);
}
else if(p){
sumcnt2++;
sumsize2+=info->asize;
printf("%5ld: %-10s %s %s %8ld\33[0m\n",info->fileno,info->filename,info->rwxx,info->atime,info->asize);
}
info=info->next;
}
printf("\nカレントディレクトリの総数は %ld でファイルの総和は %ld\n",sumcnt1,sumsize1);
printf("検索ワードは %s\n検索ワードの総数は %ld でファイルの総和は %ld\n",argv1,sumcnt2,sumsize2);
}
if(d_size<0){
perror("getdents");
j--;
return 0;
}
}
char *search(char *text,char *key){
int m,n;
char *p;
m=strlen(text);
n=strlen(key);
for(p=text;p<=text+m-n;p++){
if(strncmp(p,key,n)==0) return p;
}
return NULL;
}
void timecpy(char a[AMAX],char b[AMAX]){
a[ 0]='\33';
a[ 1]='[';
a[ 2]='3';
a[ 3]='1'+b[23]%5;
a[ 4]='m';
a[ 5]=b[20];
a[ 6]=b[21];
a[ 7]=b[22];
a[ 8]=b[23];
a[ 9]='/';
a[10]='\33';
a[11]='[';
a[12]='3';
if(b[4]=='J'&&b[5]=='a') a[13]='1';
else if(b[4]=='F'&&b[5]=='e') a[13]='2';
else if(b[4]=='M'&&b[6]=='r') a[13]='3';
else if(b[4]=='A'&&b[5]=='p') a[13]='4';
else if(b[4]=='M'&&b[6]=='y') a[13]='5';
else if(b[4]=='J'&&b[5]=='u'&&b[6]=='n') a[13]='6';
else if(b[4]=='J'&&b[5]=='u'&&b[6]=='l') a[13]='1';
else if(b[4]=='A'&&b[5]=='u') a[13]='2';
else if(b[4]=='S'&&b[5]=='e') a[13]='3';
else if(b[4]=='O'&&b[5]=='c') a[13]='4';
else if(b[4]=='N'&&b[5]=='o') a[13]='5';
else if(b[4]=='D'&&b[5]=='e') a[13]='6';
else a[13]='1';
a[14]='m';
a[15]=b[4];
a[16]=b[5];
a[17]=b[6];
a[18]='/';
a[19]='\33';
a[20]='[';
a[21]='3';
a[22]='1'+b[9]%5;
a[23]='m';
if(b[8]==' ') a[24]='0';
else a[24]=b[8];
a[25]=b[9];
a[26]='\33';
a[27]='[';
a[28]='3';
if(b[0]=='S'&&b[1]=='u') a[29]='1';
else if(b[0]=='S'&&b[1]=='a') a[29]='2';
else if(b[0]=='M') a[29]='2';
else if(b[0]=='T'&&b[1]=='u') a[29]='3';
else if(b[0]=='T'&&b[1]=='h') a[29]='6';
else if(b[0]=='W') a[29]='4';
else if(b[0]=='F') a[29]='5';
else a[29]='1';
a[30]='m';
a[31]='(';
a[32]=b[0];
a[33]=b[1];
a[34]=b[2];
a[35]=')';
a[36]='\33';
a[37]='[';
a[38]='3';
a[39]='1'+b[12]%5;
a[40]='m';
a[41]=b[11];
a[42]=b[12];
a[43]=b[13];
a[44]='\33';
a[45]='[';
a[46]='3';
a[47]='1'+b[15]%5;
a[48]='m';
a[49]=b[14];
a[50]=b[15];
a[51]=b[16];
a[52]='\33';
a[53]='[';
a[54]='3';
a[55]='1'+b[18]%5;
a[56]='m';
a[57]=b[17];
a[58]=b[18];
a[59]='\0';
if(b[0]==' ') strcpy(a," \33[31mファイルがありません ");
}
struct node *node_create()
{
struct node *from;
int size;
size=sizeof(struct node);
from = (struct node*)malloc(size);
if(!from){
printf("out of memory ¥n");
exit(0);
}
from->next=NULL;
from->back=NULL;
from->fileno=0;
strcpy(from->filename,"");
strcpy(from->atime,"");
from->asize=0;
return from;
}
struct node *shojun(struct node *temp)
{
struct node *old;
struct node *now;
now=stt;
old=NULL;
if(!last){
temp->next=NULL;
temp->back=NULL;
last =temp;
return temp;
}
while(now){
if(strcmp(now->filename,temp->filename) < 0){
old=now;
now=now->next;
}
else{
if(old){
old ->next=temp;
now->back=temp;
temp->next=now;
temp->back=old;
return stt;
}
else{
temp->next=now;
now->back=temp;
temp->back=NULL;
return temp;
}
}
}
last->next=temp;
temp->back=last;
temp->next=NULL;
last =temp;
return stt;
}
void rwx(char a[AMAX],long b){
strcpy(a,"_________________");
if((b&0x8000)==0x8000) a[1]='f';
if((b&0x4000)==0x4000) a[0]='d';
/*
if((b&0x2000)==0x2000) a[2]='c';
if((b&0x1000)==0x1000) a[3]='d';
if((b&0x0800)==0x0800) a[4]='e';
if((b&0x0400)==0x0400) a[5]='f';
if((b&0x0200)==0x0200) a[6]='g';
*/
if((b&0x0100)==0x0100) a[3]='r';
if((b&0x0080)==0x0080) a[4]='w';
if((b&0x0040)==0x0040) a[5]='x';
if((b&0x0020)==0x0020) a[6]='r';
if((b&0x0010)==0x0010) a[7]='w';
if((b&0x0008)==0x0008) a[8]='x';
if((b&0x0004)==0x0004) a[9]='r';
if((b&0x0002)==0x0002) a[10]='w';
if((b&0x0001)==0x0001) a[11]='x';
a[12]=0;
}
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿