Say I'm interested in the source for one particular Linux utility, like factor
. Where can I find the source code for that utility?
views:
898answers:
8Usually you'll find the source code on the website of the program if it is open source. In this case here since factor is part of coreutils.
What I did was type
man factor
and went to the bottom and found 'GNU coreutils 6.10'. So I googled 'coreutils' and... found the site joschi just linked to.
You can also find out which package the binary comes from an download that packages source code.
On Debian (and Ubuntu and anything else that's based on Debian) you do that like this:
$ dpkg -S /usb/bin/factor coreutils: /usr/bin/factor $ apt-get source coreutils
The first command will check which package contains the file you are searching for (use "which factor
" to find out which binary is executed when you just type "factor
").
The second command will download and unpack the sources (including the patches applied to build the package) to the current directory, so it should be executed in a dedicated or temporary directory.
I'm pretty sure rpm
-based distributions have a similar mechanism, but I don't know their commands.
On Gentoo, simply look in the ebuild you compiled the package with :D.
If you're not sure?
# which factor /usr/bin/factor # grep '/usr/bin/factor' /var/db/pkg/*/*/CONTENTS /var/db/pkg/sys-apps/coreutils-6.12-r2/CONTENTS:obj /usr/bin/factor 5aaf903daa4345efb11618b3cb47e9a5 1224224574 /var/db/pkg/sys-apps/coreutils-6.12-r2/CONTENTS:obj /usr/lib64/debug/usr/bin/factor.debug 517d965636850633e9b15926dde8c222 1224224575 # cat /var/db/pkg/sys-apps/coreutils-6.12-r2/SRC_URI ftp://alpha.gnu.org/gnu/coreutils/coreutils-6.12.tar.lzma mirror://gnu/coreutils/coreutils-6.12.tar.lzma mirror://gentoo/coreutils-6.12.tar.lzma mirror://gentoo/coreutils-6.12-patches-1.0.tar.lzma http://dev.gentoo.org/~vapier/dist/coreutils-6.12-patches-1.0.tar.lzma # cat /var/db/pkg/sys-apps/coreutils-6.12-r2/HOMEPAGE http://www.gnu.org/software/coreutils/
But of course, the source code is probably still available in /usr/portage/distfiles
.
To find the package a binary comes from, in rpm based system, you might type:
$ rpm -qf /usr/bin/factor
which will print the package name. Instead, with:
$ rpm -qif /usr/bin/factor
you will get also information on a package, including it's home page in many cases.
Source rpms also exist, but how to get them depends on the high-level package manager used on top of RPM (yum, urpmi, apt-get4 for rpm, ...).
On most systems, also, /usr/share/doc/ contains some documentation on the program, and the website link is pretty often found somewhere there, maybe in the README.
Another, very good approach is to use Google Code Search. For example, a search for factor coreutils (see the man page or factor --help
to see that it's from coreutils) came up with the package as the second result. Two clicks away I was browsing factor.c online.
Google Code Search searches most public source code. You can use regexps and many advanced search options, including restricting by language and license.