您的位置:首页 > 运维架构 > Linux

Linux 下C语言文件-目录基本操作

2012-10-31 16:19 525 查看
仅提供大概参考~ 纯手写未调试
/**
*编写一个程序,提供一个参数(文件名/路径)
*如果该参数对应文件名/目录名不存在,则创建之
*设置创建者拥有RWX权限,其他用户只有R-X权限
*如果对应文件存在
*读取并显示该文件的属性/状态信息
*创建该该文件的一个拷贝
*如果对应目录名存在
*显示该目录及其子目录结构
**/

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>

void displayproperty(char *pathname){
char buf_time[32]; //存放时间
struct passwd *psd; //从该结构体中获取文件所有者的用户名
struct group *grp; //从该结构体中获取文件所有者所属组的组名
struct stat buf;
lstat(pathname, &buf);
/*
**获取并打印文件类型
*/
//st_mode:文件内容和存取权限
if (S_ISLNK(buf.st_mode)) //判断是否为符号链接
{
printf("l: 符号链接\n");
}
else if (S_ISREG(buf.st_mode)) //判断是否为文件
{
printf("-: 文件\n");
}
else if (S_ISDIR(buf.st_mode)) //判断是否为目录
{
printf("d: 目录\n");
}
else if (S_ISCHR(buf.st_mode)) //判断是否为字符设备文件
{
printf("c: 字符设备文件\n");
}
else if (S_ISBLK(buf.st_mode)) //判断是否为块设备文件
{
printf("b: 块设备文件\n");
}
else if (S_ISSOCK(buf.st_mode)) //判断是否为socket
{
printf("s: socket\n");
}
/*
**获取并打印文件所有者的权限
*/
printf("所有者用户权限: \n");
if (buf.st_mode & S_IRUSR)
{
printf("r");
}
else
{
printf("-");
}
if (buf.st_mode & S_IWUSR)
{
printf("w");
}
else
{
printf("-");
}
if (buf.st_mode & S_IXUSR)
{
printf("x");
}
else
{
printf("-");
}
/*
**获取并打印与文件所有者同组的用户对该文件的操作权限
*/
printf("组权限:\n");
if (buf.st_mode & S_IRGRP)
{
printf("r");
}
else
{
printf("-");
}
if (buf.st_mode & S_IWGRP)
{
printf("w");
}
else
{
printf("-");
}
if (buf.st_mode & S_IXGRP)
{
printf("x");
}
else
{
printf("-");
}
/*
**获取并打印其它用户的对该文件的操作权限
*/
printf("其他用户权限:\n");
if (buf.st_mode & S_IROTH)
{
printf("r");
}
else
{
printf("-");
}
if (buf.st_mode & S_IWOTH)
{
printf("w");
}
else
{
printf("-");
}
if (buf.st_mode & S_IXOTH)
{
printf("x");
}
else
{
printf("-");
}
printf(" ");
/*
**根据uid与gid获取文件所有者的用户名与组名
*/
psd = getpwuid(buf.st_uid);
grp = getgrgid(buf.st_gid);
printf("%4d ",buf.st_nlink); //打印文件的链接数(该文件硬链接数目)
printf("%-9s", psd->pw_name); //
//打印文件拥有者
printf("%-8s", grp->gr_name); //打印文件所属用户组
printf("%6d",(int)buf.st_size); //打印文件的大小
strcpy(buf_time, ctime(&buf.st_mtime));
buf_time[strlen(buf_time) - 1] = '\0'; //去掉换行符
printf(" %s", buf_time); //打印文件的时间信息
}

//无用函数
static int get_file_size_time(const char *filename)
  {
    struct stat statbuf;
    if(stat(filename,&statbuf)==-1)
    {
      printf("Get stat on %s Error:%s\n",
        filename,strerror(errno));
      return(-1);
    }
    if(S_ISDIR(statbuf.st_mode))return(1);
    if(S_ISREG(statbuf.st_mode))
      printf("%s size:%ld bytes\tmodified at %s",
        filename,statbuf.st_size,ctime(&statbuf.st_mtime));
    return(0);
  }

int copyFile(char *filename){
char c;
int from_fd,to_fd;

from_fd=open(filename,O_RDONLY));
to_fd=open(strcat(filename,".bak"),O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR));
while(bytes_read=read(from_fd,&c,1))
{
  write(to_fd,&c,1);
}

close(from_fd);
close(to_fd);
}

void printdir(char *dir){
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if (dp = opendir(dir) == NULL){
fprintf(stderr,"cannot open directory: %s\n",dir);
return;
}

chdir(dir);
while((entry = readdir(dp)) != NULL){
lstat(entry->d_name, &statbuf);
if (S_ISDIR(statbuf.st_mode)){
if (strcmp(".",entry->d_name) == 0) || (strcmp(".",entry->d_name) ==

0)
continue;
printf("%*s%s\n ",entry->d_name);
printdir(entry->d_name);
}
else
printf("%*s%s\n",entry->d_name);
}
chdir("..");
closedir(dp);
}

//创建目录
int createDir(char *pathname){
if(mkdir(pathname, 0755)==-1)
{
perror("mkdir   error");
return   -1;
}
}

int main(int argc,char **argv)
{

if(stat(argv[1],&statbuf)==-1) {
    createDir(argv[1]);
  }
  if(S_ISDIR(statbuf.st_mode))
printdir(argv[1]);
  if(S_ISREG(statbuf.st_mode)){
  	displayproperty(argv[1]);
  copyFile(argv[1]);
}
  exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: