我如何获得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.*/

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....