Home > OS >  Implement a simple simulation function of ls
Implement a simple simulation function of ls

Time:12-13

1. The function of the ls -l
List all subdirectories and files in the directory of the main attributes and access data such as
2. After the ls -l under Linux, display a line,
should know their meaningsRw - r - r - 1 root root on January 8, 70 15:56
Meanings respectively: the type of file and access, file link, the file owner, the owner's group, file size, creation time
3. Be sure to find out the inside of the structure of the stat content, and ctime structures such as

The source code is as follows:
#include
Int main (int arg c, char * * argv)
{
The FILE * pSourse, * pDes;
Char ch;
PSourse=fopen (argv [1], the "r");
PDes=fopen (argv [2], "w");
If ((pSourse==NULL) | | (pDes==NULL))
{
Puts (" open file is error ");
}
While ((ch=fgetc (pSourse))!=(EOF)
{
Fputc (ch, pDes);
}
The fclose (pDes);
The fclose (pSourse);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include

Void the print (char * filename)
{

Struct stat st.
Int ret=stat (filename, & amp; St);
If (ret==1)
{
Perror (" stat ");
exit(1);
}

//store the file type and access
Char perms [11]={0};
//file type, judged by mask technology determine file type
The switch (st. st_mode & amp; S_IFMT)//S_IFMT file type mask
{
Case S_IFLNK://link files
Perms [0]='l';
break;
Case S_IFDIR://directory file
Perms [0]='d';
break;
Case S_IFREG://normal file
Perms [0]='-';
break;
Case S_IFBLK://file
Perms [0]='b';
break;
Case S_IFCHR://character file
Perms [0]='c';
break;
Case S_IFSOCK://socket file
Perms [0]='s'.
break;
Case S_IFIFO://piping file
Perms [0]='p';
break;
Default:
Perms [0]='? ';
break;
}
//files to access
//file owner
Perms [1]=(st. st_mode & amp; S_IRUSR)? 'r' : '-';
Perms [2]=(st. st_mode & amp; S_IWUSR)? 'w' : '-';
Perms [3]=(st. st_mode & amp; S_IXUSR)? 'x' : '-';
//file belongs to group of
Perms [4]=(st. st_mode & amp; S_IRGRP)? 'r' : '-';
Perms [5]=(st. st_mode & amp; S_IWGRP)? 'w' : '-';
Perms [6]=(st. st_mode & amp; S_IXGRP)? 'x' : '-';
//others
Perms [7]=(st. st_mode & amp; S_IROTH)? 'r' : '-';
Perms [8]=(st. st_mode & amp; S_IWOTH)? 'w' : '-';
Perms [9]=(st. st_mode & amp; S_IXOTH)? 'x' : '-';

//a hard link count
Int linkNum=st. st_nlink;
//file owner
Char * fileUser=getpwuid (st. st_uid) - & gt; Pw_name;
//file belongs to group of
Char * fileGrp=getgrgid (st. st_gid) - & gt; Gr_name;
//file size
Int fileSize=(int) st. st_size;
//modify time
Char * time=ctime (& amp; St. st_ctime);
Char * timec=ctime (& amp; St. st_mtime);
Char mtime [512]={0};
Char ctime [512]={0};
Strncpy (mtime, time, strlen (time) - 1);
Strncpy (ctime, timec, strlen (timec) - 1);
Char buf [1024].
Sprintf (buf, "% s % s % s % % 2 d - d t t % s \ % s \ " t, perms, linkNum, fileUser, fileGrp, fileSize, mtime, filename);

Printf (" % s \ n ", buf);

}
Void do_ls (char dirname [])
{
/*
* * define a directory flow, flow and directory structure preserved read as a result,
* */
DIR * dir_ptr;
Struct dirent * direntp;
If ((dir_ptr=opendir (dirname))==NULL)
Fprintf (stderr, "ls1: always open a % s \ n", dirname);
The else
{
While ((direntp=readdir (dir_ptr))!=NULL)
//print the results
If (direntp - & gt; D_name==". ")
Print (direntp - & gt; D_name '/');
The else
Print (direntp - & gt; D_name);
//printf (" % s \ n ", direntp - & gt; D_name);
////close the flow
Closedir (dir_ptr);
//
}

}
Int main (int arg c, char * argv [])
{
If (arg c==1)
Do_ls (". ");
The else
{
While (arg c)
{
: printf (" % s \ n ", * + + argv);
Printf (" ahidsfashdf \ n ");
Do_ls (* argv);
}
}
Print (argv [1]);
return 0;
}

  • Related