#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <iostream>
using std::cout;
using std::endl;
using std::cerr;
#define MAJOR(a) (int)((unsigned short)a >> 8)
#define MINOR(a) (int)((unsigned short)a & 0xFF)
bool fileType(const struct stat& fileStat)
{
cout << "file-type";
switch(fileStat.st_mode & S_IFMT) {
case S_IFSOCK:
cout << "socket";
break;
case S_IFLNK:
cout << "symbolic link";
break;
case S_IFREG:
cout << "regular file";
break;
case S_IFBLK:
cout << "block device";
break;
case S_IFDIR:
cout << "directory";
break;
case S_IFCHR:
cout << "character device";
return true;
case S_IFIFO:
cout << "FIFO";
break;
default:
cout << "unknown?";
break;
}
cout << "\n";
return false;
}
void filePerm(const struct stat& fileStat, char* perm)
{
strcpy(perm, "---------");
switch(fileStat.st_mode & S_IFMT) {
case S_IFSOCK:
perm[0] = 's';
break;
case S_IFLNK:
perm[0] = 'l';
break;
case S_IFREG:
perm[0] = '-';
break;
case S_IFBLK:
perm[0] = 'b';
break;
case S_IFDIR:
perm[0] = 'd';
break;
case S_IFCHR:
perm[0] = 'c';
break;
case S_IFIFO:
perm[0] = 'p';
break;
default:
perm[0] = '?';
break;
}
if (fileStat.st_mode & S_IRUSR)
perm[1] = 'r';
if (fileStat.st_mode & S_IWUSR)
perm[2] = 'w';
if (fileStat.st_mode & S_IXUSR)
perm[3] = 'x';
if (fileStat.st_mode & S_IRGRP)
perm[4] = 'r';
if (fileStat.st_mode & S_IWGRP)
perm[5] = 'w';
if (fileStat.st_mode & S_IXGRP)
perm[6] = 'x';
if (fileStat.st_mode & S_IROTH)
perm[7] = 'r';
if (fileStat.st_mode & S_IWOTH)
perm[8] = 'w';
if (fileStat.st_mode & S_IXOTH)
perm[9] = 'x';
}
int main(int argc, char* argv[])
{
if (argc != 2) {
cerr << "Usage:" << argv[0] << "file-name>" << endl;
exit(EXIT_FAILURE);
}
struct stat fileStat;
if (lstat(argv[1], &fileStat) == -1) {
perror("stat error");
}
cout << "file-name:" << argv[1] << endl;
cout << "st_ino = " << fileStat.st_ino <<endl;
cout << "device major:" << major(fileStat.st_dev) << ", minor:" << minor(fileStat.st_dev) << endl;
if (fileType(fileStat)) {
cout << "--------------------" << endl;
cout << "major:" << MAJOR(fileStat.st_dev) << ", minor:" << MINOR(fileStat.st_dev) << endl;
}
fprintf(stdout, "file permisson : %o", fileStat.st_mode & 07777);
char perm[11] = {0};
filePerm(fileStat, perm);
cout << ", msg:" << perm << endl;
cout << "st_nlink = " << fileStat.st_nlink << endl;
cout << "st_uid = " << fileStat.st_uid << endl;
cout << "st_gid = " << fileStat.st_gid << endl;
cout << "st_size = " << fileStat.st_size << endl;
cout << "st_blksize = " << fileStat.st_blksize << endl;
cout << "st_blocks = " << fileStat.st_blocks << endl;
cout << "st_atime = " << fileStat.st_atime << endl;
cout << "st_ctime = " << fileStat.st_ctime << endl;
cout << "st_mtime = " << fileStat.st_mtime << endl;
}
其中S_IFBLK是块设备文件,S_IFCHR是字符设备文件,S_IFIFO是命名管道
块设备文件和字符设备文件的区别是:
Linux中I/O设备分为两类:字符设备和块设备。两种设备本身没有严格限制,但是,基于不同的功能进行了分类。
(1)字符设备:提供连续的数据流,应用程序可以顺序读取,通常不支持随机存取。相反,此类设备支持按字节/字符来读写数据。举例来说,键盘、串口、调制解调器都是典型的字符设备。
(2)块设备:应用程序可以随机访问设备数据,程序可自行确定读取数据的位置。硬盘、软盘、CD-ROM驱动器和闪存都是典型的块设备,应用程序可以寻址磁盘上的任何位置,并由此读取数据。此外,数据的读写只能以块(通常是512B)的倍数进行。与字符设备不同,块设备并不支持基于字符的寻址。
总结一下,这两种类型的设备的根本区别在于它们是否可以被随机访问。字符设备只能顺序读取,块设备可以随机读取。
命名管道的介绍: