tags:

views:

898

answers:

8

Say I'm interested in the source for one particular Linux utility, like factor. Where can I find the source code for that utility?

+4  A: 

Usually 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.

joschi
Thanks for supplying the link.
Bill the Lizard
+7  A: 

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.

yjerem
Thanks, this (a general explanation of how to find the source for a given program) is what I was after.
Bill the Lizard
That may not be a good idea for all things. Some distributions may have modified the original for their distribution. It may be a better idea to use the package management software that comes with you system (Package management can get source as well as binaries) See 'Saua' below.
Martin York
+12  A: 

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.

Joachim Sauer
Thanks, that's a good, non-Google option. I know Google will always be there, but for some reason I still like knowing how to live without it.
Bill the Lizard
I find it convenient because I don't even have to start my browser. It just gets whatever I want.
Joachim Sauer
+3  A: 

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 .

Kent Fredric
+3  A: 

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.

Blaisorblade
+2  A: 

I usually find a link to the source, homepage, and other useful information by doing a search on freshmeat.

vinc456
+1  A: 

You may want to check out SourceForge.net.

+2  A: 

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.

A. Rex
Nice. The thing I really like about this is that any #included files appear as links in the source, making it very easy to navigate through complicated source code.
Bill the Lizard