int get_file_type( char *filename ){
struct stat info;
stat( filename,&info );
int file_type = 0;
file_type = info.st_mode & S_IFMT;
return file_type;
}
/*get file type*/
int get_modify_time( char *filename ){
struct stat info;
stat( filename,&info );
int modify_time = 0;
modify_time = info.st_mtime;
return modify_time;
}
/*get file last modify time*/
int isadir(char *str)
{
struct stat info;
return ( lstat(str,&info) != -1 && S_ISDIR(info.st_mode) );
}
/*判断某文件是否为目录文件*/
void getWidth(){
char *tp;
struct winsize wbuf;
terminalWidth = 80;
if( isatty(STDOUT_FILENO) ){
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &wbuf) == -1 || wbuf.ws_col == 0){
if( tp = getenv("COLUMNS") )
terminalWidth = atoi( tp );
}
else
terminalWidth = wbuf.ws_col;
}
return ;
}
/******************************************************************
得出终端的宽度,默认宽度为80,如果获取失败,将激活-1选项
*******************************************************************/
int cmp1( const void *p ,const void *q ){
char T1[ LenOfName ],T2[ LenOfName ];
strcpy( T1,(*(struct outputFile *)p).FileName );
strcpy( T2,(*(struct outputFile *)q).FileName );
int len1 = strlen( T1 );
int i ;
for( i=0;i<len1;i++ ){
if( T1[ i ]>='A' && T1[ i ]<='Z' ){
T1[ i ] = T1[ i ] - 'A' + 'a';
}
}
int len2 = strlen( T2 );
for( i=0;i<len2;i++ ){
if( T2[ i ]>='A' && T2[ i ]<='Z' ){
T2[ i ] = T2[ i ] - 'A' + 'a';
}
}
return strcmp( T1,T2 );
}
/********************************************************
文件名排序 cmp1
*********************************************************/
int cmp2( const void *p,const void *q ){
return (*(struct outputFile *)p).modify_time < (*(struct outputFile *)q).modify_time ;
}
/********************************************************
文件修改时间排序 cmp2
*********************************************************/
void show_file_info( char *filename,struct stat * info_p ){
char modestr[ 12 ];
mode_to_letters( info_p->st_mode,modestr );
printf("%s",modestr);
printf("%3d ",(int)info_p->st_nlink);
printf("%-2s ",uid_to_name(info_p->st_uid));
printf("%-1s ",gid_to_name(info_p->st_gid));
printf("%7ld ",(long)info_p->st_size);
printf("%.12s ",4+ctime(&info_p->st_mtime));
printf("\033[%d;%d;%dm%s\033[0m\n",colormode,foreground,background,filename);
return ;
}
/*******************************************************
ls -l 的输出
********************************************************/
void getcolor( char *filename ){
struct stat info;
stat( filename,&info );
foreground = 30;
background = 1;
colormode = 0;
switch ( (info.st_mode & S_IFMT) ){
case S_IFREG: /*regular 普通文件 , 色*/
foreground = 30;
colormode = 1;
break;
case S_IFLNK: /*symbolic link 链接文件 , 青蓝色*/
foreground = 36;
colormode = 1;
break;
case S_IFSOCK: /*紫红色*/
foreground = 35;
colormode = 1;
break;
case S_IFDIR: /*directory 目录文件 , 蓝色*/
foreground = 34;
break;
case S_IFBLK: /*block special 块设备文件 , 黄色*/
foreground = 33;
colormode = 1;
break;
case S_IFCHR: /*character special 字符设备文件 , 黄色*/
foreground = 33;
colormode = 1;
break;
case S_IFIFO: /*fifo 绿色*/
foreground = 32;
colormode = 1;
break;
}
}
/*******************************************************
给文件添加颜色
********************************************************/