Linux DIR)

Source: Internet
Author: User

1) fstat, stat, lstat system call
# Include
<Unistd. h>
# Include <sys/STAT. h>
# Include
<Sys/types. h> // This is not required for Linux systems and is required for UNIX systems.

Int fstat (int
Fildes, struct stat * BUF );

Returns information about the state file related to the file descriptor. This information is stored in a stat struct, which stores almost all the state information of the file.

Int Stat (const char * path, struct stat * BUF );
Int lstat (const char
* Path, struct stat * BUF );

These two system calls provide the same results, but their parameters are somewhat different. They accept a path instead of a file descriptor. In addition
The difference between lstat and stat is that if the symbolic connection is read, lstat reads the status information of the symbolic connection itself, while stat reads the information that the symbolic connection points to the file.

To get the struct pointer of stat, you also need to know how to use it. First, you need to understand some of its members:

St_mode File Permission information and type information
St_ino File-related inode
St_dev Text
Device to which the device belongs
St_uid ID of the file owner
St_gid Group ID of the file owner
St_atime Last file access time
St_ctime Last file modification time (modify permission, user, group or content)
St_mtime Last modification time
St_nlink Hard-connected data mother

As shown in the preceding table, the stat struct is rich in information, and there is no information about how to use it. This is not the topic of this article. The most common member is st_mode.

In order to conveniently obtain the File status information, some macro operations for st_mode are defined:
S_isdir // is it a directory?
S_ifblk //
Is the file block device?
S_ififo // whether the file is FIFO (Named Pipe)
S_iflnk // whether the file is a symbolic connection

There are still many macros defined, and the comprehensive introduction does not seem to conform to the topic of this article.

2) opendir ()
# Include
<Sys/types. h> // This is not required for Linux systems and is required for UNIX systems.
# Include <dirent. h>
// Dirent is the abbreviation of directory entry.
Dir * opendir (const char * Name );

Open a directory through the path and return a dir struct pointer (also called directory stream). Like the file descriptor and file stream, it corresponds to a specific directory, and used as a system call for other operating Directories
. If a system call fails, null is returned.

3) readdir ()
# Include
<Sys/types. h> // This is not required for Linux systems and is required for UNIX systems.
# Include <dirent. h>
Read the next directory entity (directory entry), readdir (Dir
*) Structure a dir struct pointer as a parameter, returns a dirent struct pointer, the specific content of this struct in: http://www.i170.com/user/killercat/Article_54617
Of
The last part has been described, including the file name (d_name member) and
Inode (d_ino member ). If this system call fails or no directory entity can be read, the return value is null.

4) chdir ()

# Include <unistd. h> // note that this header file is different from other header files.
Int chdir (const char
* Path );
Change the directory. The program can also change the Directory through chdir, just like changing the Directory through the CD command, so that
Fopen (), opendir (), where the path system call is required. You can use the relative path relative to the current directory to open the file (directory ).

5)
Closedir (dir *)
# Include <dirent. h>
# Include
<Sys/types. h> // This is not required for Linux systems and is required for UNIX systems.
Close the directory stream.

The following is a program that recursively generates a file under the directory (from the English version 3rd of Linux programming ):
# Include <unistd. h>
# Include <sys/STAT. h>
# Include <stdio. h>
# Include
<String. h>
# Include <stdlib. h>
# Include
<Dirent. h>

Void printdir (char * Dir, int depth ){

Dir * DP;
Struct dirent * entry;
Struct stat statbuf;


If (dp = opendir (DIR) = NULL ){

Fprintf (stderr, "cannot open directory: % s/n", DIR );
Return;
}
Chdir (DIR );
While (Entry = readdir (DP ))! = NULL ){
Lstat (Entry-> d_name, & statbuf );

If (s_isdir (statbuf. st_mode )){

If (strcmp ("..", entry-> d_name) = 0 |

Strcmp (".", entry-> d_name) = 0)
Continue;

Printf ("% * S % S // n", depth, "", entry-> d_name );

Printdir (Entry-> d_name, depth + 4 );
}
Else
Printf ("% * S % s/n", depth, "", entry-> d_name );
}

Chdir ("..");
Closedir (DP );
}

Int main (){

Printdir ("/Home", 0 );
}

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.