How to Get the Source Code for the Linux Utility Tail

How can I get the source code for the linux utility tail?

The tail utility is part of the coreutils on linux.

  • Source tarball: ftp://ftp.gnu.org/gnu/coreutils/coreutils-7.4.tar.gz
  • Source file: http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c

I've always found FreeBSD to have far clearer source code than the gnu utilities. So here's tail.c in the FreeBSD project:

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

How do I read the source code of shell commands?

All these basic commands are part of the coreutils package.

You can find all information you need here:

http://www.gnu.org/software/coreutils/

If you want to download the latest source, you should use git:

git clone git://git.sv.gnu.org/coreutils

To install git on your Ubuntu machine, you should use apt-get (git is not included in the standard Ubuntu installation):

sudo apt-get install git

Truth to be told, here you can find specific source for the ls command:

http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/ls.c

Only 4984 code lines for a command 'easy enough' as ls... are you still interested in reading it?? Good luck! :D

How would you implement tail efficiently?

I don't think there are solutions different than "keep the latest N lines while reading forward the data" or "start from the end and go backwards until you read the Nth line".

The point is that you'd use one or the another based on the context.

The "go to the end and go backwards" is better when tail accesses a random access file, or when the data is small enough to be put on memory.
In this case the runtime is minimized, since you scan the data that has to be outputted (so, it's "optimal")

Your solution (keep the N latest lines) is better when tail is fed with a pipeline or when the data is huge.
In this case, the other solution wastes too much memory, so it is not practical and, in the case the source is slower than tail (which is probable) scanning all the file doesn't matter that much.

How to write a tail -f like C program

There is no standardized mechanism for monitoring changes to a file, so you'll need to implement a "polling" solution anyway (that is, when you hit the end of file, wait a short amount of time and try again.)

On Linux, you can use the inotify family of system calls, but be aware that it won't always work. It doesn't work for special files or remote filesystems, for example, and it may not work for some local filesystems. It is complicated in the case of symlinks. And so on. There is a Windows equivalent, but I believe it suffers from some of the same issues.

So even if you use a notification system, you'll need the polling solution as a backup, and since OS notifications are not guaranteed to be reliable (that is, if the system is under load, notifications might be dropped), you'll need to poll on timeout even if you are using a notification system.

You might want to take a look at the implementation of the GNU tail utility (http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/tail.c) to see how the special cases are handled.

How can I tail a log file in Python?

So, this is coming quite late, but I ran into the same problem again, and there's a much better solution now. Just use pygtail:

Pygtail reads log file lines that have not been read. It will even
handle log files that have been rotated. Based on logcheck's logtail2
(http://logcheck.org)

Java IO implementation of unix/linux tail -f

The ability to continue to read a file, and wait around until the file has some more updates for you shouldn't be that hard to accomplish in code yourself. Here's some pseudo-code:

BufferedReader br = new BufferedReader(...);
String line;
while (keepReading) {
line = reader.readLine();
if (line == null) {
//wait until there is more of the file for us to read
Thread.sleep(1000);
}
else {
//do something interesting with the line
}
}

I would assume that you would want to put this type of functionality in its own Thread, so that you can sleep it and not affect any other areas of your application. You would want to expose keepReading in a setter so that your main class / other parts of the application can safely shut the thread down without any other headaches, simply by calling stopReading() or something similar.



Related Topics



Leave a reply



Submit