Sponsored Content
Top Forums Programming Some questions regarding old if.c Post 302930432 by Don Cragun on Sunday 4th of January 2015 01:10:04 AM
Old 01-04-2015
Quote:
Originally Posted by orbit
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
Quote:
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.
If you look closely, I think you'll find that that is "if { command }" (with no expr); the exit status of command, in this case, is the expression.

ongoto already explained most of what is going on in the code above. From your comments above, I get the feeling that you don't understand how fork() works. If we look at the code:
Code:
    if(fork()) /*parent*/ wait(&ccode);
    else { /*child*/
        doex(1);
        goto err;
    }
    while((a=nxtarg()) && (!eq(a,"}")));
    return(ccode? 0 : 1);

Quote:
So we fork the current process, and then wait for our child process to finish?
What you have to understand is that after a successful call to fork(), it essentially returns twice; once in the parent process and once in the child process. In the parent process (the one that called fork()), the return code is never zero; if it is positive, it is the process ID of the newly created child process, and if it is negative, it indicates that the fork was unsuccessful and errno will indicate what error occurred (in this case there is no child process).

In the child process, fork() always returns zero.

Quote:
But where is our child process continuing his work?
So the code shown in green above, is only executed in the child process and the code shown in red is only executed in the parent process. Presumably, the function doex() in the child will parse the expression between the { and }, evaluate it, and then branch to the code at the label err which should terminate the child process with an exit code indicating whether the expression evaluated to true or false.

Quote:
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?
The wait(&ccode) in the parent waits for the child to exit and saves the exit status from the child in the variable ccode. Then the while loop in the parent skips over the expression that was evaluated by the child and returns true or false depending on the exit status of the child.

Note also that exit code 0 conventionally indicates success and a non-zero exit code indicates a failure. So, the command:
Code:
test -r file

produces exit status zero if file is readable and produces a non-zero exit status if file can't be found or is not readable. As ongoto explained, the ternary operator converts the exit code convention (0 for success, non-zero for failure) into the C convention (1 for true, 0 for false).
This User Gave Thanks to Don Cragun For This Post:
 

8 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

8. 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
All times are GMT -4. The time now is 10:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy