UNIX系统编程(2)
注:本文来自“网易”博主
第三章:文件系统
这回我们来说一下UNIX的文件系统。由于一般情况下UNIX机的硬盘会很大,所以一般你可以给它分成几个区,而每个分区又都可以有独立的文件系统。如果你是UNIX系统,你有可能看见/dev/sd/c0t0d0s0/dev/sd/c0t0d0s1
/dev/hda0/dev/hda1
$mount /dev/fd0 /mnt/floppy
$umount /mnt/floppy
#include <stdio.h>#include <sys/types.h>#include <sys/statvfs.h>int main(int argc , char *argv[]){ struct statvfs buf[1]; sync(); if( statvfs(argv[1],buf)!=0 ) { fprintf(stderr , "Cannot read super block !\n"); exit(1); } fprintf(stderr , "%4.1f %% free\n", (float)buf[0].f_bfree / buf[0].f_blocks*100 ); return 0;}编译执行: $./a.out36.7 % free
#include<sys/statvfs.h>int statvfs( char *path , struct statvfs *buf );返回值: 成功时:0失败时:-1
#include <stdio.h>#include <dirent.h>int main( int argc , char *argv[] ){ DIR *fp; struct dirent *p; fp=opendir(argv[1]); while( (p=readdir(fp))!=NULL ) { printf("%i %s\n", p->d_ino , p->d_name ); } closedir(fp); return 0;}$./a.out /96673 sbin966721 dev998945 root11 lost+found2 ..2 ...等等..1031169 lib
作用:打开一个目录#include <dirent.h>#include <sys/types.h>DIR *opendir( char *dirname );返回值 成功时返回DIR结构体的地址,失败返回NULL
作用:读取目录信息到DIR结构体。DIR结构体是什么大家也可以查一下man 3 readdir#include <linux/types.h>#include <linux/dirent.h>struct dirent *readdir(DIR *dir);返回值:成功是返回DIR的地址,失败返回NULL
作用:关闭目录#include <sys/types.h>#include <dirent.h>int closedir(DIR *dir);返回值:成功时为0,失败为1书接上回,上次我们说到了UNIX文件系统的结构,知道了有哪几个block,各是干什么用的,这回我们说说怎样对文件进行操作。
#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>int main( int argc , char *argv ){ struct stat buf[2] , *p ; if( argc!=3 ) { fprintf( stderr , "Usage : %s file1 file2\n" , argv[0] ); exit(1); } p=buf; if( stat(argv[1],p)!=0 ) { fprintf( stderr , "%s not found !\n" , argv[1] ); exit(1); } p++; if( stat(argv[2],p)!=0 ) { fprintf( stderr , "%s not found !\n" , argv[2] ); exit(1); } if( buf[0].st_mtime > buf[1].st_mtime ) //比较更新时间 printf( "%s\n" , argv[1] ); else printf( "%s\n" , argv[2] ); return 0;}作用:得到一个stat结构体。 #include <sys/types.h>#include <sys/stat.h>int stat( char *path , struct stat *buf );返回值: 成功时:0 失败时:-1
#include <sys/types.h>#include <sys/stat.h>#include <stdio.h>#define MASK 0555 //设置掩码int main( int argc , char *argv[] ){ struct stat buf[1]; mode_t mode; if( argc!=2 ) { fprintf( stderr , "Usage : %s file\n" , argv[0] ); exit(1); } if( stat(argv[1],buf)!=0 ) { fprintf( stderr , "Cannot read i-node\n" ); exit(1); } mode = ( buf[0].st_mode & MASK ); if ( chmod(argv[1],mode)!=0 ) //改变文件的权限 { fprintf( stderr , "Cannot change mode\n" ); } return 0;}作用:改变文件的权限(关于权限下面要说)#include <sys/types.h>#include <sys/stat.h>int chmod( char *path , mode_t mode );返回值: 成功时:0 失败时:-1
$ls –l /
drwxr-xr-x 2 root root 4096 11-04 22:43 bindrwxr-xr-x 4 root root 1024 09-27 02:15 bootdrwxr-xr-x 10 root root 3640 11-11 15:08 devdrwxr-xr-x 88 root root 12288 11-11 15:08 etcdrwxr-xr-x 3 root root 4096 09-27 02:40 homedrwxr-xr-x 11 root root 4096 10-31 21:49 libdrwx------ 2 root root 16384 09-27 10:51 lost+founddrwxr-xr-x 2 root root 4096 09-27 20:27 mediadrwxr-xr-x 2 root root 4096 2006-02-11 miscdrwxr-xr-x 2 root root 4096 2006-02-12 mntdrwxr-xr-x 2 root root 0 11-11 15:08 netdrwxr-xr-x 2 root root 4096 2006-02-12 opt
rwxr-xr--
rwx r-x r--所有者 同group者 其他人
111 101 100
7 5 4