unix系统编程小结(二)------文件和目录
一.对linux的安全机制的一点感悟
各种权限,read,write,execute,set-user-ID,set-group-ID,sticky bit,对目录的权限,对文件的权限,用于保证系统安全的各种组合技,各种经典。比如,如果我们想unlink一个文件,就必须拥有该文件所在目录的write与execute的权限。
二.两个小例子
1.当文件有hole时,cp命令会同时拷贝这些hole为'\0'。这里是一个实现了拷贝时跳过文件hole的程序。ps:我用的buffer是一个字节的,效率很低,但如果用大的buffer就会使得hole被移除,使得原先分开的字符被连上。我没想好如何解决这个问题。如果您知道,请您告诉小弟我,非常感谢!
?ln -s ../foo link_file 这里的../foo可以是不存在的文件,然后就像绝对路径一样,link_file 直接指向../foo。例子:foo是一个目录。ln -s ../foo foo/testdir再ll foo会显示drwxr-xr-x 2 root root 4096 2012-12-06 22:37 ./drwxr-xr-x 3 root root 4096 2012-12-06 22:37 ../lrwxrwxrwx 1 root root 6 2012-12-06 22:37 testdir -> ../foo/17.All the information in the i-node is stored separately from the actual contents of the file.
18.A directory is simply a file containing directory entries:filenames and associated i-node numbers.
19.
?typedef的一个用法:typedefintFunc(inta,intb);//这声明了一个新的函数类型,Func,这种函数类型有两个int类型的参数,返回值也是int类型staticFunc my_func1;//my_func1的函数原型,完整的写法是static int my_func1(int a ,int b); 在这里使用typedef,会使得代码更加简洁,清晰,对代码的可读性有好处20.
?char*p; *p++='0'; 是将*p赋值为'0'后,p指针再加1个单位的地址*++p='0'; 是先将p执行加一个单位的地址后,再将*p赋值为'0'; 这是一个比较容易混淆和容易记错的地方21.char *ptr; ptr[-1]是指是的ptr指向位置的上一个元素。
22.The current working directory is an attribute of a process .
23. If we try,using either open or creat,to create a file that alredy exists,the file's access permission bits are not changed but the files were truncated(被清空).
24.The size of a directory should never be 0,since there should always be entries for dot and dot-dot.The size of a symbolic link is the number of characters in the pathname contained in the symbolic link,and this pathname must always contain at least one character.
25.time ./pro1 计算整个pro1程序执行的时间
26.The kernel has no inherent limit on the depth of a directory tree.But many commands will fail on pathnames that exceed PATH_MAX,比如getcwd.
参考资料:apue
如果您觉得我的文章对您有帮助,请顶一下,非常感谢!