Исходные коды утилит linux

View source for standard Linux commands e.g. cat, ls, cd

I would like to view the source code for a Linux command to see what is actually going on inside each command. When I attempt to open the commands in /bin in a text/hex editor, I get a bunch of garbage. What is the proper way to view the source on these commands? Thanks in advance, Geoff EDIT: I should have been more specific. Basically I have a command set that was written by someone who I can no longer reach. I would like to see what his command was actually doing, but without a way to ‘disassemble’ the command, I am dead in the water. I was hoping for a way to do this within the OS.

5 Answers 5

Many of the core Linux commands are part of the GNU core utils. The source can be found online here

The file you are opening is the binary executables which are the stuff the kernel passes to the CPU. These files are made using a compiler that takes in the source code you and I understand and turns it via a number of stages into this CPU friendly format.

Hi Andy, I should have been more specific. Basically I have a command set that was written by someone who I can no longer reach. I would like to see what his command was actually doing, but without a way to ‘disassemble’ the command, I am dead in the water. I was hoping for a way to do this within the OS.

that’s much harder. Native code is hard to dissemble into c or c++ source code. You can turn the binary into assembler using something like ‘objdump -d mycommand’. Then you’ll need to understand assembler to understand what it’s doing.

Читайте также:  Arch linux install boot

Updated my comment above. Not really is the answer, not to the c code, if you knew the compiler (specific version), the compiler options and how it was doing to conversion it is theoretically possible (although there still might be ambiguity in the reversal).

That worked and now I have the assembly, now I just need to get the assembly code disassembled into C. Thanks for getting me this far. If you have any suggestions for getting the assembly into C code, please let me know!

Источник

Where can I find system call source code?

In Linux where can I find the source code for all system calls given that I have the source tree? Also if I were to want to look up the source code and assembly for a particular system call is there something that I can type in terminal like my_system_call ?

3 Answers 3

You’ll need the Linux kernel sources in order to see the actual source of the system calls. Manual pages, if installed on your local system, only contain the documentation of the calls and not their source itself.

Unfortunately for you, system calls aren’t stored in just one particular location in the whole kernel tree. This is because various system calls can refer to different parts of the system (process management, filesystem management, etc.) and therefore it would be infeasible to store them apart from the part of the tree related to that particular part of the system.

The best thing you can do is look for the SYSCALL_DEFINE4 macro. It is used (obviously) to define the given block of code as a system call. For example, fs/ioctl.c has the following code :

SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg) < /* do freaky ioctl stuff */ >

Such a definition means that the ioctl syscall is declared and takes three arguments. The number next to the SYSCALL_DEFINE means the number of arguments. For example, in the case of getpid(void) , declared in kernel/timer.c , we have the following code :

Читайте также:  Graphical user interfaces in linux

Hope that clears things up a little.

Источник

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

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

4 Answers 4

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:

The BSD version is shorter because the functions that do all the work aren’t in that file. i.e.: follow().

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.

Hmm, never thought of that. Thanks. I have to think that it would still be faster when working with seekable input.

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.

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.

The core of most of the tools generally is short. But the argument processing and corner case handling can be a pain.

Читайте также:  Linux set users home directory

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.

/`*This example implements the option n of tail command.*/` #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #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.*/ 

Источник

Оцените статью
Adblock
detector