The topics are as follows:
Implement a single function of Linux under tree [only prints the number of directories and the number of files (no hidden files)]
Preferred we introduce several related Linux system APIs
Name of function |
Function description |
function declaration |
Opendir |
Open a directory, successfully return a dir* type pointer, failure to return null |
dir* opendir (const char* name) |
Readdir |
Reads a child member under an open directory, returns a struct pointer successfully, otherwise returns null |
struct dirent* readdir (dir* DIR)
|
Closedir |
Close the Open directory. Successful return 0, failure return-1 |
int Closedir (dir* dirp) |
struct Dirent Structural body
#include <dirent.h>//need to include header files
struct dirent{ ino_t D_ino; index [inode] Number off_t D_off; Offset in the catalog file unsigned short d_reclen; File name length unsigned char d_type; File type
Char d_name[256]; Filename }
D_type all file types to be addressed DT_BLK Block Equipment
DT_CHR character device
Dt_dir Directory
Dt_fifo Famous Pipe
Dt_lnk Symbolic Link (soft link)
Dt_reg Common Files
Dt_sock Socket Socket
Dt_unknown Unknown type
|
Precautions:
L The Linux directory has. And.. Represents the current directory and the top-level directory, respectively, and does not process a dead loop
2. Do not include hidden files, you need to use ordinary files to ignore processing
The code is as follows:
#include <stdio.h> //input/output header file
#include <stdlib.h> //eixt () function header file
#include <unistd.h> //unix Standard library header files
#include <sys/types.h> //opendir () Closedir () Readdir () required header file
#include <dirent.h> //Ibid .
#include <string.h> //String Common API header file
//function function
Successful return to Exit_success
Failed to return exit_failure
int Mytree (const char* root, unsigned int* const dirs, unsigned int* const files) {
int ret = exit_success; //return value
dir* dir = NULL; //Open Directory
char path[1024] = {0}; //splicing file relative path use
struct dirent* ptr = NULL; //Read the directory under the member struct
//Judging parameters
if (NULL = = Root | | NULL = = Dirs | | NULL = = files) {
//Error hints
printf ("func%s err:[null = = Root | | NULL = = Dirs | | NULL = = Files] ", __function__);
//exit directly
ret = exit_failure;
Goto END;
}
//Open Directory
Dir = opendir (root);
//Determine if Open failed
if (NULL = = dir) {
//Friendship Error Tips
Perror ("Func tree->opendir Error:");
ret = exit_failure;
Goto END;
}
//Iterate through the file members in the directory
while (NULL! = (ptr = Readdir (dir))) {
//Determine if the. and: Directories, or. Start of hidden files
if (0 = = strncmp (ptr->d_name, ".", 1) | | 0 = = strcmp (Ptr->d_name, "...")) {
Continue //Skip processing
}
//If it is a directory
if (Dt_dir = = Ptr->d_type) {
(*dirs) + +; //Directory number +1
sprintf (Path, "%s/%s", root, ptr->d_name);
mytree (path, dirs, files);
}else{
(*files) + +;
}
}
END:
//Determine if the directory is closed
if (NULL! = dir) {
Closedir (dir);
dir = NULL;
}
return ret;
}
Main function
int main (int argc, char* argv[]) {
unsigned int dirs = 0; //Statistics Directory number
unsigned int files = 0; //Number of statistics files
//Judging command line arguments
if (2 > ARGC) {
//No parameters, default statistics under current directory
Mytree (".", &dirs, &files);
}
else{
int i = 0;
//Multiple parameters in case
for (i = 1; i < argc; i++) {
Mytree (Argv[i], &dirs, &files);
}
}
//Output final statistic information
printf ("%d directories,%d files\n", dirs, files);
return exit_success;
}
This article is from the "Sea" blog, be sure to keep this source http://lisea.blog.51cto.com/5491873/1787156
Linux network programming face question----------all directories and files in the statistics directory