Q1. 用stat 函数替换图4-3程序中的lstat 函数,如若命令行参数之一是符号链接,会发生什么变化?
改之前:
[root@clstore3 ~]# ./lstat /etc/passwd /etc /dev/log /dev/tty /dev/sr0 /dev/cdrom
/etc/passwd: regular
/etc: directory
/dev/log: socket
/dev/tty: character special
/dev/sr0: block special
/dev/cdrom: symbolic link
可以看出,lstat 把/dev/cdrom 当着软链接去看。
现在我们把程序lstat 改为stat 之后,允许结果如下,可以看stat查看的是符号链接所引用的文件的信息。
结论:lstat 函数类似于stat,但当命名的文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是有该符号链接引用的文件的信息。
[root@clstore3 ~]# ./stat /etc/passwd /etc /dev/log /dev/tty /dev/sr0 /dev/cdrom
/etc/passwd: regular
/etc: directory
/dev/log: socket
/dev/tty: character special
/dev/sr0: block special
/dev/cdrom: block special
#include "apue.h"
int
main(int argc, char *argv[])
{
int i;
struct stat buf;
char *ptr;
for ( i = 1; i < argc; i++) {
printf("%s: ", argv[i]);
if (stat(argv[i], &buf) < 0){ /* this need to change lstat from stae*/
err_ret("lstat error");
continue;
}
if ( S_ISREG(buf.st_mode))
ptr = "regular";
else if ( S_ISDIR(buf.st_mode))
ptr = "directory";
else if ( S_ISCHR(buf.st_mode))
ptr = "character special";
else if ( S_ISBLK(buf.st_mode))
ptr = "block special";
else if ( S_ISFIFO(buf.st_mode))
ptr = "fifo special";
else if ( S_ISLNK(buf.st_mode))
ptr = "symbolic link";
else if ( S_ISSOCK(buf.st_mode))
ptr = "socket";
else
ptr = "** unknow mode **";
printf("%s\n", ptr);
}
exit(0);
}
Q2.
如果文件模式创建屏蔽字是777(八进制),结果会怎样?用shell 的umask 命令验证该结果。
[root@clstore3 ~]# umask
0022
改为777后
[root@clstore3 ~]# umask 777
[root@clstore3 ~]# umask
0777
创建一个test3文件子后,只有root可以看,也不可以更改了。
[root@clstore3 ~]# vim test3.txt
[root@clstore3 ~]# ls -al test3.txt
----------. 1 root root 12 Nov 13 10:44 test3.txt
Q3.关闭一个你所有拥有的用户的读权限,将导致拒绝你访问你自己的文件,对此进行验证。
[root@clstore3 ~]# chmod 640 /home/zhangb/Makefile
[root@clstore3 ~]# ls -al /home/zhangb/Makefile
-rw-r-----. 1 root root 18709 May 22 17:32 /home/zhangb/Makefile
切换到用户zhangb,由于用户zhangb没有该文件的读权限,所以不能查看该文件。
[root@clstore3 ~]# su - zhangb
522868 $ ls -al Makefile
-rw-r-----. 1 root root 18709 May 22 17:32 Makefile
522868 $ less Makefile
Makefile: Permission denied
522868 $ whoami
522868
Q4,创建文件foo 和bar后,运行图4-9的程序,将发生什么情况?
什么都没有发生, 权限为没有任何改变。
[root@clstore3 ~]# touch foo bar
[root@clstore3 ~]# ls -al foo bar
-rw-r--r--. 1 root root 0 Nov 13 11:34 bar
-rw-r--r--. 1 root root 0 Nov 13 11:34 foo
[root@clstore3 ~]# ./create_foobar
[root@clstore3 ~]# ls -al foo bar
-rw-r--r--. 1 root root 0 Nov 13 11:34 bar
-rw-r--r--. 1 root root 0 Nov 13 11:34 foo
###############################################################
[root@clstore3 ~]# cat create_foobar.c
#include "apue.h"
#include <fcntl.h>
#define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
int
main(void)
{
umask(0);
if ( creat("foo",RWRWRW) < 0)
err_sys("create error for foo");
umask ( S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (creat("bar", RWRWRW) < 0 )
err_sys("create error for bar");
exit(0);
}
Q4.5 4.12节中讲到一个普通文件的大小可以是0,同时我们又知道st_size字段是为目录或符号链接定义的,那么目录和符号链接的长度可否为0?
A:对于普通文件,其文件长度可以是0,在开始读写这种文件是,将得到文件(end-of-file)指示。
对于目录,文件长度通常是一个(16or512)的整倍数。
符号链接-文件长度是在文件名中的实际字节数。
[root@clstore3 /]# ls -al /dev/cdrom
lrwxrwxrwx. 1 root root 3 May 29 21:55 /dev/cdrom -> sr0
[root@clstore3 var]# ls -al mail
lrwxrwxrwx. 1 root root 10 Sep 15 2014 mail -> spool/mail
Q4.6,编写一个类似cp1的程序,它复制包含空洞的文件,但不将字节0写到输入文件中去.
[root@clstore3 ~]# od -c file.hole
0000000 a b c d e f g h i j \0 \0 \0 \0 \0 \0
0000020 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
*
0040000 A B C D E F G H I J
0040012
[root@clstore3 ~]# od -c file.out
0000000 a b c d e f g h i j A B C D E F
0000020 G H I J
0000024
程序代码部分
#include <stdio.h>
#include <stdlib.h>
/* this program is to copy a file ( ignore the hole part if there is a hole in it
* zhangbo 2015/11/13 */
int main ()
{
int c;
FILE *in, *out;
in = fopen("file.hole","r");
out = fopen("file.out","w");
c = fgetc(in);
while ( c != EOF )
{
/* this is to copy the file, if encounter file with a hole in it ‘\0‘, do nothing,
* otherwise, copy the content to file.out*/
if ( c != ‘\0‘)
fputc(c,out);
c = fgetc(in);
}
exit(0);
}
本文出自 “bobo5620301” 博客,请务必保留此出处http://bobo5620301.blog.51cto.com/946576/1712526
原文:http://bobo5620301.blog.51cto.com/946576/1712526