LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (http://www.linuxquestions.org/questions/slackware-14/)
-   -   Where is the source for commands like 'cat' and 'tee' stored? (http://www.linuxquestions.org/questions/slackware-14/where-is-the-source-for-commands-like-cat-and-tee-stored-4175441230/)

WhiteHotLoveTiger 12-12-2012 08:36 PM

Where is the source for commands like 'cat' and 'tee' stored?
 
In an effort to better understand my computer, I've started to poke around at things that I previously wasn't brave enough to look at, like the files in /etc/rc.d. I was really surprised that some of these files aren't all that complex or scary. I felt like I could understand most of what was going on. I've also started looking at the source for some of the programs that I use. However, I can't seem to find everything I'm looking for. I've looked through the different Slackware source directories, and searched a little through the kernel source, but I can't find the command "tee", or "cat". Are these part of larger packages with a different name, or am I just not looking hard enough?

T3slider 12-12-2012 08:43 PM

`cat` and `tee` are part of the coreutils package. The following command would tell you that:
Code:

grep -E "/(cat|tee)$" /var/log/packages/*

josiah 12-12-2012 11:03 PM

Incidentally, the man or info page for either of them would make it clear that they're part of the coreutils package.

Mark Pettit 12-13-2012 02:03 AM

To be honest, I'm not sure that you will learn a huge amount from the source code of those base utilities. They're all written in "c" - a very powerful language. But fraught with the most awful dangers. Unless you have a huge need to suddenly start coding in "c", your efforts will be better rewarded in learning something like Python. Both of those programs (cat/tee) could be rewritten in Python in less than 100 lines, considerably less than the original "c" sources.

zakame 12-13-2012 02:22 AM

Quote:

Originally Posted by Mark Pettit (Post 4848260)
To be honest, I'm not sure that you will learn a huge amount from the source code of those base utilities. They're all written in "c" - a very powerful language. But fraught with the most awful dangers. Unless you have a huge need to suddenly start coding in "c", your efforts will be better rewarded in learning something like Python. Both of those programs (cat/tee) could be rewritten in Python in less than 100 lines, considerably less than the original "c" sources.

I'll agree to that, but for a different reason: GNU coreutils isn't exactly a good example of accessible C code. Take this gist comparing different implementations of echo(1), for example.

As for a Python reimplementation, I'd say Perl went ahead with that years ago.

Mark Pettit 12-13-2012 04:22 AM

I'll resist the temptation, great that it is, to turn this into a Python vs Perl thread :-)

Didier Spaier 12-13-2012 05:23 AM

Or the usual replacement for cat:
Code:

awk '{print}' <file>

273 12-13-2012 05:55 AM

Quote:

Originally Posted by Mark Pettit (Post 4848260)
Both of those programs (cat/tee) could be rewritten in Python in less than 100 lines, considerably less than the original "c" sources.

Isn't that a little misleading? I mean, I could write a program to "cat" in one word in BASH "cat". There still needs to be an underlying mechanism to do the job.

jpollard 12-13-2012 06:27 AM

Also note - there are things "cat" can do that python isn't guaranteed to do.

You CAN use cat to copy files... unless those files are sparse.
You CAN use bash implement a cat... unless you need to ensure that the files are not altered (by line termination problems at a minimum)
You CAN implement cat in Python (or perl)... but it will run slower.

I've even seen ls implemented in "sh"... but it can't handle large directories...

And the last time I looked (many years ago now) cat only took about 100 lines of C (about half was the help text built in). Of course there is the runtime C library, but you have to use that even if you are using Perl or Python.

Just because something CAN be done, doesn't mean it should be done, especially in production.

Experimentation though - it's good for you. And you will learn a lot.

Mark Pettit 12-13-2012 06:56 AM

Quote:

Originally Posted by 273 (Post 4848394)
Isn't that a little misleading? I mean, I could write a program to "cat" in one word in BASH "cat". There still needs to be an underlying mechanism to do the job.

Well - not really. Remember Python has an excellent library system, much of which is a simple direct interface to the standard C library. But my point here was not to promote Python itself, but more to tell the OP that studying this particular codebase was not a good idea. To be honest, if we're making an academic exercise of learning code, then really the holy grail should be LISP. After 30 years of professional programming, I still aspire to try to learn LISP. I've programmed professionally in (mainframe) Assembler, Cobol (yugh) and "c" (10 years of that), and now for the last 10 years, in Python. And the more I program in Python, the less desire I have to program in anything else !

BTW - one of the underlying modules in Python that would do a cat/tee like implementation would be "shutils".

WhiteHotLoveTiger 12-13-2012 01:22 PM

Thanks for all the responses. I appreciate hearing the different viewpoints and insights.

This is getting a off topic from my original question, but if I might respond to some of the comments about which languages to spend time learning more about:
I have a basic familiarity with both C and Python. I'd love to learn LISP. Everything I've read about it makes it sound very interesting. From a practical standpoint though, I feel I should focus on skills that can help me gain employment. Whenever I check job postings around the area where I live, there's tons of positions for Visual Basic and ASP.net positions, a few for java, C++, SQL, and javascript positions, the odd one for C developers, and anything else is pretty rare (this is a little subjective, as I don't tally the results or anything).
I worked for a couple years in a .Net shop. It paid the bills well enough, but I really longed to be using something else. Right now I've gone back to school to finish off my Bsc, and pretty well everything is java. I'm fine with java, but I really enjoy the odd class where we have to use C.
I know that I really enjoy using C. I know that I hate using Visual Basic. Even though it would be much easier for me to find work as a VB programmer, I know that I'd be much happier working as a C programmer.
So, I think it makes sense for me to spend reading though C code bases and practising my C coding skills, but I guess it would be more practical to boot up Windows, install Visual Studio and practice the other stuff...


All times are GMT -5. The time now is 02:33 AM.