C language to write the LS command in Linux

Source: Internet
Author: User
Tags posix save file strcmp
[Reprint address: http://keren.blog.51cto.com/720558/144896]The LS command in Linux is primarily responsible for displaying the following information about the file:

The inode value of the file, the permission information, the user and the group to which they belong, the last modification date, the filename, the number of links, and so on. module partition directory file list get module get all file list under File directory put file list into linked list information output module traverse list to each file call stat get information and then Print function library information

#include <stdio.h>
#include <dirent.h>//posix.1 standard UNIX-class directory operations header file
#include <stdlib.h>// The most commonly used system functions in C and C + + languages
#include <unistd.h>//C C + + provides header file #include for access to the POSIX operating system API
<sys/stat.h>/ The/stat function can return a structure that includes all the properties of the file.
#include <sys/types.h>//Basic system data type
#include <string.h>

Two a struct
Save file or directory name
typedef struct DIR_LINK
{
	char d_name[256];
	struct dir_link* next;
Dirlink;


Save specific information for a single file
typedef struct ITEM_INFO
{
	unsigned long inode;
	Char permission[11];
	int owner;
	int group;
	off_t size; The off_t type in Inux defaults to a 32-bit long 
	time_t mod_time;
	Char name[256];
} Info

Main function dir* opendir (const char *name) Feature: Opens the directory specified by name, returns a pointer, and fails to return NULL.
Related header file: Sys/types.h dirent.h
struct dirent* readdir (dir* DIR): Get specific content in an open Directory
Related header file: Sys/types.h dirent.h



Catalog file list Get module get all file list in file directory to save list of files
dirlink* Get_dir_detail (char* dirname)
{
	dir* dir;
	struct dirent* DRT;
	dirlink* head_node = NULL;
	dirlink* cur_node = NULL;
	dirlink* next_node = NULL;
	dir = Opendir (dirname);

	if (dir = = NULL)
	{
		perror ("Can ' t Open the directory.")//Output The reason of the last function error to the standard device
	} while

	(DRT = Readdir (dir)) != NULL)
	{
		if (strcmp (Drt->d_name, ".") = 0) | | (strcmp (Drt->d_name, "..") = = 0))
		{
			continue;  Ignore. and. Directory
		}
		Next_node = (dirlink*) malloc (sizeof (Dirlink));
		if (Head_node = = NULL)
		{
			head_node = Next_node;	
		} else
		{
			cur_node->next = Next_node;
		}
			Cur_node = Next_node;
			strcpy (Cur_node->d_name, drt->d_name);
	}
		Cur_node-> next = NULL;
		Closedir (dir);
		return head_node;
}


Information Output module traversal list for each file call stat get information to print
void Print_file_info (dirlink* head_node) {struct stat file_stat;
	dirlink* Cur_node = Head_node;
	Info File_info; 
	Static char* perm[] = {"---", "--x", "-w-", "-wx", "r--", "R-x", "rw-", "rwx"};
		while (Cur_node!= NULL) {int i = 3;
		int j = 0;
		unsigned int mask = 0700;
			if (stat (cur_node->d_name,&file_stat) = = 1) {perror ("Cannot get the info\n");
			Cur_node = cur_node->next;			
		Continue
  		} if (S_isreg (File_stat.st_mode)) {file_info.permission[0]= '-';
  		} if (S_isdir (File_stat.st_mode))//is directory {file_info.permission[0]= ' d ';
   			while (i>0) {file_info.permission[1+j*3] = perm[(File_stat.st_mode & Mask) >> (i-1) *3][0];
   			FILE_INFO.PERMISSION[2+J*3] = perm[(File_stat.st_mode & Mask) >> (i-1) *3][1];
   			FILE_INFO.PERMISSION[3+J*3] = perm[(File_stat.st_mode & Mask) >> (i-1) *3][2];
   			i--;
 		 	j + +;
		mask>>=3;
  } file_info.permission[10] = '; File_info.owner = fiLe_stat.st_uid;
  File_info.group = File_stat.st_gid;
  File_info.mod_time = File_stat.st_atime;
  File_info.size = file_stat.st_size;
  File_info.inode = File_stat.st_ino;
strcpy (File_info.name,cur_node->d_name);
  Print information printf ("%u", file_info.inode);
  printf ("%s", file_info.permission);
  printf ("%d", file_info.owner);
  printf ("%d", file_info.group);
  printf ("%u", file_info.size);
  printf ("%s", CTime (&file_info.mod_time));
  
  printf ("%s \ n", file_info.name);

		
	Cur_node = cur_node->next; }
}

Main function
int main (int argc,char* argv[])
{
 dirlink* head = Get_dir_detail (".");   The input parameter is the current directory
 Print_file_info (head);
 return 0;
}


ls-d command
void Ls_d (char* dirname)
{
	dir* DIR;
	struct dirent* DRT;
	dir = Opendir (dirname);

	if (dir = = NULL)
	{
		perror ("Can ' t Open the directory."); 
	}

	while ((DRT = Readdir (dir))!= NULL)
	{
		if (strcmp (Drt->d_name, ".") = = 0)
		{
			printf ("%s \ n", drt->d_name);
		}
		Closedir (dir);




[Reprint address: http://my.oschina.net/sharelinux/blog/115389]Ls-r command
void Ls_r (char* dirname)
{
	dir* DIR;
	struct dirent* DRT;
	dir = Opendir (dirname);
	struct stat file_stat;

	if (dir = = NULL)
	{
		perror ("Can ' t Open the directory."); 
	}
    	ChDir (dirname);

	while ((DRT = Readdir (dir))!= NULL)
	{
		stat (drt->d_name,&file_stat);
		if (S_isdir (file_stat.st_mode))    //is the directory
  		{
			if (strcmp (Drt->d_name, ".") = 0) | | (strcmp (Drt->d_name, "..") = = 0))
		{
			continue;  Ignore. and. Directory
		}
			printf ("%s \ n", drt->d_name);
			Ls_r (Drt->d_name);
  		}
		else
		{
			printf ("%s \ n", Drt->d_name)
		;

	}
		ChDir ("..");
		Closedir (dir);




Ls-a command
void Ls_a (char* dirname)
{
	dir* DIR;
	struct dirent* DRT;
	dir = Opendir (dirname);
	struct stat file_stat;

	if (dir = = NULL)
	{
		perror ("Can ' t Open the directory."); 
	}

	while ((DRT = Readdir (dir))!= NULL)
	{
		stat (drt->d_name,&file_stat);
		printf ("%s \ n", drt->d_name);
	}

		Closedir (dir);


Ls-i command
void Ls_i (char* dirname)
{
	dir* DIR;
	struct dirent* DRT;
	dir = Opendir (dirname);
	struct stat file_stat;

	if (dir = = NULL)
	{
		perror ("Can ' t Open the directory."); 
	}

	while ((DRT = Readdir (dir))!= NULL)
	{
		if (strcmp (Drt->d_name, ".") = 0) | | (strcmp (Drt->d_name, "..") = = 0))
		{
			continue;  Ignore. and. Directory
		}

		stat (drt->d_name,&file_stat);
		printf ("%u\t%s\n", File_stat.st_ino, Drt->d_name);
	}

		Closedir (dir);






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.