simple shell (c), fgets problem

This is a discussion on simple shell (c), fgets problem within the Linux Programming forums, part of the Platform Specific Boards category; Hi all, Could you please look at my source at http://razza.org/source.html (its formatted, and color coded) I have two problems, ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    simple shell (c), fgets problem

    Hi all,

    Could you please look at my source at http://razza.org/source.html (its formatted, and color coded)

    I have two problems, they seem to be related
    1

    when I use fgets to grab from the command line, rather than gets, I seem unable to run programs like ping ipaddress (ping doesnt receive the ip) If I use gets ping will work.

    2
    However my shell should also run programs in the background (that is fork the process off, and return to the command prompt before the process has finished). To do this you would type say echo hello & (the '&' should always trail), this by the way actually works for echo but if you try ls / & ( then it complains)

    I presumed that their is something wrong with the parse function, so I substituted another users function in, the shell then worked correctly with fgets.

    Since I check for the & symbol in my parse function I was unable to check wether the background process bug is also fixed.

    Hopefully one of you guys can tell me whats up with my parse function

    Thanks

    razza
    Last edited by razza; 05-25-2002 at 11:28 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,538
    > when I use fgets to grab from the command line, rather than gets
    You've got to strip off that newline before passing the parameters to your programs

    > if you try ls / & ( then it complains)
    Can you be more specific?

    > its formatted, and color coded
    Well it's colour coded, but I wouldn't say it was formatted
    Formatted is where all the statements at the same scope level actually line up

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    well for ls for example:

    ls /

    (lists directory contents properly)

    ls / &

    ls: : No such file or directory
    /:

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    Code:
    int parse(char *buffer, char **v) {
    int temp = 0;
            while (*buffer != '\0') // if not the end of buffer yet.
            {
                    while (*buffer == ' ' || *buffer == '\t' || *buffer == '\n')
                    *buffer++ = '\0';     // replace white spaces (includes tabs etc..)
                    *v++ = buffer;          // save buffer place to v.
    Once out of the while loop v is null terminated with *v = '\0'

    wouldn't the above code take care of the final new line character?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,538
    > wouldn't the above code take care of the final new line character?
    On the face of it, yes
    But that doens't mean it actually works

    Try printing each v[i] using this loop
    Code:
    for ( i = 0 ; v[i] != NULL ; i++ ) {
      printf( "param %d is '%s'\n", i, v[i] );
    }
    printing the string between two '' will allow you to see if there are any extra space/newlines which you didn't expect.


    > Once out of the while loop v is null terminated with *v = '\0'
    *v = NULL;
    would be better

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    Thanks I didn't think of putting the value in ' ', which did help me spot something

    Managed to fix it, so it works now.

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    try calling the other functions from main and adding return 0; to main

    ERR nm
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 05:42 AM
  2. Help: A Simple Shell
    By roaming_builder in forum C Programming
    Replies: 6
    Last Post: 02-10-2008, 09:12 PM
  3. problem with A simple modeless messagebox
    By hanhao in forum C++ Programming
    Replies: 8
    Last Post: 07-05-2005, 11:18 PM
  4. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  5. Replies: 5
    Last Post: 12-03-2003, 04:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21