Write your own shell command ls-r1ff

Source: Internet
Author: User
Tags lstat strcmp

The R-parameters of the LS command represent a recursive listing of all the files in all subdirectories, and 1 means that each row shows only one file or directory, and F means that the output is not sorted. F represents the end of each item's output according to its file type corresponding to the plus */=>@| character. Through C language implementation of the LS-R1FF command effect, its source code such as the following:

#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.