Document type under Linux

xiaoxiao2021-03-06  23

File type get

First, STAT, FSTAT and LSTAT functions

#include

#include

INT Stat (const char * pathname, struct stat * buf);

INT FSTAT (INT FILEDES, STRUCT STAT * BUF);

INT LSTAT (Const Char * Pathname, Struct Stat * BUF);

The return of the three functions: If success is 0, if the error is -1.

Given a P a T H n a m e, the S t a t function returns a information structure related to this named file, and the F S T A T function obtains information about the file opened on the descriptor F i l E D e s. The L S t a t function is similar to S t a t, but when the named file is a symbolic connection, the L S t a t returns the information about the symbol connection, not the information of the file referenced by the symbol. The second parameter is a pointer, which points to a structure we should provide. These functions fill in the structure directed by B u F. The actual definition of this structure may be different, but its basic form is:

Struct stat {

UNSIGNED SHORT ST_DEV;

Unsigned short __pad1;

Unsigned long st_ino;

UNSIGNED SHORT ST_MODE;

UNSIGNED SHORT ST_NLINK;

UNSIGNED SHORT ST_UID;

UNSIGNED SHORT ST_GID;

UNSIGNED SHORT ST_RDEV;

UNSIGNED SHORT __PAD2;

Unsigned long st_size;

Unsigned long st_blksize;

Unsigned long st_blocks;

Unsigned long st_atime;

Unsigned long __unused1;

Unsigned long st_mtime;

Unsigned long __unused2;

Unsigned long st_ctime;

Unsigned long __unused3;

Unsigned long __unused4;

Unsigned long __unused5;

}

Second, file type

Most files in the Linux system are ordinary files or directories, but there are additional file types:

1. REGULAR FILE. This is the most common file type, which contains some form of data. to

This data is whether the text or binary data is different for the kernel. Interpretation of the content of ordinary documents is processed by the article

The application is done.

2. Directory file. This file contains the name of other files, and points to these files.

Interest pointer. Any process for reading a directory file can read the content of the directory, but only the kernel can

Write a directory file.

3. Character Special File. This file is used in some types of devices in the system.

4. Block Special File. This file is typically used for disk devices. All devices in the system or special files, or block special files

5. f i f O. This file is used in communication between the processes, sometimes referred to as a naming pipe. 1 4. 5 will be described.

6. Set of sockets (S O c k e t). This file is used in network communication between processes. The socket can also be used to non-network communication between the processes on a host.

7. Symbolic link. This file points to another file. 4. 1 6 Sections will be connected more and symbolic. The file type information is included in the S t _ M O D e members of the S t a T structure. The file type can be determined using the macro in Table 4 - 1. The parameters of these macros are members of the S t _ M O D e in the S t a T structure. File type macro in

Macro Fill Type S _ IsReg () Ordinary File S _ ISDIR () Directory Files S _ ISCHR () Character Special File S _ ISFIFO () Pipes or FIOS _ ISLNK () Symbol Connection S _ Issock () Set of knots

Third, the example program

1, code

The program takes its command line parameters and prints its file type for each command line parameter.

FileType.c:

#include #include #include #include int main (int Argc, char * argv []) {INT i; struct stat BUF; char * ptr; for (i = 1; i

2, compile

Make FileType

3, execute

./Filetype filetype.c / home /

4, output

FileType.c: Regular / Home /: Directory

转载请注明原文地址:https://www.9cbs.com/read-39686.html

New Post(0)