كيف يمكنني الحصول على الكود المصدري لأداة Linux المساعدة؟

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

  •  10-07-2019
  •  | 
  •  

سؤال

هذا الأمر مفيد جدًا حقًا ولكن حيث يمكنني الحصول على الكود المصدري لمعرفة ما يجري بالداخل.

شكرًا .

هل كانت مفيدة؟

المحلول

الأداة المساعدة التيل هي جزء من الأدوات الأساسية في نظام التشغيل Linux.

لقد وجدت دائمًا أن FreeBSD يحتوي على كود مصدر أكثر وضوحًا من أدوات gnu المساعدة.إذن، هذا هو tail.c في مشروع FreeBSD:

نصائح أخرى

وكزة حول الموقع uclinux. منذ قاموا بتوزيع البرنامج، كانت مطلوبة لجعل مصدر المتاحة بطريقة أو بأخرى.

وأو، هل يمكن قراءة man fseek وتخمين في الكيفية التي يمكن أن يتم ذلك.

وتعليقات NB-- انظر وليام أدناه، هناك حالات عندما لا يمكنك استخدام نسعى إليه.

وقد تجد أنه من ممارسة مثيرة للاهتمام لكتابة بنفسك. الغالبية العظمى من أدوات سطر الأوامر يونكس هي صفحة أو نحو ذلك من اضحة إلى حد ما كود C.

لمجرد إلقاء نظرة على الرمز، وجدت مصادر GNU CoreUtils بسهولة على gnu.org أو المفضلة لديك موقع المرآة لينكس.

/`*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