Testing the type of a file in UNIX

Source: Internet
Author: User
Tags lstat

TheFile Mode, Stored inst_modeField of the file attributes,
Contains two kinds of information: the file type code, and the access permission bits. this section discusses only the type code, which you can use to tell whether the file is a directory, socket, symbolic link, and so on.
For details about access permissions seepermission bits.

There are two ways you can access the file type information in a file mode. Firstly, for each file type there isPredicate macroWhich examines a given file mode and
Returns whether it is of that type or not. secondly, you can mask out of the rest of the file mode to leave just the file type code, and compare this against constants for each of the supported file types.

All of the symbols listed in this section are defined in the header fileSys/STAT. h. The following
Predicate macros test the type of a file, given the valueMWhich isst_modeField returnedstatOn that file:

-Macro: int S_isdir( Mode_t m)

This macro returns non-zero if the file is a directory.

-Macro: int S_ischr( Mode_t m)

This macro returns non-zero if the file is a character special file (a device like a terminal ).

-Macro: int S_isblk( Mode_t m)

This macro returns non-zero if the file is a block special file (a device like a disk ).

-Macro: int S_isreg( Mode_t m)

This macro returns non-zero if the file is a regular file.

-Macro: int S_isfifo( Mode_t m)

This macro returns non-zero if the file is a FIFO special file, or a pipe. See pipes and operating OS.

-Macro: int S_islnk( Mode_t m)

This macro returns non-zero if the file is a symbolic link. See symbolic links.

-Macro: int S_issock( Mode_t m)

This macro returns non-zero if the file is a socket. See sockets.

An alternate non-POSIX method of testing the file type is supported for compatibility with BSD. The mode can be bitwise AND-edS_IFMTTo extract the file type code,
And compared to the appropriate constant. For example,

     S_ISCHR (mode)

Is equivalent:

     ((mode & S_IFMT) == S_IFCHR)
-Macro: int S_ifmt

This is a bit mask used to extract the file type code from a mode value.

These are the symbolic names for the different file type codes:

S_IFDIR
This is the file type constant of a directory file.
S_IFCHR
This is the file type constant of a character-oriented device file.
S_IFBLK
This is the file type constant of a block-oriented device file.
S_IFREG
This is the file type constant of a regular file.
S_IFLNK
This is the file type constant of a symbolic link.
S_IFSOCK
This is the file type constant of a socket.
S_IFIFO

This is the file type constant of a FIFO or pipe.

First:

#include "apue.h"#include "my_err.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 ){err_ret("lstat error", argv[i]);continue;}if ( S_ISREG(buf.st_mode) )ptr = "regular";else if ( S_ISDIR(buf.st_mode) )ptr = "direcotry";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";else if ( S_ISLNK(buf.st_mode) )ptr = "symbolic link";else if ( S_ISSOCK(buf.st_mode) )ptr = "socket";else ptr = "** unknow mode **";printf("%s\n", ptr);}exit(0);}

Second:

#include "apue.h"#include "my_err.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 ){err_ret("lstat error", argv[i]);continue;}switch (buf.st_mode & S_IFMT){case S_IFREG: ptr = "regular"; break;case S_IFBLK: ptr = "block special"; break;case S_IFCHR: ptr = "character special"; break;case S_IFIFO: ptr = "fifo"; break;case S_IFLNK: ptr = "symbolic link"; break;case S_IFSOCK: ptr = "socket"; break;case S_IFDIR: ptr = "directory"; break;default:  ptr = "** unknow **";}printf("%s\n", ptr);}exit(0);}

Related Article

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.