Getting started with coding unix commands [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have been learning c and data structures for quite some time now and I wanted to see whether I could apply what I have learnt. I searched a bit and found out that I could start with util linux but, before I could do so, I thought I'd check and perhaps dabble a bit with the code for basic unix commands like "cat". I was able to understand what a part of the code might have been trying to do, but I was not able to understand the entire code as a unit.
For example, in the "cat" code, a pointer to the output buffer and input buffer is declared and is appropriately used, which I could understand. What i could not understand, are parts of code like io_blksize (stat_buf) which has no description whatsoever, on what it does. Or how two pointers declared as pointers to the input and output buffers, actually correspond to the input and output buffers ?
So my question being, how do I approach these type of code, how can I understand something that has no description to what it does (in the example given above) and how can I make and implement changes in the code, so that I can see the changes when i run a command ?
(Would really appreciate references or topics I should start with, so that I can relate what I have learnt to how command code's can be modified. I also apologize if the question is to abstract.)

This is a bit of a subjective question so my answers will just be my opinion of course.
A good place to start when you run into something you don't recognise while reading source code is the manpages. Each function will generally have a manpage, e.g. man 2 read or man 3 printf. Beyond that, I feel perhaps you should get more of a foundation in Unix before attempting to read the straight source code, a good book is Advanced Programming in the Unix Environment. I've been working through it myself and am finding my Unix knowledge improving considerably.
Just my two cents.

Related

Are Linux commands really C object files? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was reading "The C Programming Language" by Kernighan & Ritchie and came across some programs which mimic some Unix commands (also implemented in Linux) such as the cat command. The program took command line arguments just like the original cat command.
I am just curious to know whether they are the same thing or not.
Correct me if I'm wrong, any help would be appreciated.
In a command-line environment (such as, of course, Unix/Linux), a principle unit of abstraction is the command. A command has a well-defined interface: the command-line arguments it expects, the input it reads (if any), and the output it generates. You can reimplement a command any time you like, either using a different internal algorithm, or a different language, or just because you want to write your own version. Yes, cat was originally written in C, but we could rewrite it in C++, or Perl, or Python, or sh, or other languages. As long as our reimplementation meets the same interface requirements, we can accurately say that it "is" cat.

What do you mean by file handling? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What does a file handling mean? What is the difference between them in different languages?, for example perl or c. I'm unable to get a grasp on the concept.
I'm posting an example from perl suggested by #jeegar patel.
open(DATA, " <file.txt");
if you see in this piece of snippet, DATA acts as a file handle, so what exactly is the function/purpose of "DATA" here.
PS: I apologize if this is such a lame question, but I'm trying to get the basics right! Appreciate if anybody could acknowledge this.
In Any programming language, to perform any file operation in programming way it will have some own APIs.
Like C programming has.
http://www.w3schools.in/c/file-handling/
Like perl has
http://www.tutorialspoint.com/perl/perl_files.htm
What is the difference between them in different languages?
You can read file handlings APIs for different programing language and came to know what are different in them..
In fact your program does not get direct access to physical hardware and devices. Your program has to request these as services from the operating system. In each language there is a "run time model" of what a pralooks like, things like where the stack, heap and program memory are. The file system provided by the language is contains the routines needed to coordinate with the operating system, convert data types to the format required by your program and ways to find out the status of the file or even where to put things so the OS will actually write them out
This is complex stuff and each language and language design team has approached the problems somewhat differently.
Trust me they used to be a lot less standardized...
There are many functions on various level of file system hierarchy in the word "File handling".
An example of file system hierarchy and functions in each level:
Handle file systems:
Device/Disk/Directory(Folder)
create/delete/rename/move
Handle a file:
create/delete/rename
Handle a contents of a file:
open/close/read/write
 etc.
Each languages have corresponding functions and/or libraries/APIs for "file handling".
When you focused in certain level of hierarchy, "a contents of a file" for example, functions of that level may be comprehensible for you.
Don't to try to see broad functions in all levels at once.
That is not the way of human kind, but of god.

Writting a syntax analyser using an AFD for C language [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have been given a task to write a C language analyser using an AFD. I can choose whichever language I want so I think I will go for Ruby. However this task is a little overwhelming to grasp at the beginning.
The problem I stumble across is : How do I even represent the AFD of the entire C language?.
I have been doing a little bit of digging and I ended up reading this on lexical analysis. In this paper the author defines every token of the language as a transition between 2 states (which is very logical). I find it almost impossible for me not to miss a few or build such a big AFD by hand without many mistakes. Any tips ?
The task you have is a similar one posed to many undergraduate students in compiler courses every year in thousands of universities, and the notes you cite are good sample of the many sets of course notes available on the topic.
The solution is the same as any software engineering problem: testing against the specification.
Although the intellectual problem of the analysis and creation of AFDs for a whole language by hand might seem overwhelming error prone, don't forget you are tasked with also implementing this (in your chosen language of Ruby).
This implementation can be tested by feeding it carefully graded and selected samples of C language input. When it does not deliver the expected result there error will either be in the coding of the AFD or a fault in the AFD you constructed. You make the necessary change and go around the testing loop again.
You will eventually end up with a valid AFD for the entire C language and an analyser for it written in Ruby.
It is often a good idea to start small and implement a subset of the C language and get that working first and then add more to it using stepwise refinement. This is a less risky strategy than attempting to do the whole thing in one go.
You need to apply all those techniques you should have learned about building specifications, designs, programs and testing and apply it to this problem. Just apply good computer science and software engineering to this problem.

Ruby programmer wants to help a C novice to refactor his small C app [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
A friend of mine needs to learn programming C in school, but unfortunately he has troubles with it and his teacher seems to be pretty incapable, for example, they are using Eclipse IDE and the teacher never showed how to do a Refactor => Rename, for example (so all pupils search and replace stuff manually when they need to).
I promised my friend to help him optimize (fortunately running) app which he copy&pasted with trial&error for hours now. But since I'm a Ruby programmer for years now and touched C/C++ only for a few weeks in a programmer's beginner course about 10 years ago myself, the world of non-OO C is totally unknown for me.
So I would really appreciate some good advice on how to refactor the following small app:
http://speedy.sh/Vnfnw/Energie.zip
It basically reads a CSV (energy/heat values?), computes some stuff, and then prepares some data which should later be sent to GNUplot.
For example, I'm unsure about this:
Is it good practice to send variables into a function as pointers and manipulate them within the function body? From and OOP, I'm used to be very careful with such things and try to change variables only by sending them to some method and assigning the method's return value to the variable again, e.g. x = sum(x, y) (or with ! methods).
I'm sure there is a lot more to optimize in the code. I'm thankful for some basic hints on how to optimize the code. It doesn't have to be perfect, but it's quite a mess at the moment, and before refactoring it into the "wrong" direction, it would be nice to get some feedback here.
Thank you.
"Is it good practice to send variables into a function as pointers and manipulate them within the function body?"
If you need to change them (pass by reference) there is no other way in C. It is a standard way. Also, there are no methods. Only functions. Although you can model OOP by structs and function pointers, for the small program it is not worth it.
You just must be much more attentive when programming in C as any wrong pointer can lead the whole application to crash.
Also I advise your friend to write program by himself and not refactor copy-paste code. From my point of view, the success of refactoring lays in understanding the program much more than on knowing where is "Refactor" menu item.

Taking notes when programming? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I just got my first programming book, and I just started programming.
I have a little question. Should I take notes while reading the book, or should I just memorize, and refer back if I forget something?
Thanks
Never read from just 1 book, read multiple books on the subject to get a better picture.
for C read The C Programming Language, How To Program - C, C: A complete reference and then C a reference manual(by Harbison and Steele) touching on at least C99
Take notes, keep a book handy at all times - think before you ink though.
Always sit by a computer + text editor + compiler (yes, do not use an IDE - learn with manual compilation)
Learn good debugging techniques, gdb is fine to start off with(although has a significant learning curve)
Be attentive to what is being said in the books and - also do not forget to experiment all the time. Programming is best learnt by doing it/practicing it.
The best thing to do is to understand what is being said to you by the book, I've tried memorizing before.. it doesn't work I would suggest a bit of both, but mostly understanding it that way you would know what you are coding also practice your level of confidence of coding will increase and you'll continue to want to code more.

Resources