0

I want to use a goto statement in my code after the exec system call. but it exits from the program after using exec. How can it stay in my code?

Here is the code.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>

#define MAX_LINE 80
#define MAX 3

void main()
{
    char *args[MAX_LINE];
    char arg1[MAX_LINE/2]="\0";
    char arg2[MAX_LINE/2]="\0";

UBOS: 
    printf("ubos>");
    fflush(stdout);
    fgets(arg1, sizeof(arg1), stdin);
    arg1[strlen(arg1)-1]='\0';
    fgets(arg2, sizeof(arg2), stdin);
    arg2[strlen(arg2)-1]='\0';
    printf("You typed: %s %s\n",arg1,arg2);
    fflush(stdin);
    if (strlen(arg2) == '\0')
    {
         args[0] = arg1;
         args[1] = '\0';
    } 
    else 
    {
        args[0] = arg1;
            args[1] = arg2;
            args[2] = 0;
    }
    int i;
    for(i=0;i<MAX && args[i];i++)
    {
            printf("Vlue of args[%d] =%s\n",i, args[i]);
    }

    execvp(args[0],args);
    goto UBOS;


    printf("Something is not correct...\n");
    exit(0);
}

I there any way I can make this code work?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Viren Patel
  • 128
  • 1
  • 11
  • 1
    That's what `exec` (and its variants) do; see [this previous question](http://stackoverflow.com/questions/4204915/please-explain-exec-function-and-its-family) (and especially the discussion about using it together with `fork`). – Gordon Davisson Oct 12 '16 at 23:55
  • Yup, that's what exec does. Did you read the documentation? – user253751 Oct 13 '16 at 01:15
  • Note that if you gave a non-existent program name so the `exec` fails, your code would loop. – Jonathan Leffler Oct 13 '16 at 03:00

1 Answers1

1

With exec family to run programs, once the specified program file starts its execution, the original program in the caller's address space is gone and is replaced by the new program. Thus there is no "return". In case of error condition , calling programs gets a negative return with errno set.

You can fork and have the child process do the execvp and the parent call goto

Prabhu
  • 3,443
  • 15
  • 26