Thread: problem with pointer/array

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

    problem with pointer/array

    can someone tell me how to make this work properly ive been trying for hours! basically it should act like a shell, and run a command.

    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <errno.h>
    #define PROMPT "fire:"

    int main()
    {
    pid_t childpid;
    int status;
    char v[256]; // the problem is to do with this array ive tried
    printf("%s", PROMPT); // char * but that makes it worse.
    while(fgets(v, 256, stdin) != NULL && v[0] != '\n')

    {
    if((childpid= fork()) == -1) {
    perror("fork failed\n");
    exit(1);
    } else if (childpid == 0) { /*child code*/
    if (execvp(v[0], &v[0]) < 0)
    {
    perror("the exec of command failed \n");
    exit(1);
    }
    } else /*parent code*/
    while(childpid != wait(&status))
    if((childpid == -1) && (errno != EINTR)) break;

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >while (fgets(v, 256, stdin) != NULL && v[0] != '\n')
    I wouldn't do these 2 tests in the same statement. I don't believe you can guarantee the order in which they will be executed.

    >(execvp(v[0], &v[0]) < 0)
    You're passing the wrong kind of parameters here. Take a look at your book/manual for examples of how to use this function. The function prototype is

    Code:
    int execvp(  file, argv );
    const char *file;     /* file name            */
    char *const argv[];   /* array of arguments   */
    The value in argv[0] should point to a filename that is associated
    with the program being loaded. The last member of argv must be
    a NULL pointer. The value of argv cannot be NULL, but argv[0]
    can be a NULL pointer if no argument strings are passed.
    I haven't checked all of your code, so there may be more troubles.

    Please use code tags when posting code in future.....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    The following code works which is very similar, but its not what I want, it takes stuff from argv, I want to make a continous while loop (while 1), and run programs the user enters eg echo hello or something.

    Code:
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <errno.h>
    
    int main(int argc, char **argv){
    
        pid_t childpid;
        int status;
    
        if((childpid= fork()) == -1) {
            perror("fork failed\n");
            exit(1);
        } else if (childpid == 0) { /*child code*/
            if (execvp(argv[1], &argv[1]) < 0) {
                perror("the exec of command failed \n");
                exit(1);
            }
        } else                      /*parent code*/
            while(childpid != wait(&status))
                if((childpid == -1) && (errno != EINTR))   break;
    
        exit(0);

  4. #4
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    What does printing your argument "v[0]" ? tell you... is your code trying to exec something valid? Replace your exec statement with a print statement to find out. I'd start there, and see if your problem is in the information you're giving exec, or the way you're giving it.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    the output with printf is as it should be
    eg i type in ls

    it prints out ls

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Using the infamous google, I found plenty examples of using execvp. Here's one.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's your original code with slight amendments to make it run. It'll only do commands that don't have parameters, you'll need to parse the input before you can do that.
    Code:
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <errno.h>
    #define PROMPT	"fire:"
    
    /* */
    int main(void)
    {
    	pid_t	childpid;
    	int		status;
    	char	*v[5];		// the problem is to do with this array ive tried
    	char buffer[1024];
    	printf("%s", PROMPT);	// char * but that makes it worse.
    	
    	while (fgets(buffer, 256, stdin) != NULL)
    	{
    		if (buffer[0] == '\n') continue;
    		buffer[strlen(buffer)-1] = '\0';
    		v[0] = buffer;
    		v[1] = NULL;
    		if ((childpid = fork()) == -1)
    		{
    			perror("fork failed\n");
    			exit(1);
    		}
    		else if (childpid == 0)
    		{					/*child code*/
    			if (execvp(buffer, v) < 0)
    			{
    				perror("the exec of command failed");
    				printf ("buffer >%s<\n", buffer);
    				exit(1);
    			}
    		}
    		else				/*parent code*/
    			while (childpid != wait(&status))
    				if ((childpid == -1) && (errno != EINTR)) break;
    	}
    	return (0);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    yeah i gotta work on that

    thanks for the help though hammer greately appreciated.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > >while (fgets(v, 256, stdin) != NULL && v[0] != '\n')
    > I wouldn't do these 2 tests in the same statement. I don't believe you can guarantee the order in which they will be executed.
    The evaluation order of && and || is well defined

    In the case of &&, the right hand expression is only evaluated if the left hand expression is TRUE.

    This is not the case with the bitwise & and | operators

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >The evaluation order of && and || is well defined
    My mistake, must have been thinking of something else
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM