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

4

33

this command is really very useful but where I can get the source code to see what is going on inside .

thanks .

Oaten answered 17/9, 2009 at 16:6 Comment(0)
K
42

The tail utility is part of the coreutils on linux.

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

Kev answered 17/9, 2009 at 16:14 Comment(3)
+1 for diversity, even though the original question was about linux utility.Santiagosantillan
I like how the linux version is several times longer than the BSD version.Sterigma
The BSD version is shorter because the functions that do all the work aren't in that file. i.e.: follow().Chicane
A
1

Poke around the uclinux site. Since they distributed the software, they are required to make the source available one way or another.

Or, you could read man fseek and guess at how it might be done.

NB-- See William's comments below, there are cases when you can't use seek.

Albaalbacete answered 17/9, 2009 at 16:10 Comment(3)
tail does not use fseek. If it did, it would not work on a stream (eg 'grep pat file | tail')Hydrography
Hmm, never thought of that. Thanks. I have to think that it would still be faster when working with seekable input.Albaalbacete
This is not exactly true. tail doesn't always use seek ;-)Santiagosantillan
R
0

You might find it an interesting exercise to write your own. The vast majority of the Unix command-line tools are a page or so of fairly straightforward C code.

To just look at the code, the GNU CoreUtils sources are easily found on gnu.org or your favorite Linux mirror site.

Rhpositive answered 17/9, 2009 at 16:14 Comment(3)
I wrote a fairly complete set of tools for MS-DOS back when Linux was in its infancy. And while many are certainly straight forward, I would hesitate to say "a page' and wouldn't say a "vast majority". find and ls for example, were considerably more complex.Pavlish
The core of most of the tools generally is short. But the argument processing and corner case handling can be a pain.Albaalbacete
Yes, I probably should have said "many" tools are short & simple. GNU tail has a lot of options, so it probably doesn't fit into the "simple" category. The version of tail that most people would use (i.e. tail -xxx file, or tail -f file) would be pretty simple.Rhpositive
A
-2
/`*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: %s\n",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, buffer, nread);
      }
    }

    void walkFile(FILE *file, long nlines)
    {
      off_t fposition;
      fseek(file,0,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.*/
Arnhem answered 27/6, 2013 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.