Implementation and principle of LS command [Linux drill]__linux

Source: Internet
Author: User
Intro:Personal work for two years, has never summed up their own learning, this article is a hope to summarize their learning. If I can help you, it is a useful article. This is the first of a series of Linux exercises. The main is to summarize their understanding of Linux. Start with the shell first.
order and principle: LSIn gun code, the LS command and the Dir,vdir two commands belong to the same code implementation. Three commands are controlled by variable Ls_mode variables. A macro definition based on Ls_mode is available at the beginning of the file in LS, and you can see that different values correspond to different commands.
  #define PROGRAM_NAME (Ls_mode = = Ls_ls? "ls" \
                        : (ls_mode = = ls_multi_col \
                           ? "dir": "VDir"))
The LS command is run with color control, mainly dependent on the ls_colors environment variables. LS source of the portal called the Parse_ls_color function. The main function of the function is "analytic ls_colors environment variable" for the output of LS to do color control.
  if (p = getenv ("ls_colors") = = NULL | | | *p = = ' ") return
    ;
It is worth noting that even if the ls_colors variable has a value that is not equal to the LS command output must have color, the variable is only for color configuration, not the use of color. The ls_colors variable mainly specifies the file type and the corresponding color value, and the value is printed so that it can be glimpsed.
[Nobody@myseo ~]$ echo $LS _colors
no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or= 01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*. csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*. z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00 ; 35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:
When the output color, by the LS command discretion, judged by two:--color value, and whether the output handle is a terminal (Isatty function to determine whether the output handle is TTY, when the output of LS is redirected, the function returns false).
 case color_option: {int i;
            if (optarg) i = Xargmatch ("--color", Optarg, Color_args, color_types);  else/* Using--color with no argument is equivalent to using--color=always.

            * * i = color_always; Print_with_color = (i = = Color_always | |

            (i = = = Color_if_tty && isatty (Stdout_fileno)));  if (print_with_color) {/* Don ' t use TAB characters in output. Some terminal emulators can ' t handle the combination of tabs and color codes on the  Same line.
              * * tabsize = 0;
          } break; }
This is why the LS command is very intelligent and very friendly to output in any environment. PS: A code that frets about the color output of custom shell farmers can learn from this. The value range of--color parameters can be seen from the code, commonly used for TTY, none, auto; Careful study of the code can also find that some parameter values such as force, mainly for compatibility and retention.
static char const *const color_args[] =
{/
  * Force and none are for compatibility with another COLOR-LS version * *
  "Always", "yes", "Force",
  "never", "no", "none",
  "Auto", "TTY", "If-tty", NULL
};
ls command to get the information of the project in the directory is very simple, using the standard C interface, function Opendir. The function returns the dirent structure, which contains very rich file metadata. Includes file name, size, permissions, affiliation, and so on. Code Snippet summary important part, give the following, rough browsing can know the main ideas.
  DIR *dirp;
  struct dirent *next;

  errno = 0;
  DIRP = Opendir (name);
  if (format = = Long_format | | print_block_size) {
      p = human_readable (Total_blocks, buf, human_output_opts,
                          st_n BLOCKSIZE, output_block_size);
      Dired_fputs (P, stdout, strlen (p));
      Dired_putchar (' \ n ');
  }
  if (cwd_n_used)
    print_current_files ();
About the LS command, first summed up here. Next article LDd






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.