1

I have a troubles with the following. Let's say, I have a two programs, one is "input.c" and second is "output.c". Output is a simple one and looks like this (I will paste only the most important passage).

outputbin.c

//
char buffer[512];
strncpy(buffer, argv[1], sizeof(buffer));
printf("Your output is: %s\n", buffer);
//

And this is the main passage from my input.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
    pid_t pid;
    char *charchar = "\x41";
    int status;
    char *outputbin;
    int i, j, iterations;

    if(argc < 2)
    {
        fprintf(stderr, "Usage: %s <iterations> <outputbin>\n", argv[0]);
        exit(0);
    }

    iterations = atoi(argv[1]);
    outputbin = argv[2];

    pid = fork();

    if(pid != 0)
    {       
        waitpid(-1, &status, 0);
    }

    if(pid == 0)
    {
        for(i=0; i < iterations; ++i)
        {
            for(j = 0; j <= i; ++j)
            {   
                printf("%s", charchar);                         
                //execl(outputbin, outputbin, charchar, NULL);              
            }
            printf("\n");
        }
    }
    return 0;
}

When I compile this program using gcc and do this (without argv[2]): ./input 10 I get this:

A
AA
AAA
AAAA
AAAAA
AAAAAA
AAAAAAA
AAAAAAAA
AAAAAAAAA
AAAAAAAAAA

It's okay, but only till I remove this piece of code - "printf("%s", input);" and uncomment "execl", so:

for(j = 0; j <= i; ++j)
                {                                                   
                    execl(output, output, input, NULL);             
                }

And run: ./a.out 10 ./outputbin

I get only this - Your output is: A

Only first char and that's all. How to let is execute whole "half pyramid" of strings line by line? No matter what I tried, everything end with the same result.

Yeez
  • 282
  • 1
  • 3
  • 9
  • Your code and question is a little confusing. I suggest post the working code in block and post the problematic code in another block. Also, please include the entire `main` function. That make it simpler to follow what your code is doing. – R Sahu Feb 15 '15 at 19:37
  • What's the `while` loop supposed to be doing? It's useless for 2 completely separate reasons (you have a nested loop incrementing `i` and using the same termination test, and the `exit` at the end of the loop prevents the `while` condition from being tested again anyway) –  Feb 15 '15 at 19:38
  • @RSahu Okay, sorry. I edited it and added whole code of input.c file. – Yeez Feb 15 '15 at 19:44
  • @WumpusQ.Wumbley I just tried something with the while loop, but still, it doesn't matter so much. Because it's the same result - with and without while. – Yeez Feb 15 '15 at 19:46

1 Answers1

0

Assuming it executes successfully, execl does not return - it replaces the program running in the process with another program. So when calling:

for(j = 0; j <= i; ++j)
{                                                   
    execl(output, output, input, NULL);             
}

The loop will only iterate one time - after execl is called, outputbin will start executing. If you want to keep the loop logic, you should move it to outputbin.c so that it'll be executed there.

Daniel Kleinstein
  • 5,262
  • 1
  • 22
  • 39
  • It looks like a good idea, I will definitely try it. But for now, I am looking for solution, how to execute it directly into "outputbin" from "input", if it's possible. – Yeez Feb 15 '15 at 19:49
  • @Yeez No, it's not possible - once you call `execl`, execution will *not* return to the program. You should move the loop logic to `outputbin.c` (you have all the necessary parameters). – Daniel Kleinstein Feb 15 '15 at 19:50
  • As I see now, it's not the real solution I am looking for... What about some other solution with a another function instead of execl? – Yeez Feb 15 '15 at 20:01
  • @Yeez It's kind of hard to answer that without knowing exactly what you're trying to do (why are you even calling `outputbin`?).. You could fork a process from inside the loop, have that process run `outputbin` with appropriate parameters, and then wait until the forked process finishes before continuing. – Daniel Kleinstein Feb 15 '15 at 20:04
  • Ok, I don't know, if I understand how you mean it or honestly - I don't know how to implement it. I'm calling "outputbin", because I want to send whole strings into some external C binary, in this case outputbin, but it doesn't matter so much, it can be any other. – Yeez Feb 15 '15 at 20:50
  • 1
    @Yeez Using `exec` is the wrong way to go if you want to run a program and then resume execution in your main program. Fork a process, use `exec` in the forked process to run your external binary, and wait for the forked process to finish (using the `wait()` function) before resuming your main program's execution. – Daniel Kleinstein Feb 15 '15 at 20:59
  • 1
    Ah, yes.. I checked today some more informations about fork(), now I think, I know how you mean it. Thanks a lot for showing me the right way. – Yeez Feb 16 '15 at 17:09