UNIX环境高级编程学习笔记二_文件与目录习题

部分习题练习

4.6 .创建一个类似cp(1)的程序,复制包含空洞的文件,但不将0写到输出文件中
(1). 首先创建一个含有空洞的文件,代码如下:
#include"apue.h"
#include<dirent.h>
#include<fcntl.h>
		
char str[] = "This is new string";
char str1[] = "This is a string";

int main(void)
{
	int fd;
	/* Creat a new file named my.hole */
	if( (fd = creat("my.hole", FILE_MODE)) < 0)
		err_sys("Creat file error");
	/* Append the file with lseek */
    if( (write(fd, str1, strlen(str1)) != strlen(str1))) 
            err_sys("once wirte error!"); 
	if(lseek(fd, 400, SEEK_SET) == -1)
		err_sys("Can not seek");
	/* write strbuf to file */
    printf("The str length: %ld \n", strlen(str) );
	if( (write(fd, str, strlen(str)) != strlen(str)) )
			err_sys("str write error");
	return 0;
}

这里有两个问题需要注意

  1. 注释在c90中只能用 ‘ /* … */ ’而不能用 ‘ // ’
  2. 使用write函数写入字符串时遇到一个问题,在写成1的情况时,write函数不能将字符串写入到文件中(原因未知…),情况2加个括号就成了…,有点玄学。
    情况1:if( write(fd, str, strlen(str) != strlen(str)) )
    情况2:if( (write(fd, str, strlen(str)) != strlen(str)) )

也可以对一个已存在的文件进行扩展,代码如下:

#include"apue.h"
#include<dirent.h>
#include<fcntl.h>
		
char str[] = "This is new string";

int main(int argc, char *argv[])
{
	int fd;
    ssize_t nu;
	/* Find filename in current dir*/
	if(argc != 2)
		err_quit("usage: no argv[1] <description#>");
	if( (fd = open(argv[1], O_RDWR)) < 0)           //读写模式
		err_quit("usage: open error <description#>");
    printf("fd1: %d  ", fd);
	/* Append the file with lseek */
	if(lseek(fd, 800, SEEK_SET) == -1)             //从开始处后移800字节
		err_sys("Can not seek");
	/* write buf to file */
    printf("The str length: %ld \n", strlen(str) );
	if( (nu = write(fd, str, strlen(str)) != strlen(str)) )
			err_sys("str write error");
    printf("Output %zd Byte \n", nu);          
    close(fd);
	return 0;
}

这里也有一个问题,nu接收write函数返回值后再打印时总是位0?,可能是ssize_t数据类型输出的问题,还未解决。

(2). 复制含有空洞的文件,除去空洞,代码如下:
#include"apue.h"
#include<fcntl.h>

#define bufsize 4096

int main(int argc, char *argv[])
{
    /*read from first file*/
    int fd1, fd2, n, i, j=0;
    char str[bufsize], buf[bufsize];
    if(argc != 3)
        err_sys("arguments error");
    if( (fd1 = open(argv[1], O_RDONLY)) == -1)
        err_sys("open file error");
    if( (fd2 = creat(argv[2], FILE_MODE)) < 0)
        err_sys("creat file error");
    /*process the data, remove invalued 0/ */
    while( (n = read(fd1, str, bufsize)) ){
        for(i=0; i<n; i++)
            if(str[i])
                buf[j++] = str[i]; 
    /*write to the second file*/
        if((write(fd2, buf, strlen(buf))) != strlen(buf))
                err_sys("write file error");
        memset(buf, 0, j);
        j = 0;
    }
    return 0;
}
4.10 系统可打开文件数的限制对4.22节的myftw程序有什么影响

因为系统把目录也看作文件,所以打开一个新的目录就必须保持上级目录文件的打开状态,此时如果打开文件数目有限制,就会使得程序遍历的目录深度有影响。

4.11 书中4.22节myftw从不改变目录,对这种处理方法进行改动:每遇到一个目录就使用chdir函数改变当前工作目录,这样每次调用lstat时可以直接使用文件名而非路径名,遍历完目录下文件项后使用chdir("…")返回上级目录,比较两种方法的运行时间。

为进行对比,先使用原来的代码即ftw8.c文件对" ~ "目录进行遍历:
具体代码参考上一篇笔记:
UNIX高级环境编程学习笔记二
执行结果如下:
在这里插入图片描述
然后更改部分代码,使用chdir函数改变工作目录并用单个文件名代替完整的路径名。
更改后源文件代码如下:

#include "apue.h"
#include <dirent.h>
#include <limits.h>
#include <time.h>

/* function type that is called for each filename */
typedef	int	Myfunc(const char *, const struct stat *, int);

static Myfunc	myfunc;
static int		myftw(char *, Myfunc *);
static int		dopath(Myfunc *);

static long	nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;

int
main(int argc, char *argv[])
{
    clock_t start, end;        /*define time value*/
	int		ret;
    double seconds;

    start = clock();           /*start cal time*/

	if (argc != 2)
		err_quit("usage:  ftw  <starting-pathname>");

	ret = myftw(argv[1], myfunc);		/* does it all */

	ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;

    end = clock();
    seconds = (double)(end - start)/CLOCKS_PER_SEC;
    printf("Use time: %.9f \n", seconds);

	if (ntot == 0)
		ntot = 1;		/* avoid divide by 0; print 0 for all counts */
	printf("regular files  = %7ld, %5.2f %%\n", nreg,
	  nreg*100.0/ntot);
	printf("directories    = %7ld, %5.2f %%\n", ndir,
	  ndir*100.0/ntot);
	printf("block special  = %7ld, %5.2f %%\n", nblk,
	  nblk*100.0/ntot);
	printf("char special   = %7ld, %5.2f %%\n", nchr,
	  nchr*100.0/ntot);
	printf("FIFOs          = %7ld, %5.2f %%\n", nfifo,
	  nfifo*100.0/ntot);
	printf("symbolic links = %7ld, %5.2f %%\n", nslink,
	  nslink*100.0/ntot);
	printf("sockets        = %7ld, %5.2f %%\n", nsock,
	  nsock*100.0/ntot);
	exit(ret);
}

/*
 * Descend through the hierarchy, starting at "pathname".
 * The caller's func() is called for every file.
 */
#define	FTW_F	1		/* file other than directory */
#define	FTW_D	2		/* directory */
#define	FTW_DNR	3		/* directory that can't be read */
#define	FTW_NS	4		/* file that we can't stat */

static char	*fullpath;		/* contains full pathname for every file */
static size_t pathlen;

static int					/* we return whatever func() returns */
myftw(char *pathname, Myfunc *func)
{
	fullpath = path_alloc(&pathlen);	/* malloc PATH_MAX+1 bytes */
										/* ({Prog pathalloc}) */

	if (pathlen <= strlen(pathname)) {
		pathlen = strlen(pathname) * 2;
		if ((fullpath = realloc(fullpath, pathlen)) == NULL)
			err_sys("realloc failed");
	}
	strcpy(fullpath, pathname);
	return(dopath(func));
}

/*
 * Descend through the hierarchy, starting at "fullpath".
 * If "fullpath" is anything other than a directory, we lstat() it,
 * call func(), and return.  For a directory, we call ourself
 * recursively for each name in the directory.
 */
static int					/* we return whatever func() returns */
dopath(Myfunc* func)
{
	struct stat		statbuf;
	struct dirent	*dirp;
	DIR				*dp;
	int				ret, n;
    
	if (lstat(fullpath, &statbuf) < 0)	/* stat error */
		return(func(fullpath, &statbuf, FTW_NS));
	if (S_ISDIR(statbuf.st_mode) == 0)	/* not a directory */
		return(func(fullpath, &statbuf, FTW_F));

	/*
	 * It's a directory.  First call func() for the directory,
	 * then process each filename in the directory.
	 */
	if ((ret = func(fullpath, &statbuf, FTW_D)) != 0)
		return(ret);
	if ((dp = opendir(fullpath)) == NULL)	/* can't read directory */
		return(func(fullpath, &statbuf, FTW_DNR));

    if (chdir(fullpath) < 0){
        err_ret("change work dir failed"); 
    }

	while ((dirp = readdir(dp)) != NULL) {
		if (strcmp(dirp->d_name, ".") == 0  ||
		    strcmp(dirp->d_name, "..") == 0)
				continue;		/* ignore dot and dot-dot */
        if((fullpath = realloc(fullpath,pathlen)) == NULL)
            err_sys("realloc failed");
		strcpy(fullpath, dirp->d_name);	    /* rename fullpath */
		if ((ret = dopath(func)) != 0)		/* recursive */
			break;	/* time to leave */
	}
    
    if (chdir("..")<0)
        err_sys("back to father dir failed");

	if (closedir(dp) < 0)
		err_ret("can't close directory %s", fullpath);
	return(ret);
}

static int
myfunc(const char *pathname, const struct stat *statptr, int type)
{
	switch (type) {
	case FTW_F:
		switch (statptr->st_mode & S_IFMT) {
		case S_IFREG:	nreg++;		break;
		case S_IFBLK:	nblk++;		break;
		case S_IFCHR:	nchr++;		break;
		case S_IFIFO:	nfifo++;	break;
		case S_IFLNK:	nslink++;	break;
		case S_IFSOCK:	nsock++;	break;
		case S_IFDIR:	/* directories should have type = FTW_D */
			err_dump("for S_IFDIR for %s", pathname);
		}
		break;
	case FTW_D:
		ndir++;
		break;
	case FTW_DNR:
		err_ret("can't read directory %s", pathname);
		break;
	case FTW_NS:
		err_ret("stat error for %s", pathname);
		break;
	default:
		err_dump("unknown type %d for pathname %s", type, pathname);
	}
	return(0);
}

与上一验证代码遍历的目录一致,也为" ~ "目录,执行结果如下:
在这里插入图片描述
由对比可知,使用chdir后用文件名作为lstat的参数,运行时间减少了14%左右。所以效率算是有一个比较大的提升。

4.15 cpio 和 tar 命令的使用

(1) cpio 是用来建立,还原备份档的工具程序,它可以加入,解开 cpio 或 tar 备份档内的文件。
(2) tar是用来建立,还原备份文件的工具程序,它可以加入,解开备份文件内的文件。

4.16 循环创建目录检测UNIX系统对目录树的深度

以下为测试代码:

#include"apue.h"
#include<stdlib.h>
#include<sys/stat.h>
#include<unistd.h>
#include<string.h>

#define Max_length 65535

char alp[] = "abcdefghigklmnopqrstuvwxyz";
int main()
{
    short ranu[2] = {0}, i;
    int dirdp=0;
    char dirstr[3] = {0}, cwd[Max_length];
    /*creat a dir name randomly*/
    for(i=0; i<2; i++){
        ranu[i] = rand() % 26;
        dirstr[i] = alp[ranu[i]];
    }
    /*creat a dir int work dir*/
    while(1){
        if( mkdir(dirstr, S_IRUSR|S_IXUSR|S_IWUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) < 0 ){
            err_sys("can not creat new dir");
            break;
        }
        dirdp++;
        if(dirdp % 1000 == 0)
            printf("depth: %d \n", dirdp);
    /*change the work dir to new dir*/
        if(chdir(dirstr) < 0)
            err_sys("can not change current dir");
    /*printf current pwd*/
        if( getcwd(cwd, sizeof(cwd)) == NULL )
            err_sys("can not get fullpath of this dir");
        /*printf("Current work dir: %s \n", cwd);*/
        memset(cwd, 0, strlen(cwd));
        for(i=0; i<2; i++){
            ranu[i] = rand() % 26;
            dirstr[i] = alp[ranu[i]];
        }
    }
    /*loop*/
    return 0;
}

在测试代码中,首先生成随机的两个字母的目录名,再把新目录作为工作目录,再在新目录下生成目录,如此循环。测试生成目录深度限制,结果如下:
(1) 本次采用216 个字节的字符数组保存生成的完整目录。在字符数组长度范围内获取的深度都成功,与书中答案描述4096个字节的长度即失效的结果不同,猜测可能为系统不同所致。只看本次结果,getcwd函数返回的长度只与系统size_t的长度有关。

char *getcwd(char *buf, size_t size);

(2) 同时也如书上可得出unix类系统下一般对目录深度没有限制。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值