LINUX ls2 further

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <ctime>
#include <error.h>
#include <ctype.h>
#define BLKSIZE 1024
void list(char *);
void do_ls(const char *);
void printout(const char * , char *);
void listsimple(char *);
void printoutsimple(const char * , char *);
char *get_perms(struct stat * , char *);
struct filename
{
   char *dirname;
   char *name;
}arrayf[100];;

#define n_array sizeof(array)/sizeof(const char *);

char * array[] = {};																					
int count;
static int compare (const void * a, const void * b)
{
    /* The pointers point to offsets into "array", so we need to
       dereference them to get at the strings. */
    return strcmp (*(const char **) a, *(const char **) b);
}
int stricmp (const char *p1, const char *p2)
{
  register unsigned char *s1 = (unsigned char *) p1;
  register unsigned char *s2 = (unsigned char *) p2;
  unsigned char c1, c2;

  do
  {
      c1 = (unsigned char) toupper((int)*s1++);
      c2 = (unsigned char) toupper((int)*s2++);
      if (c1 == '\0')
      {
	        return c1 - c2;
      }
  }
  while (c1 == c2);

  return c1 - c2;
}
static int compare2 (const void * a, const void * b)
{
    /* The pointers point to offsets into "array", so we need to
       dereference them to get at the strings. */
	struct filename *c = (filename *)a;
    struct filename *d = (filename *)b;
    if(stricmp (c->name,d->name) ==0)
	{
		return stricmp(c->dirname,d->dirname);
	} 
    else
    	return stricmp(c->name,d->name);
}



int main(int argc, char  *argv[])
{
	struct stat sbuf;
	/*handle arg*/	
	if(argc == 2 && strcmp(argv[1], "-l") == 0)
	{
		printout(".",*argv);
		exit(0);
	}  
	if(strcmp(argv[1], "-l") == 0) 
	{
		
		while(--argc)
		{	
			count=0;
			//list type of file
			if(stat(*++argv,&sbuf) < 0 )
			{
				continue;
			}
			printf("%s:\n", *argv );
			if((sbuf.st_mode &S_IFMT) == S_IFDIR)
					list(*argv);
			else 
				printout(".",*argv);
		}
	}
	else
	{
		if ( argc == 1 )
		do_ls( "." );
	    else
		  while ( --argc )
		{
			printf("%s:\n", *++argv );
			do_ls( *argv);
		}
	}
	exit(0);
}
/*read a dir, and list all file*/
void list(char * name)
{
	DIR * dp;
	struct  dirent * dir;
	//opendir 
	if((dp = opendir(name)) == NULL )
	{
		fprintf(stderr, "%s:cannot open.\n", name);
		return ;
	}
	/*process every file */
	while((dir = readdir(dp)) != NULL)
	{
		//printf("%s    %s\n",name,dir->d_name );
		//printout(name,dir->d_name);
		if(dir->d_name[0]!='.')
		{
		  filename temp;
		  char * tempname=name;
		  char * tempdirname=dir->d_name;
		  temp.dirname = tempname;
		  temp.name = tempdirname;
		  // printf("%s    %s\n",temp.name,temp.dirname );
		  arrayf[count++] = temp;
		 // printf("%s    %s\n", arrayf[count-1].name, arrayf[count-1].dirname );
	    }
	}
	
	qsort (arrayf, count, sizeof(arrayf[0]), compare2);
     for (int i = 0; i < count; i++) 
     {
     	 printout(arrayf[i].dirname,arrayf[i].name);
     }
     closedir(dp);
}
void printout(const char *dir, char *name)
{
	int i , j ;
	char perms[10];
	struct stat sbuf;
	char newname[BLKSIZE];
	//new path of the file
	sprintf(newname,"%s/%s",dir,name);
	stat(newname,&sbuf);
	// if(name[0]=='.')
	// 	return ;
	//the size of file
	printf("%5d",(int)(sbuf.st_size + BLKSIZE-1)/BLKSIZE);
    
    //the type
    switch( sbuf.st_mode & S_IFMT)
    {
    	case S_IFREG: putchar('-');break;
    	case S_IFDIR: putchar('d');break;
    	case S_IFCHR: putchar('c');break;
    	case S_IFBLK: putchar('b');break;
    	case S_IFIFO: putchar('l');break;
    #ifdef S_IFLNK
       putchar('l');break;
    #endif
	#ifdef S_IFSOCK
	   putchar('l');break;
    #endif
    #ifdef S_IFSOCK
       putchar('l');break;
    #endif
    default:
    putchar('?');break;
    }
    //get permission , in sbuf
    get_perms(&sbuf,perms);
    /*access , link , userid, groupid*/
    printf("%s%3d %5d/%-5d ", perms,(int)sbuf.st_nlink,(int)(sbuf.st_uid),(int)(sbuf.st_gid) );
    /*file size and mode-time*/
    printf("%7d %.20s ",(int)sbuf.st_size,ctime(&sbuf.st_mtime) );
    printf("%s\n",name);

}

void do_ls( const char *dirname )
/*
 *	list files in directory called dirname
 */
{
	DIR		*dir_ptr;		/* the directory */
	struct dirent	*direntp;		/* each entry	 */

    int count=0;
	if ( ( dir_ptr = opendir( dirname ) ) == NULL )
		fprintf(stderr,"ls1: cannot open %s\n", dirname);
	else
	{
		while ( ( direntp = readdir( dir_ptr ) ) != NULL )
		{
			//printf("%s\n", direntp->d_name );
			array[count++] = direntp->d_name;
			const char *a = direntp->d_name;
			 if(a[0]=='.')
			 	count--;
		}
		closedir(dir_ptr);
	}
	
    qsort (array, count, sizeof (const char *), compare);
    for (int i = 0; i < count; i++) 
    {
    	 printf ("%d: %s.\n", i, array[i]);
    }
}

char *get_perms(struct stat *sbuf,char * perms)
{
	static const char *access[]={
		"---","--x","-w-","-wx","r--","r-x","rw-","rwx"
	};
	int i,j;
	*perms ='\0';

	for (i = 2; i >=0; --i)
	{

		j=(sbuf->st_mode >>(i*3)) &07;
		strcat(perms, access[j]);
	}

	if((sbuf->st_mode & S_ISUID) !=0 )
		perms[2] ='s';
	if((sbuf->st_mode & S_ISGID) !=0 )
		perms[5] ='s';
	if((sbuf->st_mode & S_ISUID) !=0 )
		perms[8] ='t';
    return perms;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值