在unix系统中文件分成了其中类型。
-
Regular file. The most common type of file, which contains data
of some form. There is no distinction to the UNIX kernel whether this data is
text or binary. Any interpretation of the contents of a regular file is left to
the application processing the file.One notable exception to this is with binary executable files.
To execute a program, the kernel must understand its format. All binary
executable files conform to a format that allows the kernel to identify where to
load a program's text and data. -
Directory file. A file that contains the names of other files
and pointers to information on these files. Any process that has read permission
for a directory file can read the contents of the directory, but only the kernel
can write directly
to a directory file. Processes must
use the functions described in this chapter to make changes to a
directory. -
Block special file. A type of file providing buffered I/O
access in fixed-size units to devices such as disk drives. -
Character special file. A type of file providing unbuffered I/O
access in variable-sized units to devices. All devices on a system are either
block special files or character special files. -
FIFO. A type of file used for communication between processes.
It's sometimes called a named pipe. We describe FIFOs in Section 15.5
. -
Socket. A type of file used for network communication between
processes. A socket can also be used for non-network communication between
processes on a single host. We use sockets for interprocess communication in Chapter 16
. -
Symbolic link. A type of file that points to another file. We
talk more about symbolic links in Section 4.16
.
我不太清楚为什么是这么分的。 比如说pipe也归为FIFO类型。
下面的代码是检查指定文件是什么类型的。
使用lstat才能检查出symbolic link。
#include "apue.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");
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";
else if (S_ISLNK(buf.st_mode))
ptr = "symbolic link";
else if (S_ISSOCK(buf.st_mode))
ptr = "socket";
else
ptr = "** unknown mode **";
printf("%s/n", ptr);
}
exit(0);
}