Thread: Some questions regarding old C code

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    Some questions regarding old C code

    Hey all
    I have some trouble understanding the old goto.c ( see also goto man page V6 Thompson Shell Port - Manuals - GOTO(1) ) from this source code:

    Code:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    int offset 0; main(argc, argv) char *argv[]; { extern fin; char line[64]; if (argc<2 || ttyn(0)!='x') { write(1, "goto error\n", 11); seek(0, 0, 2); return; } seek(0, 0, 0); fin = dup(0); loop: if (getlin(line)) { write(1, "label not found\n", 16); return; } if (compar(line, argv[1])) goto loop; seek(0, offset, 0); } getlin(s) char s[]; { int ch, i; i = 0; l: if ((ch=getc())=='\0') return(1); if (ch!=':') { while(ch!='\n' && ch!='\0') ch = getc(); goto l; } while ((ch=getc())==' '); while (ch!=' ' && ch!='\n' && ch!='\0') { s[i++] = ch; ch = getc(); } while(ch != '\n') ch = getc(); s[i] = '\0'; return(0); } compar(s1, s2) char s1[], s2[]; { int c, i; i = 0; l: if(s1[i] != s2[i]) return(1); if (s1[i++] == '\0') return(0); goto l; } getc() { offset++; return(getchar()); }
    Line 1-17: Everything ok.
    My problem starts with getlin(line).
    I try to comment as far as I understood:
    Code:
    getlin(s) 
    char s[]; 
    { 
            int ch, i; 
      
            i = 0; 
    l: 
    // if we reach the end of the script, we return with 1 
            if ((ch=getc())=='\0') return(1); 
    //If the first char is not a ':' we iterate char by char thorugh the whole line
    //we goto l and check the next line for a starting ':'
            if (ch!=':') { 
                    while(ch!='\n' && ch!='\0') 
                            ch = getc(); 
                    goto l; 
                    } 
    // there is a ':' as the first char in a line found
    // we skip all the spaces in front of the label   
            while ((ch=getc())==' '); 
    // Now we copy just the label to s  
            while (ch!=' ' && ch!='\n' && ch!='\0') { 
                    s[i++] = ch; 
                    ch = getc(); 
                    } 
    // Question 1
            while(ch != '\n') 
                    ch = getc(); 
            s[i] = '\0'; 
    // Question 2
            return(0); 
    }
    @Question 1
    Why would you do this while loop?
    What would happen if we don't run this loop?

    @Question 2
    Why do we return with 0? We found a line, which starts with ':' and a label.
    If we return 0, we would go into
    Code:
     if (getlin(line)) { 
                    write(1, "label not found\n", 16); 
                    return; 
                    }
    altough we did find a label.

    I hope you can help me

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    1) The notion of a "label" in that code is something between a ':' and a space (or newline). That code will deal with extraneous text on the end of a line. For example, given input like ":abc def" it will discard the "def". It will also loop forever if the last line of input is not terminated with a newline.

    2) Return value of zero indicates a label was found. Return value of 1 indicates no label found. It is an old convention, often used in old C, that 0 indicates success (or found, for a search) and non-zero indicates failure.

    There is a wrinkle that the code assumes getchar() returns zero on any error (rather than EOF), and it also defines a function getc() (which is a function in the standard library). But the code is also making use of features of some specific (and probably old) compiler/library, so those are possible moot points.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Thank you grumpy

    1)
    Imagine this script:
    Code:
    ...
    goto end
    ...
    : end
    Now let's look at this part of the code:
    Code:
    while (ch!=' ' && ch!='\n' && ch!='\0') {
    s[i++] = ch;
    ch = getc();
    }
    while(ch != '\n')                 
    ch = getc();         
    s[i] = '\0'; 
    We would exit the first while-loop with ch='\0', because ": end" ist the last line of our script. And now we would enter the second while-loop.
    But how will we exit the second while-loop? ch will never be '\n' because we already reached the end of our script?

    2)
    Return value of zero indicates a label was found. Return value of 1 indicates no label found.
    Okay, I understand that. But then it should be:
    Code:
    if (!getlin(line)) 
    {
    write(1, "label not found\n", 16);
    return;                 
    } 
    
    Shouldn't it?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    1) That was the point of one of my previous comments.

    2) No. "if (!x)" is equivalent to "if (x == 0)". Only do either if you really want to print "label not found" when a label is found.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 11-04-2013, 12:12 AM
  2. New to C... questions about this code...ADTs
    By eva in forum C Programming
    Replies: 3
    Last Post: 02-24-2008, 10:07 AM
  3. Code Questions
    By Taikon in forum C Programming
    Replies: 8
    Last Post: 03-02-2005, 08:31 AM
  4. Nub Code Questions
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-25-2004, 09:30 PM
  5. Questions on behavior of code
    By emily in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2003, 01:48 AM

Tags for this Thread