Beruflich Dokumente
Kultur Dokumente
I_amok
2009-04-13
然后就定义的行号的头尾的指针和具体输出行号的时候的打印指针,这个比较有趣,第一次看到这样的计
数器。
size_ t n = n_read;
if (full_write (STDOUT_FILENO, buf, n) != n)
size_ t
full_rw (int fd, const void *buf, size_ t count)
{
size_ t total = 0;
const char *ptr = (const char *) buf;
return total;
}
整个 simple_cat 自定义函数的用意就是一边读,一边写。
cat 函数几个判断的地方是
为了理解
if (show_nonprinting)
后面的代码,好绕,我提取出来单独测试了一下
提取的代码如下:
#include <stdio.h>
也就是执行
*bpout++ = 'M';
*bpout++ = '-';
if (show_nonprinting)中只有遇到了换行符,才会跳出第二个 for 循环,然后进入到第一个 for 循环,进
行判断。
进入到主函数部分了
取文件属性
/usr/include/sys/stat.h 定义。
------------------------------------------
src/system.h 定义
------------------------
S_ISREG (stat_buf.st_mode)
应该是用来判断文件是不是普通文件的。
----------------------
isatty
在 /usr/include/unistd.h 中定义
--------------------------
freopen (NULL, "wb", stdout);
/usr/include/stdio.h 中定义
freopen
FILE *freopen(const char *restrict filename, const char *restrict mode,
FILE *stream);The function closes the file associated with the stream stream (as if by
calling fclose); then it opens the file with the filename filename and associates the file with
the stream stream (as if by calling fopen(filename, mode)). It returns stream if the open is
successful; otherwise, it returns a null pointer.
void *
xmalloc (size_ t n)
{
void *p = malloc (n);
if (!p && n != 0)
xalloc_die ();
return p;
}
是个 inline function
在 src/system.h 有定义
/* Return PTR, aligned upward to the next multiple of ALIGNMENT.
ALIGNMENT must be nonzero. The caller must arrange for ((char *)
PTR) through ((char *) PTR + ALIGNMENT - 1) to be addressable
locations. */
但是我有个不解的地方,他为啥不在分配内存的时候就对齐呢?还要去对齐指针,上面两个连接都是说在
内存分配的时候对齐的。
这次分析写的比较粗,但是实际上我是看完一行行的代码的。这里只是说说我觉得比较难懂的几个部分,
这个帖子 https://bbs.be10.com/vbb3.0.1/showthread.php?t=3468
是我的读书笔记,大家可以跟踪。