Ls command implementation, ls command implementation

Source: Internet
Author: User
Tags file info getcolor

Ls command implementation, ls command implementation

The online code for implementing ls commands in linux is not always perfect. Mainly implements two functions: ls and ls-l. The sorting by column is not satisfactory. The original linux partition is awesome and the alignment is perfect. This code is completed by modifying bugs and modifications on the Internet. Code updates in https://github.com/ljfly/ls


Main function void do_ls (int, char []);

Void dostat (char *);/* get file info */

Void show_file_info (char *, struct stat *); // ls-l output

Void display_Ls (int cnt) // column output of ls

Void mode_to_letters (int, char []); // file attribute: drwxr-xr-x

Char * uid_to_name (uid_t);/* obtain the user name through uid */

Char * gid_to_name (gid_t);/* obtain the corresponding group name through gid */

Void getcolor (char *); // obtain the compression color

Int get_file_type (char *);/* get file type */

Int get_modify_time (char *);/* get file last modify time */

Void getWidth (); // obtain the screen width

# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <sys/types. h> # include <sys/stat. h> # include <sys/ioctl. h> # include <dirent. h> # include <pwd. h> # include <grp. h> # include <unistd. h>/* head file */# define LenOfName 256 # define maxN 1005 # define maxM 505 # define maxL 105 # define LenOfPath 256 <4 # define LS 0 // ls # define LS_A 1 // ls-a # define LS_L 2 // ls-l # define LS_TMP 3 // ls/tmp # define LS_T 5 // ls-t/* de Fine file */void do_ls (int, char []); void dostat (char *);/* get file info */void show_file_info (char *, struct stat *); // ls-l output void mode_to_letters (int, char []); // file attribute: drwxr-xr-xchar * uid_to_name (uid_t ); /* obtain the corresponding username through uid */char * gid_to_name (gid_t);/* obtain the corresponding group name through gid */void getcolor (char *); int get_file_type (char *);/* get file type */int get_modify_time (char *);/* get file last mo Dify time */void getWidth (); int cmp1 (const void *, const void *); // sort int cmp2 (const void *, const void *); // sorting the file modification time: struct outputFile {char FileName [LenOfName]; int modify_time; int file_type;} Output [maxN], OutputPoint [maxM], Temp [maxN + maxM]; int colormode, foreground, background; int terminalWidth; void dostat (char * filename) {struct stat info; if (stat (filename, & info) =-1) {pe Rror (filename); printf ("filename: % s \ n", filename);} else {char * pname = strrchr (filename, '/'); getcolor (filename ); show_file_info (pname + 1, & info);} return;}/* get file info */void mode_to_letters (int mode, char str []) {strcpy (str, "----------"); if (S_ISDIR (mode) str [0] = 'D'; if (S_ISCHR (mode) str [0] = 'C '; if (S_ISBLK (mode) str [0] = 'B'; if (mode & S_IRUSR) str [1] = 'R'; if (m Ode & S_IWUSR) str [2] = 'W'; if (mode & S_IXUSR) str [3] = 'X'; if (mode & S_IRGRP) str [4] = 'R'; if (mode & S_IWGRP) str [5] = 'W'; if (mode & S_IXGRP) str [6] = 'X '; if (mode & S_IROTH) str [7] = 'R'; if (mode & S_IWOTH) str [8] = 'W'; if (mode & S_IXOTH) str [9] = 'X'; return;} char * uid_to_name (uid_t uid) {struct passwd * pw_ptr; static char numstr [10]; if (pw_ptr = getpwuid (uid) = NULL) {sprintf (nu Mstr, "% d", uid); return numstr;} else {return pw_ptr-> pw_name ;}} /* obtain the corresponding username through uid */char * gid_to_name (gid_t gid) {struct group * grp_ptr; static char numstr [10]; if (grp_ptr = getgrgid (gid )) = NULL) {sprintf (numstr, "% d", gid); return numstr;} else {return grp_ptr-> gr_name ;}} /* obtain the corresponding group name through gid */int get_file_type (char * filename) {struct stat info; stat (filename, & info); int file_type = 0; f Ile_type = info. st_mode & S_IFMT; return file_type;}/* get file type */int get_modify_time (char * filename) {struct stat info; stat (filename, & info); int modify_time = 0; modify_time = info. st_mtime; return modify_time;}/* get file last modify time */int isadir (char * str) {struct stat info; return (lstat (str, & info )! =-1 & S_ISDIR (info. st_mode);}/* determine whether a file is a directory file */void getWidth () {char * tp; struct winsize wbuf; terminalWidth = 80; if (isatty (STDOUT_FILENO )) {if (ioctl (STDOUT_FILENO, TIOCGWINSZ, & wbuf) =-1 | wbuf. ws_col = 0) {if (tp = getenv ("COLUMNS") terminalWidth = atoi (tp);} else terminalWidth = wbuf. ws_col;} return ;} /*************************************** * ************************** to obtain the width of the terminal, default width It is 80. If the retrieval fails, the-1 option will be activated *********************************** * ******************************/int cmp1 (const void * p, const void * q) {char T1 [LenOfName], T2 [LenOfName]; strcpy (T1, (* (struct outputFile *) p ). fileName); strcpy (T2, (* (struct outputFile *) q ). fileName); int len1 = strlen (T1); int I; for (I = 0; I <len1; I ++) {if (T1 [I]> = 'A' & T1 [I] <= 'Z ') {T1 [I] = T1 [I]-'A' + 'a';} int len2 = Strlen (T2); for (I = 0; I <len2; I ++) {if (T2 [I]> = 'A' & T2 [I] <= 'Z ') {T2 [I] = T2 [I]-'A' + 'a';} return strcmp (T1, T2 );} /*************************************** ******************** * ***********************************/int cmp2 (const void * p, const void * q) {return (* (struct outputFile *) p ). modify_time <(* (struct outputFile *) q ). modify_time ;}/*********** **************************************** ****************************** * *************************/Void show_file_info (char * filename, struct stat * info_p) {char modestr [12]; mode_to_letters (info_p-> st_mode, modestr); printf ("% s", modestr); printf ("% 3d ", (int) info_p-> st_nlink); printf ("%-2 s", uid_to_name (info_p-> st_uid); printf ("%-1 s ", gid_to_name (info_p-> st_gid); printf ("% 7ld ", (long) info_p-> st_size); printf (" %. 12 s ", 4 + ctime (& info_p-> st_mtime); printf (" \ 033 [% d; % dm % s \ 033 [0m \ n ", colormode, foreground, background, filename); return ;} /*************************************** ******************* * ***********************************/void getcolor (char * filename) {struct stat info; stat (filename, & info); foreground = 30; background = 1; colormo De = 0; switch (info. st_mode & S_IFMT) {case S_IFREG:/* regular common file, color */foreground = 30; colormode = 1; break; case S_IFLNK:/* symbolic link file, blue/green */foreground = 36; colormode = 1; break; case S_IFSOCK:/* purple */foreground = 35; colormode = 1; break; case S_IFDIR: /* directory file, blue */foreground = 34; break; case S_IFBLK:/* block special block device file, yellow */foreground = 33; colormode = 1; B Reak; case S_IFCHR:/* character special character device file, yellow */foreground = 33; colormode = 1; break; case S_IFIFO:/* fifo green */foreground = 32; colormode = 1; break ;}} /*************************************** ******************** * **********************************/void display_Ls (int cnt) {int wordLenMax = 0; // the LenMax word int wordRowNum = 0; // the amount of one row int wordCo LNum = 0; // the amount of one col int I, j; for (I = 0; I <cnt; I ++) {if (I = 0) wordLenMax = strlen (Output [I]. fileName); else wordLenMax = wordLenMax> strlen (Output [I]. fileName )? WordLenMax: strlen (Output [I]. fileName);} wordLenMax + = 2; wordRowNum = terminalWidth/wordLenMax; if (cnt % wordRowNum = 0) wordColNum = cnt/wordRowNum; else wordColNum = cnt/wordRowNum + 1; for (I = 0; I <wordColNum; I ++) {j = I; while (j <cnt) {getcolor (Output [j]. fileName); printf ("\ 033 [% d; % dm %-15s \ 033 [0 m", colormode, foreground, background, Output [j]. fileName); j + = wordColNum;} printf ("\ N");} return ;} /*************************************** ***************** **************************************** **/void display_Ls_a (int cntPoint, int cnt) {int CNT = 0; int wordLenMax = 0; // the LenMax word int wordRowNum = 0; // the amount of one row int wordColNum = 0; // the amount of one col int I, j; for (I = 0; I <cntPoint; I ++) {strcpy (Temp [CNT]. fileName, OutputPo Int [I]. fileName); Temp [CNT]. file_type = OutputPoint [I]. file_type; Temp [CNT]. modify_time = OutputPoint [I]. modify_time; CNT ++; wordLenMax = wordLenMax> strlen (OutputPoint [I]. fileName )? WordLenMax: strlen (OutputPoint [I]. fileName) ;}for (I = 0; I <cnt; I ++) {strcpy (Temp [CNT]. fileName, Output [I]. fileName); Temp [CNT]. file_type = Output [I]. file_type; Temp [CNT]. modify_time = Output [I]. modify_time; CNT ++; wordLenMax = wordLenMax> strlen (Output [I]. fileName )? WordLenMax: strlen (Output [I]. fileName);} wordLenMax + = 2; wordRowNum = terminalWidth/wordLenMax; if (CNT % wordRowNum = 0) wordColNum = CNT/wordRowNum; else wordColNum = CNT/wordRowNum + 1; for (I = 0; I <wordColNum; I ++) {j = I; while (j <CNT) {getcolor (Temp [j]. fileName); printf ("\ 033 [% d; % dm %-15s \ 033 [0 m", colormode, foreground, background, Temp [j]. fileName); j + = wordColNum;} printf ("\ n ");} Return ;} /*************************************** *************** **************************************** * ***/void display_Ls_tmp (int cnt) {display_Ls (cnt); return ;} /*************************************** *************** **************************************** * ***/void do_ls (int myJudge, char myOrder []) {char dirname [maxL ]; If (myJudge! = LS_TMP & myJudge! = LS_L) {strcpy (dirname ,". ");} else {strcpy (dirname, myOrder);} DIR * dir_ptr; struct dirent * direntp; int cntOutput = 0; int cntOutputPoint = 0; char full_path [256]; if (dir_ptr = opendir (dirname) = NULL) {fprintf (stderr, "my god, I cannot open % s \ n", dirname );} else {if (myJudge! = LS_L) {while (direntp = readdir (dir_ptr ))! = NULL) {if (direntp-> d_name [0] = '. ') {strcpy (OutputPoint [cntOutputPoint]. fileName, direntp-> d_name); OutputPoint [cntOutputPoint]. file_type = get_file_type (OutputPoint [cntOutputPoint]. fileName); OutputPoint [cntOutputPoint]. modify_time = get_modify_time (OutputPoint [cntOutputPoint]. fileName); cntOutputPoint ++;} else {strcpy (Output [cntOutput]. fileName, direntp-> d_name); Output [CntOutput]. file_type = get_file_type (Output [cntOutput]. fileName); Output [cntOutput]. modify_time = get_modify_time (Output [cntOutput]. fileName); cntOutput ++ ;}} if (myJudge == LS_T) {qsort (OutputPoint, cntOutputPoint, sizeof (OutputPoint [0]), cmp2); qsort (Output, cntOutput, sizeof (Output [0]), cmp2);} else {qsort (OutputPoint, cntOutputPoint, sizeof (OutputPoint [0]), cmp1); qsort (O Utput, cntOutput, sizeof (Output [0]), cmp1 );} /*************************************** ******************* **************************************** /if (myJudge = LS | myJudge = LS_T) {display_Ls (cntOutput); closedir (dir_ptr );} /*************************************** ******************* ls & ls-t *************** **************************************** * ***/else if (myJ Udge = LS_A) {display_Ls_a (cntOutputPoint, cntOutput); closedir (dir_ptr );} /*************************************** * ***************** ls-****************** **************************************** */else if (myJudge = LS_L) {while (direntp = readdir (dir_ptr ))! = NULL) {if (direntp-> d_name [0] = '. ') {strcpy (OutputPoint [cntOutputPoint]. fileName, direntp-> d_name); OutputPoint [cntOutputPoint]. file_type = get_file_type (OutputPoint [cntOutputPoint]. fileName); OutputPoint [cntOutputPoint]. modify_time = get_modify_time (OutputPoint [cntOutputPoint]. fileName); cntOutputPoint ++;} else {strcpy (full_path, dirname); int dir_len = strlen (dirname); str Cpy (full_path + dir_len, direntp-> d_name); strcpy (Output [cntOutput]. fileName, full_path); Output [cntOutput]. file_type = get_file_type (Output [cntOutput]. fileName); Output [cntOutput]. modify_time = get_modify_time (Output [cntOutput]. fileName); cntOutput ++ ;}} int I; for (I = 0; I <cntOutput; I ++) dostat (Output [I]. fileName); closedir (dir_ptr );} /************************************* ******************** Ls-l **************** **************************************** * **/else {display_Ls_tmp (cntOutput ); closedir (dir_ptr );} /*************************************** * ***************** ls/tmp ****************** **************************************** */} return ;} /* sovle */int main (int argc, char * argv []) {getWidth (); int I; if (argc = 1) {do_ls (LS, "ls");} else {int o Rd; while (ord = getopt (argc, argv, ": lat "))! =-1) {switch (ord) {case 'A': do_ls (LS_A, "ls-a"); break; case 'l ': {if (argc = 2) do_ls (LS_L, "/"); else {for (I = 2; I <argc; I ++) {if (argv [I] [0] = '-') continue; printf ("% s: \ n", argv [I]); do_ls (LS_L, argv [I]) ;}}return;} break; case 'T': do_ls (LS_T, "ls-t"); break; default: break ;}} for (I = 1; I <argc; I ++) {if (argv [I] [0] = '-') continue; printf ("% s: \ n ", argv [I]); do_ls (LS_TMP, argv [I]) ;}} return 0 ;}/ * main */



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.