Implement the Simple LS command

Source: Internet
Author: User
Tags strcmp

Today, the weather is good, to achieve their own LS command to play, now implement the-l parameter, and then the other parameters to complete the completion:)

1#include <stdio.h>2#include <sys/types.h>3#include <sys/stat.h>4#include <dirent.h>5#include <string.h>6#include <pwd.h>7#include <time.h>8 9 voidShowls (structStat *p,Char*d_name)Ten { One     CharFlag; A     if(S_isreg (P->st_mode)) flag='-'; -     Else if(S_isdir (P->st_mode)) flag='D'; -         Else if(S_ISCHR (P->st_mode)) flag='C'; the         Else if(S_isblk (P->st_mode)) flag='b'; -         Else if(S_isfifo (P->st_mode)) flag='P'; -         Else if(S_islnk (P->st_mode)) flag='L';  -     Elseflag='?'; +printf"%c\t", flag); -  +printf"%o\t",p->st_mode&0777); A  atprintf"%d\t",p->st_nlink); -  -     structpasswd *s; -S=getpwuid (p->st_uid); -printf"%s\t",s->pw_name); -  inprintf"%ld\t",p->st_size); -  to     Charbuf[ -]; +time_t t=p->St_mtime; -Strftime (BUF,sizeof(BUF),"%F%T", LocalTime (&t)); theprintf"%s\t", buf); *  $printf"%s\t", d_name);Panax Notoginsengprintf"\ n"); - } the  + intMainintargcChar*argv[]) A { the     Char*path; +     if(argc==1) -Path="."; $     if(argc==2) $path=argv[1];  -     if(argc==3) -path=argv[2]; the  -     if(argc>=2&AMP;&AMP;STRCMP (argv[1],"- L")==0)Wuyi     {     the         if(strcmp (argv[1],"- L")==0&&argc==2) -Path="."; Wu         if(Opendir (path) = =NULL) -         { Aboutprintf"File open failed! Please enter it correctly! "); $printf"%m"); -             return-1; -         } -DIR *p = opendir (path);//pointer to all the contents of the current path A chdir (path); +         structDirent *q;//structure partition content block theprintf"type \ t permissions \ t-link number \ t user name \ t byte number \ t last modified time \ t file name \ n"); -          while((Q=readdir (p))! =NULL) $         { the             structstat s; theStat (q->d_name,&s); the             if(q->d_name[0]=='.') the                 Continue;//do not show hidden files -Showls (&s,q->d_name); in         } theprintf"\ n"); the Closedir (p); About         return 0; the     } the     if(Opendir (path) = =NULL) the     { +printf"File open failed! Please enter it correctly! "); -printf"%m"); the         return-1;Bayi     } theDIR *p =opendir (path); the chdir (path); -     structDirent *Q; -      while((Q=readdir (p))! =NULL) the     {     the         if(q->d_name[0]=='.') the             Continue;//do not show hidden files theprintf"%s\t",q->d_name); -     } theprintf"\ n"); the Closedir (p); the     return 0;94}

Execution Result:

Implement the Simple LS command

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.