Thread: Need help please

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    17

    Need help please

    I want to make a very simple linux shell in c (one that will just run one program with any required arguments ) but th program is not working, i dont know why.

    Code:
    void prompt() {
         printf("Console$ ");
    }
    
    int main() {
    	while(1) {
    		prompt();
    		char s[20];
    		int res_fork, status;
    		
                    fgets(s, sizeof(s), stdin);
    		res_fork = fork();
    		if (res_fork==0) {
    			execv (s, NULL); 
    		}
    		else {
    			wait(&status);
    		}
    
    		}
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    A few problems (probably not all).

    - This is more to do with Linux programming than C, so it is in the wrong forum

    - fgets includes the newline, so when you pass a filename with a newline to exec, it won't work; you need to strip the newline.

    - you need to specify the full path of the executable, because execv doesn't understand the notion of the PATH environment variable

    - execv won't pass on the stdout of the program you run.

  3. #3
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    I think in C,you have to define variables at the top of functions.I am not sure.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    With your current implementation of:
    Code:
    		res_fork = fork();
    		if (res_fork==0) {
    			execv (s, NULL); 
    		}
    		else {
    			wait(&status);
    		}
    You could easily change all that to just system(s); and it would work, once you strip the newline off of s as previously mentioned by someone else.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by itsme86
    With your current implementation of:
    Code:
    		res_fork = fork();
    		if (res_fork==0) {
    			execv (s, NULL); 
    		}
    		else {
    			wait(&status);
    		}
    You could easily change all that to just system(s); and it would work, once you strip the newline off of s as previously mentioned by someone else.
    Use of system() is naughty, see the FAQ.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by ahluka
    Use of system() is naughty, see the FAQ.
    Maybe you should read it. Let me go over each of the points marked as "disadvantages" to system() for you.

    # The child program is uninterruptible from the parent program. This means that you cannot stop the program you have started. If it takes 5 hours to complete, that's how long your program will wait
    Same problem with the OP's current implementation of execv().

    # You cannot communicate or share variables with the child process
    The OP isn't using these features.

    # For various security reasons, its unsafe and may leave your system open to exploitation.
    The security problem is that someone can replace the program you're trying to run with a malicious program. This is not any worse than calling the program with execv() if you pass an absolute path to the program to system().

    # In system terms, its relatively slow.
    No slower than fork() + exec().

    So explain to me how it's worse than what the OP is doing again?
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by cbastard
    I think in C,you have to define variables at the top of functions.I am not sure.
    In C90, you have to declare them at the start of a block. In C99, you don't. It's a good idea to in C to declare them at the start because C90 compilers are more widespread, and it doesn't buy you a great deal to declare them "in the middle".

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed