Some questions regarding old if.c


 
Thread Tools Search this Thread
Top Forums Programming Some questions regarding old if.c
# 1  
Old 12-31-2014
Some questions regarding old if.c

Hey Smilie
I have some questions regarding the old unix if command.
(see man pageman.cat-v.org/unix-6th/1/if

Here I uploaded the source code: (it's a bit too long, to put it here)
pastebin.com/bj0Hvfrw

Now my questions:

1.) Line 14: The function exp() is called with no arguments. But the function is declared as exp(s), so it needs an argument. Why is this working?


2.) What's happening there with exp() -> e1() -> e2() -> e3()... I think it's called recursive descent parsing, but I don't really get it. Could you help me a bit there.

Last edited by orbit; 01-02-2015 at 12:33 PM..
# 2  
Old 12-31-2014
Could you show us some code and usage of this 'old if.c' and explain what you are trying to do with it? That pastebin stuff is nonsense.

Last edited by ongoto; 12-31-2014 at 03:52 PM..
# 3  
Old 12-31-2014
I am trying to understand the whole source code. I would like to know what each functions does, what each line does. So I picked some lines I do not understand and wrote the questions here.

Quote:
That pastebin stuff is nonsense.
Why is it nonsense? It's the acutal source code of the command if (unix ver.6).
How to use the command in unix is described in the posted man page.
# 4  
Old 12-31-2014
I meant no offense.
I'm just going along with what you said. The questions you raised supports the fact that it doesn't make any sense, right?

if (exp()) is asking if the function exists; it's not calling that function.
p1 is not assigned to e1(), e2(), etc; p1 is a pointer to (the address of) those functions. By doing that it's redirecting the search for special characters (-r, -w, -c, etc.). The logic is: "If you don't find what you want here, I'll provide you another place to look", and so on.

A typical if statement would be something like...
if ( x = y ) { then do something
The source you provided checks for the characters '{} () = !=' and other options. In other words it's just checking for proper syntax and storing arguments for some later action. That's about all it's good for. Eventually it will return true or false or error.

Last edited by ongoto; 01-01-2015 at 03:01 PM..
This User Gave Thanks to ongoto For This Post:
# 5  
Old 01-01-2015
The source code is old K&R C, without function declarations.

Don't write code like that, and don't ever modify old K&R C by adding function declarations - unless you like getting into the intricacies and implications of C variable promotion rules, and how they may have changed over the years.

Original K&R C just took all arguments to a function, promoted them so they'd all be the same size, and stuffed them on the stack.

I think, if arguments aren't declared after the first function definition line:
Code:
main(argc, argv) <--defintion
char *argv[];  <-- argument declaration
{
    ....

then the argument implicitly defaults to "int".

Basically, in K&R C all functions are called as variable argument functions with every argument promoted to the same size, and the arguments aren't type-checked. Ever. And argument declarations in the function definitions only tell the function how to interpret the data in the variable passed - whatever that value may be, with, again, no type checking.

A "declaration" is code that tells the compiler what something is - think of it as a customs declaration for a bottle of booze - you're telling customs that you have a bottle of booze somewhere in your luggage, and what it is. It's not the bottle itself.

A "definition" is code that IS the function or variable. It's the bottle itself.

K&R C has pretty much no declarations. No one knows what anything else is. Try making drinks without knowing in advance what's in every bottle of booze...

Last edited by achenle; 01-01-2015 at 01:47 PM..
This User Gave Thanks to achenle For This Post:
# 6  
Old 01-03-2015
Thank you ongoto and thank you achenle for your great explanation Smilie

I worked really hard on understanding the code and I nearly got everything.
This is the last part I do not understand:

Code:
if(eq(a, "{")) { /* execute a command for exit code */
    if(fork()) /*parent*/ wait(&ccode);
    else { /*child*/
        doex(1);
        goto err;
    }
    while((a=nxtarg()) && (!eq(a,"}")));
    return(ccode? 0 : 1);
}

As described in the man-page (if page from Section 1 of the unix-6th manual), if we put the command in brackets "if expr { command } ", we can obtain his exit code.

So we fork the current process, and then wait for our child process to finish? But where is our child process continuing his work? After the fork, we will go into the while-loop and and just skip some arguments and then return with ccode? Where was ccode changed? What is ccode?

Could you please explain me this the given code snippet? And elaborate on ccode?

The man page of wait: wait page from Section 2 of the unix-6th manual
The source code: [C] code - Pastebin.com

Thank you very much Smilie
# 7  
Old 01-04-2015
Part of an if statement can be to call (fork) an external function; e.g. sed. You could say a == (the results of) sed /something/ for example. ccode is just the name of a variable. Judging by the way it's used here, ccode could be described as a container for:
Quote:
The exit status or return code of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) ...
The child process will be running in it's own "shell" program space and then return it's status (success or failure).
return(ccode? 0 : 1); is a ternary expression. (You can look up ternary). ? indicates a test, 0 is returned if test is true, 1 if false. I think in this case, true would indicate no errors.
This User Gave Thanks to ongoto For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Vi questions

Hello, I would like to know how we can highlight/select a section of a file in vi and delete that section if we don't want to use the dd command to delete one line at at time. There is one where we don't want to delete the whole line , but up to a certain word. (2 Replies)
Discussion started by: Pouchie1
2 Replies

2. UNIX for Dummies Questions & Answers

Just had a few questions

1) The lpr and sort utilities accept input either from a file named on the command line or from standard input. a)Name two other utilities that function in a similar manner. b)Name a utility that accepts its input only from standard input. 2) Explain the following error message. What... (10 Replies)
Discussion started by: youngyou
10 Replies

3. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

4. Programming

two questions

hey all, I have question when am writing simple shell... in the child am calling execvp, i want the parent to know when execvp returns - 1. how can i let the parent know the result of execvp thanks in advance (9 Replies)
Discussion started by: joey
9 Replies

5. UNIX for Dummies Questions & Answers

Just a few questions.

Hi everyone im new to this forums, i just wanted to get started by asking a few question(Im a Unix newbie) 1. How do i sort a file called "dirr" in a ascending order on the 3rd column 2. what does alias on=who do Thanks in advance!!! (1 Reply)
Discussion started by: Da Paper
1 Replies

6. Solaris

2 Questions

Hello Everbody I hope you can give me a hand, I have some questions The first one itīs about some message that I donīt know what means, I was looking about it. but nothing. This is the message rsh: connection from bad port bsd-gw: Error reading from connection: Bad file number And my... (4 Replies)
Discussion started by: lo-lp-kl
4 Replies

7. Programming

C questions

What does "extern" do? ex. extern int x; and another question, what about using static in functions? like: static void foo(), why? (2 Replies)
Discussion started by: Esaia
2 Replies

8. UNIX for Dummies Questions & Answers

i got some questions :)

Hi! Im new to all this but the computer club im in has unix i think. now my questions. 1.is it NTFS i need to partion the harddrive with to be able to use unix? 2.Unix and Linux whats the diffrense?yes im a noob got no idea been using crap windows for ages and hate it. 3.I got a win98... (2 Replies)
Discussion started by: Pierre
2 Replies
Login or Register to Ask a Question