Obtain file types in linux

Source: Internet
Author: User
Tags lstat

File Type obtaining 1. Stat, fstat, and lstat functions # include <sys/types. h>
# Include <sys/STAT. h>
Int Stat (const char * pathname, struct stat * BUF );
Int fstat (INT filedes, struct stat * BUF );
Int lstat (const char * pathname, struct stat * BUF );
Returns the result of the three functions: 0 if the operation succeeds, and-1 if the operation fails.
Given a p a t h n a m e, s t a t function returns an information structure related to this name file, 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, l s t a t returns information about the symbolic connection, instead of the information of the file referenced by this symbol. The second parameter is a pointer pointing to a structure we should provide. These functions are in the structure directed by B u F. The actual definition of this structure may vary with implementation, 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;
};
Ii. file type most files in LINUX are common files or directories, but there are also some other file types:
1. regular file ). This is the most common file type, which contains some form of data. To
Whether the data is text or binary data is the same for the kernel. The content of a common file is interpreted
Application.
2. directory file ). This type of file contains the names of other files and a message pointing to these files
Pointer. Any process that has read permission to a directory file can read the contents of this directory, but only the kernel can
Write directory files.
3. character special file ). This type of file is used for some types of devices in the system.
4. Block special file ). This type of file is typically used for disk devices. All devices in the system, special character files, or block special files
5. f I f o. This type of file is used for inter-process communication. It is also called a named pipe. Section 1 4. 5 describes it.
6. Set of interfaces (s o c k e t ). This type of file is used for network communication between processes. A set of interfaces can also be used for non-network communication between processes on a host.
7. Symbolic Link ). This type of file points to another file. Section 4 describes more symbolic connections. The file type information is contained in the s t _ m o d e Member of the s t a t structure. You can use the macro in Table 4-1 to determine the file type. These macro parameters are members of s t _ m o d e in the s t a t structure.
File type macro in <s y s/s t a t. H>
Macro
File Type
S _ I s r e g ()
Common File
S _ I s d I R ()
Directory files
S _ I s c h r ()
Special Character files
S _ I s B L K ()
Block special files
S _ I s f I f o ()
Pipe or FIFO
S _ I s l n k ()
Symbolic connection
S _ I s o C K ()
Knot
3. Example program 1. The Code Program obtains its command line parameters and prints the file type for each command line parameter.
FileType. c:
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <stdio. h>
# Include <stdlib. h>
Int main (int argc, char * argv [])
{
Int I;
Struct stat buf;
Char * ptr;
  
For (I = 1; I <argc; I ++)
{
Printf ("% s:", argv [I]);
If (lstat (argv [I], & buf) <0)
{
Perror ("lstat error ");
Continue;
}
  
If (S_ISREG (buf. st_mode ))
Ptr = "regular ";
Else if (S_ISDIR (buf. st_mode ))
Ptr = "directory ";
Else if (S_ISCHR (buf. st_mode ))
Ptr = "character special ";
Else if (S_ISBLK (buf. st_mode ))
Ptr = "block special ";
Else if (S_ISFIFO (buf. st_mode ))
Ptr = "fifo ";
# Ifdef S_ISLNK
Else if (S_ISLNK (buf. st_mode ))
Ptr = "symbolic link ";
# Endif
# Ifdef S_ISSOCK
Else if (S_ISSOCK (buf. st_mode ))
Ptr = "socket ";
# Endif
Else
Ptr = "*** unknown mode ***";
  
Printf ("% s/n", ptr );
}
}
2. Compile Make FileType
3. Run./FileType. c/home/
4. Output
FileType. c: regular
/Home/: directory

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.