Files and directories under Linux

Source: Internet
Author: User
Tags function prototype lstat posix

(1) Ordinary document (regular file): This is the most commonly used file type, this file contains some form of data, the interpretation of the contents of the file is processed by the application of the file.


(2) directory file: This file contains the names of other files and pointers to information about those files. A process that has read permissions on a directory file can read the contents of the directory, but only the kernel can write the directory file directly.


(3) block special files (block special file): This type of file provides buffered access to a device such as a disk, each time a fixed length is accessed.


(4) Character special files (character special file): This type of file provides non-buffered access to the device with variable length of each access. All devices in the system are either character special files or block special files.


(5) FIFO: This file is used for interprocess communication, also known as a named pipe.


(6) socket (socket): This type of file is used for inter-process network communication. Sockets can also be used for non-network communication between processes on a host computer.


(7) Symbolic connection (symbolic Link): This type of file points to another file.

Function Description:
1. header file:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
2. Function prototype:
int stat (const char *path, struct stat *buf);
int Lstat (const char *path, struct stat *buf);
3. Parameters:
A, Path: file name
B, buf: is a pointer to (struct STAT)

The following is a detailed description of the struct STAT structure:

struct Stat {   dev_t     St_dev;     /* ID of device containing file */   ino_t     St_ino;     /* inode number */   mode_t    st_mode;    /* Protection */   nlink_t   st_nlink;   /* Number of hard links */   uid_t     st_uid;     /* User ID of Owner */   gid_t     st_gid;     /* Group ID of Owner */   dev_t     St_rdev;    /* Device ID (if special file) */   off_t     st_size;    /* Total size, in bytes *   /blksize_t st_blksize;/* blocksize for file system I/O */   blkcnt_t  St_blocks;
   /* number of 512B blocks allocated */   time_t    st_atime;   /* Time of last access */   time_t    st_mtime;   /* Time of last modification */   time_t    st_ctime;   /* Time of last status change * *            };

  

4. Return value:
Success: 0
Failed:-1

Summary: Here stat and lstat a little difference, lstat can see the symbolic link (symbokic link), and stat is not, the following I use the code to give a specific difference.

The file types are included in the St_mode member of the stat structure, and the following are the methods for judging the 7 file types:
macro File Type
S_isreg (m) ordinary file (is it a regular file?)
S_isdir (m) directory files (directory?)
S_ISCHR (m) character special file (character device?)
S_isblk (m) block special files (block device?)
S_isfifo (m) pipe or FIFO [FIFO (Named pipe)?]
S_islnk (m) Symbolic link [symbolic link? (not in posix.1-1996.)]
S_issock (m) socket [socket? (not in posix.1-1996.)]

Example of judgment:

       struct stat FileInfo;       Stat ("text.txt"//text.txt is a normal file       if(s_ Isreg (Fileinfo.st_mode))       {         printf ("Thefile is regular file\n" );       }

Well, say so much, go directly to the code to see how the type of file is judged:

#include <sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<errno.h>#include<stdio.h>#include<stdlib.h>#include<string.h>intMainintargcChar*argv[]) {    if(ARGC! =5) {printf ("argc must equal to 5!\n"); Exit (1); }         structStat FileInfo; inti;  for(i=1;i<5; i++) {bzero (&fileinfo,sizeof(FileInfo)); //stat (argv[i],&fileinfo);//here Stat and lstat a little difference, lstat can produce see symbolic connection (Symbokic link), and stat can notLstat (argv[i],&FileInfo); if(S_isreg (Fileinfo.st_mode))//Normal file{printf ("%s file is regular file\n", Argv[i]); }        Else if(S_isdir (Fileinfo.st_mode))//Catalogue{printf ("%s file is directory file\n", Argv[i]); }                Else if(S_ISCHR (Fileinfo.st_mode))//Character Special Files{printf ("%s file is character device file\n", Argv[i]); }        Else if(S_isblk (Fileinfo.st_mode))//Block Special Files{printf ("%s file is block device file\n", Argv[i]); }        Else if(S_isfifo (Fileinfo.st_mode))//FIFO or pipe{printf ("%s file is fifo\n", Argv[i]); }                Else if(S_islnk (Fileinfo.st_mode))//Symbolic Connection{printf ("%s file is Symbokic link\n", argv[1]); }                Else if(S_issock (Fileinfo.st_mode))//Sockets{printf ("%s file is socket\n", Argv[i]); }                Else{printf ("%s not a file name\n", Argv[i]); }    }        return 0;}

The difference between Lstat and stat:

commented out in the code://stat (argv[i],&fileinfo); That is, when the Lstat function is used, the output of the terminal is as follows

commented out in the code: Lstat (argv[i],&fileinfo); That is, the output of the STAT function terminal is as follows

Files and directories under Linux

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.