Viewing Unix cmd source code

plastick

Golden Member
Sep 29, 2003
1,400
1
81
Is there a way I can look and edit the source code of a specific unix command such as sort or grep?
 

joinT

Lifer
Jan 19, 2001
11,172
0
0
Wasn't trying to be mean or anything.. done the same thing plenty of times myself..
 

plastick

Golden Member
Sep 29, 2003
1,400
1
81
*chuckles*

Well, how do I view source code? Get it off the introweb?
 

DAM

Diamond Member
Jan 10, 2000
6,102
1
76
you will have to look for the source, either installed in your compute or the million reposotories on the web.
 

plastick

Golden Member
Sep 29, 2003
1,400
1
81
Originally posted by: DAM
you will have to look for the source, either installed in your compute or the million reposotories on the web.

I Dont even know where to start. I dont know anything about programming, so I couldnt do anything if I found it.
Is C++ a hard lang to lear?
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
Originally posted by: plastick
Originally posted by: DAM
you will have to look for the source, either installed in your compute or the million reposotories on the web.

I Dont even know where to start. I dont know anything about programming, so I couldnt do anything if I found it.
Is C++ a hard lang to lear?

Most commands are written in C. Some are scripts that point to other commands for simplicity sake. And C doesn't equal C++. They are similar but are different enough.

It depends on what you actually want, source code is provided by every distributer of Linux. It's part of the requirement for redistributing programs under the GPL liscence.

For instance for Redhat along with the ISO's you downloaded they have a optional "source" cdrom. You can find the same sources used specificly by your distro from these. They also have FTP sites were you can find the exact source code that is used by your program you want.

If you install programs from 3rd parties you can look for the source code from their homepages or the same place you found them.

Like I said in order to redistribute Linux programs you have to allow access to the source code. 95% of the time it will be found in a similar place to were you orginially downloaded the program. FTP sites are usually used because it's conveinent, but they can do stuff like use cdroms and charge you for their cost of the cdrom (strickly non-profit) if that's how they want to do it, but that's rare.

For most standard tools and apps they originate from the GNU project. They were the created for the purpose of designing a Freedom-based operating system. When Linus created Linux kernel they used apps from GNU since GNU never got around to designing their own kernel. So the technicacly correct way to refer to linux as a OS is to call it the GNU/Linux OS.

The GNU project. If you look on the left hand column they have links to a multitude of open source programs and their sources.
 

joinT

Lifer
Jan 19, 2001
11,172
0
0
eg.
Azureus - a bittorrent client.

Sourceforge Homepage

You can download either;
a: the azureus package or
b: the source releases

the source releases will have the source code to be compiled as opposed to a pre-compiled tar package
(do I have that right? drag? still learning Linux myself)
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
the source releases will have the source code to be compiled as opposed to a pre-compiled tar package
(do I have that right? drag? still learning Linux myself)

Anything that says it's source or source code along those lines will contain the actual C language (or otherwise) used to create the program.

In most source releases it will contain a directory were the source code is kept, a Make file and some other scripts and settings and stuff like configure script and some other assorted files.

The MAKE file contains the directions needed to compile programs, there are some tools other then the gcc compiler itself that help out, like the "make" command.

Generally in order to compile programs you do this command while in the home directory:

./configure

This will scan your system and look for dependances. It may need to have the GTK+ library present for instance and will look for it. Also it looks for different setting and dependances for different features. For instance if your compiling a application for playing music, it make look for alsa support or check for OSS support, or maybe the GUI part of the program can be compiled using QT or GTK+ or GTK+ 2.0.

Most programs are compiled using libraries. Libraries are common code used in many programs. I linux they are generally located in lib directories, and in windows they are *.dll files.

So if you have the libraries needed for ALSA it may compile using ALSA, if you lack the libraries for ALSA it may not end up supporting some features, or it may fail and complain. It all depends on the program.

The configure script checks for all this and sets up the MAKE file which governs what is actually going to be compiled.

Very simple programs will often lack a configure script, and just have a make file. Sometimes you even have to manually edit the make file to configure it manually for your system, but this is rare.

Then you run the make command like such:

make

That looks for the MAKE file and then it knows what to do to set up and compile the binary versions of the program and any new libraries that is created along with the program.

Then you do a:

make install

And that goes to the make file and finds out what files to copy were to install your program. Usually you have to be root.

Sometimes you have "make test" which will run some tests to check out and make sure that everything is working before you install the program.

So the generic thing to do is:

./configure
make
make install

And that will install the program from source code.

But there are lots of variations and there are NO rules on howto do that. Most authors will include a INSTALL and/or Readme file. Always read those to find out exactly what to do. Sometimes their are extra steps you have to do, or even something completely different.

For instance I installed a simple program called ratmen a while ago for my laptop. I untarred the source and just ran "make", then simply copied the resulting binary to my ~/bin directory.

If you want to look at the source code you generally look for *.c files for C programs.

The simplist way to compile C programs is to just use the gcc.
#include <stdio.h>

main()
{
printf ("Hello Anandtech!\n");
}

Put that in a file called test.c then do this:

gcc test.c

then it will create a file named a.out, run this command:

./a.out

That "#include <stdio.h>" is pulling code from the standard c libraries and is what enables me to use the printf command in this simple program.


BTW. If you want to learn the basics of programming check out Python. It's a nice interpreted langauge (you don't have to compile it to use it) and has all the features and capabilities of any modern langauge.

It's kinda of like qbasic equivilant for linux, except that it doesn't suck and you NEVER USE GOTO's if you can help it. :p

It's used from scripting websites to making GUI applications. Mostly it makes a good mathmatical programming langauge for doing large calculations quickly (C and even java are faster then python, but not when you include the time to write the program in the first place.).

One famous thing is that it is used to create the portage system for Gentoo.