By looking at the Greg's strace, it gives me great insight of the command.
So, It looks like it calls a library which can be in glib(?), and the library actually calls kernel device drivers like console or something like that, and they were communicating each other back and forth, and finally, it outputs the result of "ls" onto the screen. Is my understanding correct?
Best Regards,
Daniel (Youngwhan) Song
On Wed, Dec 2, 2009 at 7:41 PM, Greg Freemyer <greg.freemyer@xxxxxxxxx> wrote:
read the source of course, but a very handy shortcut is:On Wed, Dec 2, 2009 at 9:12 PM, Daniel (Youngwhan) Song
<breadncup@xxxxxxxxx> wrote:
> Hi,
>
> Could somebody explain me how exactly "ls" command work in Unix/Linux?
>
> When we type "ls" command in linux shell, what does process/procedure work
> with linux library or linux kernel, and how exactly does it show directory
> information to the standard output (1)?
>
> Thanks in advance.
>
> Best Regards,
> Daniel Song
>
# strace ls
The key part with a couple comments is:
# Get the directory entries
open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
fcntl64(3, F_GETFD) = 0x1 (flags FD_CLOEXEC)
getdents64(3, /* 53 entries */, 32768) = 1744
getdents64(3, /* 0 entries */, 32768) = 0
close(3) = 0
# Verify stdout is a character device
fstat64(1, {st_mode=S_IFCHR|0600, st_rdev=makedev(136, 0), ...}) = 0
# map stdin into memory. (Not sure why, see the source)
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb73ff000
# write the directory entries to stdout and wrap-up
write(1, "bin Desktop Documents Downloa"..., 91bin Desktop
Documents Download Music Pictures Public public_html Templates
Videos
) = 91
close(1) = 0
munmap(0xb73ff000, 4096) = 0
close(2) = 0
exit_group(0) = ?
Greg