新建文件tailf.c文件

文件内容如下:

/* tailf.c -- tail a log file and then follow it

* Created: Tue Jan 9 15:49:21 1996 by faith@acm.org

* Copyright 1996, 2003 Rickard E. Faith (faith@acm.org)

*

* Permission is hereby granted, free of charge, to any person obtaining a

* copy of this software and associated documentation files (the "Software"),

* to deal in the Software without restriction, including without limitation

* the rights to use, copy, modify, merge, publish, distribute, sublicense,

* and/or sell copies of the Software, and to permit persons to whom the

* Software is furnished to do so, subject to the following conditions:

*

* The above copyright notice and this permission notice shall be included

* in all copies or substantial portions of the Software.

*

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

* OTHER DEALINGS IN THE SOFTWARE.

*

* less -F and tail -f cause a disk access every five seconds. This

* program avoids this problem by waiting for the file size to change.

* Hence, the file is not accessed, and the access time does not need to be

* flushed back to disk. This is sort of a "stealth" tail.

*/

#include

#include

#include

#include

#include

//#include "nls.h"

#define _(s) s

static size_t filesize(const char *filename)

{

struct stat sb;

if (!stat(filename, &sb)) return sb.st_size;

return 0;

}

static void tailf(const char *filename, int lines)

{

char **buffer;

int head = 0;

int tail = 0;

FILE *str;

int i;

if (!(str = fopen(filename, "r"))) {

fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);

perror("");

exit(1);

}

buffer = malloc(lines * sizeof(*buffer));

for (i = 0; i < lines; i++) buffer[i] = malloc(BUFSIZ + 1);

while (fgets(buffer[tail], BUFSIZ, str)) {

if (++tail >= lines) {

tail = 0;

head = 1;

}

}

if (head) {

for (i = tail; i < lines; i++) fputs(buffer[i], stdout);

for (i = 0; i < tail; i++) fputs(buffer[i], stdout);

} else {

for (i = head; i < tail; i++) fputs(buffer[i], stdout);

}

fflush(stdout);

for (i = 0; i < lines; i++) free(buffer[i]);

free(buffer);

}

int main(int argc, char **argv)

{

char buffer[BUFSIZ];

size_t osize, nsize;

FILE *str;

const char *filename;

int count;

//setlocale(LC_ALL, "");

//bindtextdomain(PACKAGE, LOCALEDIR);

//textdomain(PACKAGE);

if (argc != 2) {

fprintf(stderr, _("Usage: tailf logfile\n"));

exit(1);

}

filename = argv[1];

tailf(filename, 10);

for (osize = filesize(filename);;) {

nsize = filesize(filename);

if (nsize != osize) {

if (!(str = fopen(filename, "r"))) {

fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);

perror(argv[0]);

exit(1);

}

if (!fseek(str, osize, SEEK_SET))

while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0)

fwrite(buffer, 1, count, stdout);

fflush(stdout);

fclose(str);

osize = nsize;

}

usleep(250000); /* 250mS */

}

return 0;

}

linux不支持tailf指令,Linux安装tailf相关推荐

  1. linux系统找运行指令,Linux系统常用指令总结

    一,系统的运行级别 0:关机 1:单用户模式(可以找回丢失的密码) 2:多用户状态没有网络服务 3:多用户状态有网络服务 4:系统未使用保留给用户 5:图形界面 6:系统重启 注意:在/etc/ini ...

  2. Linux无法运行cal指令,Linux cal 命令 command not found cal 命令详解 cal 命令未找到 cal 命令安装 - CommandNotFound ⚡️ 坑否...

    显示行号 | 选择喜欢的代码风格 默认 GitHub Dune LakeSide Plateau Vibrant Blue Eighties Tranquil cal 命令显示当前日历或指定日期的日历 ...

  3. windows命令行下访问linux,Windows支持直接访问Linux子系统文件:你的下一台Linux何必是Linux...

    原标题:Windows支持直接访问Linux子系统文件:你的下一台Linux何必是Linux 晓查 发自 凹非寺 量子位 报道 | 公众号 QbitAI 微软,致力于做最好的Linux发行版. 今天, ...

  4. windows兼容Linux php,支持windows与linux的php计划任务的实现方法

    本文实例讲述了支持windows与linux的php计划任务的实现方法.包括了在winows下利用winodows计划任务来操作,还有在linux中利用linux的方法来实现.分享给大家供大家参考.具 ...

  5. linux先运行后面指令,Linux基础命令(6)

    三十八 cat命令简介 cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. 1.命令格式: c ...

  6. linux系统支持ntfs吗,linux支持NTFS

    我的linux系统是redhat的enterprise5,缺省的情况下是不支持NTFS系统的.需要安装相应的模块.我的安装步骤是: 1,到href="" target=_blank ...

  7. linux系统支持ntfs吗,Linux支持exFAT和NTFS

    Linux系统默认可以自动识别到fat32格式的盘,但fat32支持的文件不能大于4G,所以只能将移动硬盘和U盘格式化为NTFS和exFAT这两种格式的,对于U盘最好格式化为exFAT,NTFS对U盘 ...

  8. Linux系统xshell常用指令,linux系统安装数据库时,xshell工具常用MySQL(5.5)相关命令...

    1.进入mysql数据库 [root@centos6 /]# mysql -u root -p Enter password: root Welcome to the MySQL monitor.   ...

  9. linux查看服务器温度指令,linux查看服务器温度

    有没有有线网卡,或者串口######你想做个类似于小型机器人这种的?那你可以在它的上面布署一个小的web应用么?######@氪金. @杨同学,这个上面我不想做任何接口,包括网卡和串口,所以所有通迅都 ...

  10. linux定时删除文件指令,Linux实践——定时删除目录下面的文件

    既然是定时删除目录下面的文件,那就需要我们借助linux的crontab命令.并且对于每个工作例程的内容格式我们在crontab 每一条工作的格式 中也有介绍. 同样我们需要将删除文件的功能用shel ...

最新文章

  1. 小程序海外也能做吗?小程序海外主体公司开发的案例
  2. HTML5在豆瓣的各种应用
  3. linux子系统gdp调试,Linux系统中GDB功能汇总
  4. NYOJ 643 发短信
  5. TApplication与主消息循环
  6. 此版本的应用程序不支持其项目类型 (.etp)_适用于Microsoft Dynamics 365商业中心的VPS和VJS版本1.8...
  7. Unity学习笔记:unity介绍(一)
  8. 【李宏毅机器学习】Recurrent Neural Network Part2 循环神经网络(p21) 学习笔记
  9. Windows 多个系统版本惊现大漏洞,攻击者可随意操作程序!
  10. android 文字垂直居中,前端常见问题——安卓文本无法垂直居中
  11. 移动app测试的多样性_移动App测试一些崩溃原因及触发崩溃的场景
  12. 无法支持python程序的编辑运行和调试_解决pycharm的Python console不能调试当前程序的问题...
  13. angularJS(二):作用域$scope、控制器、过滤器
  14. mysql连接 xorm_使用go xorm来操作mysql的方法实例
  15. 锐起无盘服务器内存盘,锐起及网众系列使用无盘+内存盘的新方法
  16. html中最小规格字体,font-size【CSS 字体大小】文字大小尺寸
  17. 使用PPT扣图,去掉背景
  18. 机器学习:金融领域的岗位需求
  19. 苹果XS怎么截屏_iPhoneXR 到底怎么样?真香 还是真烂?
  20. bitbake 编译错误集

热门文章

  1. matlab神经网络工具箱简介
  2. 【STM32F429的DSP教程】第1章 初学数字信号处理准备工作
  3. lsass.exe是什么?
  4. 没有基础一样可以一次性拿下CCSK认证?快来看这里!
  5. PS_01_基本操作
  6. pdca管理循环基本主张_PDCA循环在生产管理中的应用
  7. Cohen's d 效应量计算
  8. php对联广告代码,网站漂浮对联广告代码
  9. appfuse mysql_Appfuse 使用
  10. excel导入mysql教程视频_excel数据导入SQL数据库的教程