微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何获得Linux实用程序尾部的源代码?

这个命令真的非常有用,但是我可以从里面得到源代码来看看发生了什么。

谢谢 。

观看文件并使用tail -f执行命令并读取?

如何写“命令-i interval | 尾巴“输出文件

Bash:如何尾巴然后复制多个文件(例如使用xargs)?

文件统计给定模式的行数

Java“tail -f”包装器

tail实用程序是linux上的coreutils的一部分。

代码压缩包: ftp : //ftp.gnu.org/gnu/coreutils/coreutils-7.4.tar.gz

文件: http : //git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c

我一直发现FreeBSD比gnu工具有更清晰的源代码。 所以这里是FreeBSD项目中的tail.c:

http://svnweb.freebsd.org/csrg/usr.bin/tail/tail.c?view=markup

打开uclinux站点。 由于他们分发软件,他们被要求以这种或那种方式来源。

或者,你可以读man fseek并猜测如何做。

注意 – 请参阅下面的William的评论,有些情况下你不能使用seek。

你可能会发现自己写一个有趣的练习。 绝大多数的Unix命令行工具都是一个相当简单的C代码页面

只看代码,GNU CoreUtils源代码很容易在gnu.org或你最喜欢的Linux镜像站点上找到。

/`*This example implements the option n of tail command.*/` #define _FILE_OFFSET_BITS 64 #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> #include <getopt.h> #define BUFF_SIZE 4096 FILE *openFile(const char *filePath) { FILE *file; file= fopen(filePath,"r"); if(file == NULL) { fprintf(stderr,"Error opening file: %sn",filePath); exit(errno); } return(file); } void printLine(FILE *file,off_t startline) { int fd; fd= fileno(file); int nread; char buffer[BUFF_SIZE]; lseek(fd,(startline + 1),SEEK_SET); while((nread= read(fd,buffer,BUFF_SIZE)) > 0) { write(STDOUT_FILENO,nread); } } void walkFile(FILE *file,long nlines) { off_t fposition; fseek(file,SEEK_END); fposition= ftell(file); off_t index= fposition; off_t end= fposition; long countlines= 0; char cbyte; for(index; index >= 0; index --) { cbyte= fgetc(file); if (cbyte == 'n' && (end - index) > 1) { countlines ++; if(countlines == nlines) { break; } } fposition--; fseek(file,fposition,SEEK_SET); } printLine(file,fposition); fclose(file); } int main(int argc,char *argv[]) { FILE *file; file= openFile(argv[2]); walkFile(file,atol(argv[1])); return 0; } /*Note: take in mind that i not wrote code to parse input options and arguments,neither code to check if the lines number argument is really a number.*/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐