LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-22-2007, 07:13 AM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
execvp()


Hi friends,
this is a code written by me, here based on the value of "i" the corresponding case is executed and it lists the file in the specified directory... This is fine..
But how can i achive that all the 3 cases must be executed ( need not be using switch case ) that is i want all the 3 listings in the specified directories..( HOW CAN I DO THIS USING FORK() AND EXEC()and is there any other method to spawn all the 3 processess(i.e listings))


Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int spawn3(void)
{
        pid_t child_pid1,child_pid2,child_pid3;
        int i=2;
        char* arg_list1[] =
         {
                "ls", /* argv[0], the name of the program. */
                "-l",
                "/home/hello/include",
                NULL /* The argument list must end with a NULL. */
        };

        char* arg_list2[] =
         {
                "ls", // argv[0], the name of the program.
                "-l",
                "/home/hello/scripting",
                NULL // The argument list must end with a NULL.
        };

        char* arg_list3[] =
         {
                "ls", // argv[0], the name of the program.
                "-l",
                "/home/hello/Proj_conf",
                NULL // The argument list must end with a NULL.
        };


        switch(i)
        {
                case 3: child_pid3=fork();
                        if (child_pid3 != 0)
                        return child_pid3;
                        else
                        {
                                execvp ("ls", arg_list3);
                                fprintf (stderr, "\nan error occurred in execvp\n\n\n");
                        }
                        break;
                case 2:  child_pid2=fork();
                        if (child_pid2 != 0)
                        return child_pid2;
                        else
                        {
                                execvp ("ls", arg_list2);
                                fprintf (stderr, "\nan error occurred in execvp\n\n\n");
                        }
                        break;

                case 1: child_pid1=fork();
                        if (child_pid1 != 0)
                        return child_pid1;
                        else
                        {
                                execvp ("ls", arg_list1);
                                fprintf (stderr, "\nan error occurred in execvp\n\n\n");
                        }
                        break;

                default: exit(0);
        }

}


int main (int argc , char *argv[])
{
        int child_pid;
        child_pid=spawn3();
        printf("returned to main() and the child pid is %d\n\n",child_pid);
        return 0;
}
please someone help me
thanks....

Last edited by culin; 01-22-2007 at 07:15 AM.
 
Old 01-22-2007, 07:51 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Here's an example from Stephen G. Kochan & Patrick H. Wood, Topics in C Programming, Revised Edition (New York: John Wiley & Sons, Inc., 1991) that will give you an idea to build on. The program is a simple command-line interpreter (a "shell") and uses a clever bit of parsing (the breakup() function) to get arguments in the right places (in the argc[] array) before calling execvp().
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>

/*
 *      simple command interpreter
 *      supports < and > redirection and command line arguments
*/

int     breakup (char *, char **);

void    main    (void)
{
        char    line [BUFSIZ], *args [15];
        int     process, nargs;

        process = 0;
        for ( ; ; ) {           /* loop forever                 */
                (void) fprintf (stderr, "cmd: ");
                if (gets (line) == (char *) NULL) {
                        exit (EXIT_SUCCESS);
                }
                if ((process = fork ()) > 0)    /* parent       */
                        (void) wait ((int *) NULL);
                else if (process == 0) {        /* child        */
                        /*      parse command line              */
                        nargs = breakup (line, args);
                        /*      make sure there's something     */
                        if (nargs == 0)
                                exit (EXIT_SUCCESS);
                        /*      execute program                 */
                        (void) execvp (args [0], args);
                        /*      some problem if execvp returns  */
                        (void) fprintf (stderr, "cannot execute %s\n", line);
                        exit (errno);
                } else if (process == -1) {     /* can't create */
                        (void) fprintf (stderr, "can't fork\n");
                        exit (errno);
                }
        }
}

/*
 *      break up command line and return in "args"
 *      recognize < file and > file constructs and redirect
 *      standard input and output as appropriate
*/

int     breakup (char *line, char *args [])
{
        char    *strptr = line, *file;
        int     nargs = 0;

        while ((args [nargs] = strtok (strptr, " \t")) != (char *) NULL) {
                strptr = (char *) NULL;
                /*      output redirection      */
                if (args [nargs] [0] == '>') {
                        if (args [nargs][1] != '\0')
                                file = &args [nargs] [1];
                        else {
                                file = strtok (strptr, " \t");
                                if (file == (char *) NULL) {
                                        (void) fprintf (stderr, "no file after >\n");
                                        return (0);
                                }
                        }
                        (void) close (1);
                        if (open (file, O_WRONLY | O_TRUNC | O_CREAT, 0666) == -1) {
                                (void) fprintf (stderr, "can't open %s for output\n", file);
                                return (0);
                        }
                        --nargs;
                /*      input redirection       */
                } else if (args [nargs] [0] == '<') {
                        if (args [nargs] [1] != '\0')
                                file = &args [nargs] [1];
                        else {
                                file = strtok (strptr, " \t");
                                if (file == (char *) NULL) {
                                        (void) fprintf (stderr, "no file after <\n");
                                        return (0);
                                }
                        }
                        (void) close (0);
                        if (open (file, O_RDONLY) == -1) {
                                (void) fprintf (stderr, "can't open %s for input\n", file);
                                return (0);
                        }
                        --nargs;
                }
                ++nargs;
        }
        args [nargs] = (char *) NULL;
        return (nargs);
}
As an aside, Kochan and Wood's books are excellent, full of working examples, and a great help to both beginners and those of us long in the tooth -- I understand there is a newly revised edition of Topics either just or just about to be published; might be worth a look-see.
 
Old 01-24-2007, 02:27 AM   #3
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
Thanks for the code tronayne
 
Old 01-24-2007, 06:00 AM   #4
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
here is one more code i developed for "myshell"


Code:
# include <stdio.h>
# include <sys/types.h>
# include <unistd.h>
int main()
{
	int i = 0, j;
	char buff[100], *cptr[10], *bptr, cmd[10];
	while(1)
	{
		i=0;
		printf("\nMY SHELL: VARUN::-> ");
		fgets((char *)buff,100,stdin);
		bptr = buff;
		for(j=0;j<10;j++)
			*(cptr+j) = (char *)malloc(10);
		while(*bptr!='\0')
		{
			j = 0;
			while(*bptr != ' ' && *bptr != '\n')
			{
				*(*(cptr+i)+j) = *bptr++;
				j++;
			}
			if(*bptr==' '||*bptr=='\n')
			{
				*(*(cptr+i)+j) = '\0';
				bptr++;
			}
			i++;
		}	
		*(cptr+i) = NULL;
		strcpy(cmd,*cptr);
		switch(fork())
		{
			case 0:
					execvp(cmd,cptr);
					exit(1);	
			default:

					wait(NULL);
					break;
		}
	}
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
execvp vs. shell wiredj Programming 4 04-14-2004 08:24 AM
what is execvp? jules_fraser Linux - Networking 14 01-19-2004 04:11 PM
a question about execvp captainstorm Programming 15 10-30-2003 04:36 AM
execvp rose_bud4201 Linux - General 1 02-23-2003 02:54 AM
location of execvp? rachelkoncewicz Linux - General 2 11-15-2002 02:56 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration