Linuxユーティリティテールのソースコードを取得するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1439799

  •  10-07-2019
  •  | 
  •  

質問

このコマンドは非常に便利ですが、ソースコードを取得して、内部で何が起こっているかを確認できます。

ありがとう。

役に立ちましたか?

解決

tailユーティリティは、Linuxのcoreutilsの一部です。

FreeBSDは、gnuユーティリティよりもはるかに明確なソースコードを持っていることが常にあります。 FreeBSDプロジェクトのtail.cは次のとおりです。

他のヒント

uclinuxサイトを調べてみてください。彼らはソフトウェアを配布したので、ソースを何らかの方法で利用可能にする必要があります。

または、 man fseek を読んで、それがどのように行われるかを推測できます。

NB--以下のウィリアムのコメントを参照してください。シークを使用できない場合があります。

自分で書くのは面白い練習になるかもしれません。 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: %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.*/
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top