依睛(IT blog) 我回来了,PHP<-->C/C++ LINUX

笨鸟

统计

积分与排名

友情连接

最新评论

fstat printf用法

C/C++ code
								
/* * mstat.c - Naive stat(1) program */ #include < unistd.h > #include < sys / types.h > #include < sys / stat.h > #include < fcntl.h > #include < time.h > #include < stdlib.h > #include < stdio.h > int main( int argc, char ** argv) { struct stat buf; mode_t mode; char type[ 80 ]; int fd; /* validate the command line */ if (argc != 2 ){ puts( " USAGE: mstat {file} " ); exit(EXIT_FAILURE); } /* open the file */ if ((fd = open(argv[ 1 ], O_RDONLY)) < 0 ){ perror( " open " ); exit(EXIT_FAILURE); } /* get files stats */ if ((fstat(fd, & buf)) < 0 ) { perror( " fstat " ); exit(EXIT_FAILURE); } mode = buf.st_mode; printf( " FILE: %s\n " ,argv[ 1 ]); printf( " INODE: %ld\n " ,buf.st_ino); printf( " DEVICE: %d,%d\n " ,major(buf.st_dev), minor(buf.st_dev)); printf( " MODE: %#o\n " ,mode & ~ (S_IFMT)); printf( " LINKS: %d\n " ,buf.st_nlink); printf( " UID: %d\n " ,buf.st_uid); printf( " GID: %d\n " ,buf.st_gid); if (S_ISLNK(mode)) strcpy(type, " Symbolic line " ); else if (S_ISREG(mode)) strcpy(type, " Regular file " ); else if (S_ISDIR(mode)) strcpy(type, " Directory " ); else if (S_ISCHR(mode)) strcpy(type, " Character device " ); else if (S_ISBLK(mode)) strcpy(type, " Block device " ); else if (S_ISFIFO(mode)) strcpy(type, " FIFO " ); else if (S_ISSOCK(mode)) strcpy(type, " Socket " ); else strcpy(type, " Unknow type " ); printf( " TYPE: %s\n " ,type); printf( " SIZE: %ld\n " ,buf.st_size); printf( " BLK SIZE: %ld\n " ,buf.st_blksize); printf( " BLOCKS: %d\n " ,( int )buf.st_blocks); printf( " ACCESSED: %s " ,ctime( & buf.st_atime)); printf( " MODIFIED: %s " ,ctime( & buf.st_mtime)); printf( " CHANGED: %s " ,ctime( & buf.st_ctime)); /* close the file */ if (close(fd) < 0 ) { perror( " close " ); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); }


这个是linux编程指南2里面的例子。两个问题:
1.strcpy()不是该#include <string.h>才能用么?
2.printf("    MODE: %#o\n",mode & ~(S_IFMT));//%#o是什么意思?

posted on 2008-11-14 16:13 向左向右走 阅读(708) 评论(0)  编辑 收藏 引用 所属分类: C/C++学习资料库Linux 学习库

只有注册用户登录后才能发表评论。