Write your own shell command ls-r1ff

Source: Internet
Author: User
Tags lstat strcmp

The r parameter of the LS command represents the recursive listing of all files in all subfolders, 1 means that each row displays only one file or folder, and F means that the output is not sorted, and F indicates that the output of each item is followed by the corresponding */=>@| character at the end of its file type. The effect of LS-R1FF command is implemented by C language, and its source code is as follows:

#include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <pwd.h> #include <grp.h> #include <string.h>void listdir (char *); char get_type (struct stat *); int main (int Argc,char * argv[]) {if (argc = = 1) listdir ("."); Else{int index = 1;while (argc > 1) {listdir (Argv[index]); index++;argc--;}} return 0;} void Listdir (char * dirname) {dir * dir;struct stat info;//char pointer[];struct dirent * direntp;if (DIR = Opendir (dirname )) = null) {printf ("%s:\n", dirname), while ((DIRENTP = Readdir (dir)) = null) {char absolute_pathname[255];strcpy ( Absolute_pathname,dirname); strcat (Absolute_pathname, "/"); strcat (absolute_pathname,direntp->d_name); Lstat ( Absolute_pathname,&info);p rintf ("%s", Direntp->d_name);p rintf ("%c\n", Get_type (&info));} printf ("\ n"); Rewinddir (dir); while ((DIRENTP = Readdir (dir)) = NULL) {if (strcmp (Direntp->d_name, ".") = = 0 | | strcmp (Direntp->d_name, "..") = = 0) Continue;char absolute_pathname[255];strcpy (ABSOlute_pathname,dirname); strcat (Absolute_pathname, "/"); strcat (absolute_pathname,direntp->d_name); Lstat ( Absolute_pathname,&info); if (S_isdir ((&info)->st_mode)) Listdir (Absolute_pathname);}} Char get_type (struct stat * info) {if (S_ISCHR (Info->st_mode)) return ' * '; if (S_isdir (Info->st_mode)) return '/'; if (S_issock (Info->st_mode)) return ' = '; if (s_isblk (Info->st_mode)) return ' > '; if (S_islnk (Info->st_mode)) return ' @ '; if (S_isfifo (Info->st_mode)) return ' | '; Return ';}


Write your own shell command ls-r1ff

Related Article

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.