Using LINUX-C programming to implement simple LS commands

Source: Internet
Author: User

Using LINUX-C programming to implement simple LS commands
The specific code is as follows:
The loaded header file has stdio.h,sys/types.h,dirent.h,unistd.h,sys/stat.h,fcntl.h,string.h and so on.

Void main (INT ARGC,CHAR **ARGV) {     dir *dir;      struct dirent *rdir;     struct stat statbuf;      char * buffer= "/";      char *file_; char  *p;     int lenght=strlen (Argv[argc-1]);    int  len=argc;     char parameter[]={' i ', ' t ', ' a ', ' u ', ' G ', ' B ', ' s '}; //parameter group       if (argc==1 | | Strstr (Argv[argc-1],buffer) ==null) {         argv[argc++]= "./";          dir=opendir ("./");  //Open catalog File       }else{         dir=opendir (Argv[argc-1]);         file_=argv[argc-1]; //Open Catalog Files      }     while (Rdir=readdir (dir))!=null) { //read directory or file in directory           if (LEN!=ARGC) {              file_=rdir->d_name; //remove file name from Struct dirent structure           }else{             p= Rindex (File_, '/');              if (strlen (p)! = 1) {                  strcat (File_,buffer);             }              strcat (File_,rdir->d_name);          }         int  file=open (file_,o_rdonly,0);  //Open File          stat (file_,&statbuf);  // The stat function reads the state information of the file in the struct stat  structure, which contains the file's genus, owner, permission, timestamp, file size, and so on.       int tmp=1;      while (TMP<ARGC) {         if (Rindex (argv[tmp],parameter[0])!=NULL) {              printf ("%10d", Rdir->d_ino);         }        if (Rindex (argv[tmp],parameter[1 ]) {!=null) {              printf ("%10d"), Statbuf.st_atime);        }         if (Rindex (argv[tmp],parameter[2])!=null) {              printf ("%10d", Rdir->d_ino);               printf ("%20d", Statbuf.st_atime);              printf ("%10d", Statbuf.st_uid);              printf ("%10d", Statbuf.st_gid);             printf ("%10d", Statbuf.st_blocks);             printf ("%10d", Statbuf.st_size);         }        if (Rindex (argv[tmp],parameter[3 ]) {!=null) {             printf ("%10d"), STATBUF.ST_UID);        }         if (Rindex (argv[tmp],parameter[4])!=null) {              printf ("%10d", statBuf.st_gid);        }         if (Rindex (argv[tmp],parameter[5])!=null) {             printf ("%10d", Statbuf.st_blocks);        }         if (Rindex (argv[tmp],parameter[6])!=null) {             printf ("%10d", Statbuf.st_size);         }        tmp++;    }     Close (file);       //closes the Open file     if (LEN!=ARGC) {         int j=strlen (File_);         while (J>=lenght) {            file_[j--]=null;         }    }    printf ("%20s\n", Rdir->d_name);     }    closedir (dir);      //Close Open Directory}

/*

struct STAT {mode_t st_mode;//file corresponding mode, file, directory etc. ino_t St_ino;//inode node number dev_t St_dev;//device number dev_t St_rdev;//Special Equipment number Nlink _t St_nlink; The number of connections to the file uid_t St_uid; File owner gid_t St_gid; The file owner corresponds to the group off_t St_size; Ordinary file, corresponding to the number of file bytes time_t st_atime; The last time the file was visited time_t St_mtime; The last time the content of the file was modified time_t st_ctime; File status change time blksize_t st_blksize; The file content corresponds to the block size blkcnt_t st_blocks; The number of blocks corresponding to Wei Jian content};
struct drient {   long d_ino;   Index Node Number     off_t  d_off;   offset in catalog file     unsigned short d_reclen;  file name long      unsigned char d_type;  File Type     char d_name [name _max+1];   file name, maximum 255 characters  }int stat (const char *path, struct stat * Struct_stat); Int lstat (const char *path,struct stat *struct_stat); there is also a fstat function. 

The first parameter of two functions is the path of the file, and the second parameter is a pointer to the struct stat. A return value of 0 indicates a successful execution.
*/


This article is from the "Lnsanellinux" blog, make sure to keep this source http://lnsane784.blog.51cto.com/1226518/1930437

Using LINUX-C programming to implement simple LS commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.