Linux system Programming _1_ directory read implement simple LS command

Source: Internet
Author: User
Tags strcmp

Idle to do nothing, write casually, implement the simple ls command:


|  1 #include <stdio.h>|  2 #include <stdlib.h>|  3 #include <dirent.h>|  4 #include <string.h>|  5|  6 int main (int argc, char **argv) |  7 {|  8 DIR *pdir;| 9 struct Dirent *stdir;| Ten int flag = 0;| 11| if (argc > 2) | ("Usage:./ls or./ls xxx\n"); | Exit (-1); | 16}| if (argc = = 1) | {| flag = 1;| if (Pdir = Opendir (".")) = = NULL) | {|-printf ("Open dir error!\n"); | + Exit (-1); | 24}| 25}| 26| if (!flag) | {| (Pdir = opendir (argv[1)) = = NULL) | []-printf ("Open dir error!\n"); | Exit (-1); | 33}| 34}| 35| (Stdir = Readdir (pdir)) = NULL) | PNS {| strcmp (Stdir->d_name, ".") = = 0 | | strcmp (Stdir->d_name, "..") = = 0) | e;| -printf ("%s\n", stdir->d_name); | 41}| 42|    43 Closedir (pdir); | 44| Return 0;| 46}

Function:

./ls--list files in the current directory

./ls xxx--list files in the specified directory

Ignore. With.. Two of directories.


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.