博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

愿逝者安息 让生者前行 深切哀悼5.12遇难同胞

愿逝者安息 让生者前行 深切哀悼5.12遇难同胞
creatory.cublog.cn


Unix Advanced Programming Practise2
获取文件信息
<sys/types.h>
<sys/stat.h>
<unistd.h>
int stat(const char *filename,struct stat *buf);
int fstat(int fd,struct stat *buf);
struct stat
{
dev_t st_dev; //设备号
ino_t st_ino; //i节点
mode_t st_mode; //权限位
nlink_t st_nlink; //硬链接数
uid_t st_uid; //所有者ID
gid_t st_gid; //组ID
dev_t st_rdev; //设置类型
off_t st_size; //总长度(字节数)
blksize_t st_blksize; //文件系统I/O块长度
blkcnt_t st_blocks; //对象的块数
time_t st_atime; //access最后一次访问时间
time_t st_mtime; //modify最后一个修改时间
time_t st_ctime; //create创建时间
};
测试文件类型C宏
S_ISLINK(buf.st_mode)符号连接link
S_ISREG()普通文件regular
S_ISDIR()目录 directory
S_ISCHR()字符设备 character
S_ISBLK()块设备 block
S_ISFIFO() 命名设备fifo
S_ISSOCK() 套接口socket
例:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main()
{
struct stat buf;
if(stat("./tt",&buf)==-1)
{
printf("error");exit(1);
}
else
{
printf("size:%d B\n",buf.st_size);
if(S_ISDIR(buf.st_mode))
printf("directory\n");
else if(S_ISREG(buf.st_mode))
printf("regular file\n");
else
printf("other file\n");
}
return 0;
}
测试文件访问权限
<unistd.h>
int access(const char *path,int mode);
权限位:
F_OK 文件存在
R_OK 可读
W_OK 可写
X_OK 可执行
例:检测一个文件是否可执行
#include <unistd.h>
#include <stdio.h>
int main(void)
{
if(access("./a.out",F_OK|X_OK)==0)
printf("file exist and execute\n");
return 0;
}
创建符号连接
<unistd.h>
int symlink(const char *path,const char *symlnk);
注:symlink创建的是符号连接,link创建的是硬连接(副本)
例:
#include <unistd.h>
#include <stdio.h>
int main()
{
if(symlink("./tt","./ttanother")==0)
printf("create symbol link success\n");
return 0;
}
读取符号连接信息
<sys/types.h>
<sys/stat.h>
int lstat(const char *path,struct stat *sb);

<unistd.h>
int readlink(const char *path,char *buf,int bufsiz);
改变文件系统对象权限
<sys/stat.h>
int chmod(const char *path,mode_t mode);
int fchmod(int fd,mode_t mode);
例:
使脚本myscript成为对所有者和组是可执行的
#include <sys/stat.h>
#include <stdio.h>
int main()
{
if(chmod("./tt",S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP)==-1)
printf("error");
else
printf("success");
return 0;
}
注意调用此函数将改变旧的权限位
可以用ls -l查看
改变所有权
<sys/types.h>
<unistd.h>
int chown(const char *path,uid_t owner,gid_t group);
int fchown(int fd,uid_t owner,gid_t group);
例:更改文件/etc/hosts文件所有权的root(0),组权不变
if(chown("/etc/hosts",0,-1)==-1)
//report error
else
//success
有名管道FIFO
<sys/types.h>
<sys/stat.h>
int mkfifo(const char *path,mode_t mode);
例:创建有名管道/tmp/pipe且每个人对它都有读写权限
if(mkfifo("/tmp/pipe",0666)==-1)
//report error
else
//success
获得工作目录
<unistd.h>
char *getcwd(char *buf,size_t size);
char *get_current_dir_name(void);
char *getwd(char *buf);
例:
#include <unistd.h>
#include <stdio.h>
int main()
{
char path[1024];
getcwd(path,sizeof(path)-1);
path[strlen(path)]=0;
printf("PWD:%s\n",path);
return 0;
}
改变当前工作目录
<unistd.h>
int chdir(const char *path);
例:
if(chdir("/etc")!=-1)
printf("change directory success");
在指定的文件内改变目录
int fchdir(int fd);
建立新目录
<sys/types.h>
<sys/stat.h>
int mkdir(const char *path,mode_t mode);
例:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
int main(int argc,char **argv)
{
if(mkdir(argv[1],S_IRWXU|S_IRWXG)!=-1)
printf("Folder %s create\n",argv[1]);
else
printf("mkdir error:%s\n",strerror(errno));
return 0;
}
删除目录
int rmdir(const char *path);
只能用于删除空目录
打开目录进行搜索
<sys/types.h>
<dirent.h>
DIR *opendir(const char *path);
int dirfd(DIR *dirp);
说明:
DIR指针不能用于其他函数比如fchdir,因此提供了dirfd用于转换.
DIR *dirp;
int fd;
dirp=opendir("/etc");
fd=dirfd(dirp);
fchdir(fd);
关闭目录
int closedir(DIR *dirp);
搜索目录
struct dirent *readdir(DIR *dirp);
struct dirent
{
....
char d_name[256];
};
例:
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc,char **argv)
{
DIR *dirp;
struct dirent *dp;
if(argc!=2)
{
printf("Uage:ls [dirname]\n");
exit(1);
}
if((dirp=opendir(argv[1])!=NULL)
{
while((dp=readdir(dirp))!=NULL)
printf("%s\n",dp->d_name);
closedir(dirp);
}
return 0;
}
重新回到目录开始
void rewinddir(DIR *dirp);
例:
DIR *dirp;
rewinddir(dirp);
返回目录位置
long telldir(const DIR *dirp);
在目录中定位
void seekdir(DIR *dirp,long loc);
例:
DIR *dirp;
long dirpos;
....
dirpos=telldir(dirp);
....
seekdir(dirp,dirpos);
扫描目录
<dirent.h>
int scandir(const char *dir,struct dirent ***namelist,
int(*select)(const struct dirent*),
int(*compar)(const struct dirent **,const struct dirent**));
int alphasort(const vid *a,const void *b);
说明:
dir为要扫描目录的路径名
namelist用于保存返回目录项的列表
select函数用于指定过滤函数,为NULL则不过滤保存全部
compar用于排序,若为NULL则默认不排序
例:
#include XXXX
int my_select(struct dirent *dp)
{
if(dp->d_name[0]!='h')
return 0;
return 1;
}
int my_compar(const void *d1,const void *d2)
{
struct dirent *dir1=*(struct dirent **)d1;
struct dirent *dir2=*(struct dirent **)d2;
return strcmp(dir2->d_name,dir1->d_name);
}
int main()
{
int x,n;
struct dirent **namelist;
n=scandir("/etc",&namelist,my_select,my_compar);
printf("%d entries for %s\n",n,"/etc");
for(x=0;x<0;x++)
printf("%3d:%s\n",x,namelist[x]->d_name);
if(n>0)
{
for(x=0;x<n;x++)
free(namelist[x]);
free(namelist);
}
return 0;
}

发表于: 2008-03-26 ,修改于: 2008-03-26 14:14,已浏览183次,有评论0条 推荐 投诉


网友评论

发表评论