0

In my program i get to 2 paths, one is a path of a directory which contains all kinds of files. Whenever I find a c file I compile it. The second path is of an input txt file. lets say something like this:

home/dvir/workspace/assignment1/students/  -(directory)
home/dvir/workspace/tests/input1.txt/ -(input txt file)

here is part of my code:

void listdir(const char *name, int indent)
{
    char path[80];
    char cmd[4096 + 2*80];

    DIR *dir;
    struct dirent *entry;

    if (!(dir = opendir(name)))
        return;

    while ((entry = readdir(dir)) != NULL) {
        if (entry->d_type == DT_DIR) {
            char path[1024];
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
                continue;
            snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
            printf("%*s[%s]\n", indent, "", entry->d_name);
            listdir(path, indent + 2);
        } else {
            printf("%*s- %s\n", indent, "", entry->d_name);
            snprintf(path, sizeof(path), "%s/%s", name, entry->d_name); 
            snprintf(cmd, sizeof(cmd), "gcc -c %s -o %s.o", path, path);
            if (system(cmd) == 0) {
                printf("Compiled %s to %s.o\n", path, path);
            }
        }
    }
    closedir(dir);
}

It goes recursivly over the directory and compiles all the files, I saved the input.txt directory in a char array and called it input. now lets assume a have a c program I compiles and got the ashly.c.o file. how can I run this program using the txt file as an input?(and how do I actually get access to that compiled program?) for example: let's say the ashly.c.o gets 2 numbers from the user and multiples them. I want to use the input.txt file to be these 2 numbers and save the output as a new txt file.(so that I can read it latter) I have found some ofthis answers but in my case I don't want to use freopen() function (just open) and I need I a way to access the compiled file from my program... Any help would be appreciated.

ashly
  • 3
  • 6
  • 2
    are you trying to re-invent [make](http://man7.org/linux/man-pages/man1/make.1.html) ? – Sander De Dycker Dec 10 '18 at 13:13
  • 2
    It's a bit unclear to me what you are asking but as far as I can tell you have not generated any executable program yet. You need to link the .o files together (or in case each c-file is a full program, you need to compile without the -c option) – Support Ukraine Dec 10 '18 at 13:24
  • You typically cannot run .o files. But you could make a library out of them, then `dlopen()`it, `dlsym()`-link the functions provide by the single .o which made it into the library and call these functions. – alk Dec 10 '18 at 13:32
  • Perhaps you might want [pipe(7)](http://man7.org/linux/man-pages/man7/pipe.7.html)-s – Basile Starynkevitch Dec 10 '18 at 13:34

1 Answers1

0

fork and exec may do what you want.

if (fork() == 0) {
    int fd = open("/path/to/your/input", O_RDONLY);
    dup2(fd, 0);
    // execl or so
    exec("/path/to/the/created.o", ...);
}
Ze Chen
  • 21
  • 5
  • could you please explain a little bit on how it works? – ashly Dec 10 '18 at 13:42
  • After gcc compiles the file (I guess the `-c` option should be dropped), fork() and exec() would create a new process that execute the .o file just compiled. See also [redirect-exec](https://stackoverflow.com/questions/2605130/redirecting-exec-output-to-a-buffer-or-file). `stdin` was redirected to the input file by `dup2` before `exec` (I didn't know whether you've decided to use `stdin` as input, drop that line if it was not the case). More information on `fork` and `exec` may be found on APUE, CSAPP or [here](https://stackoverflow.com/questions/19099663/how-to-correctly-use-fork-exec-wait). – Ze Chen Dec 10 '18 at 14:09
  • thank you,I do understand the idea better now, One more thing,I keep on getting the warning: implicit declaration of function ‘exec’ ,although i did include unistd.h.nDo you have any idea on what may be the reason?(I tried to compile the code without changing anything) – ashly Dec 10 '18 at 14:22
  • `#include ` may help (but if linker doesn't throw an error, the program would run despite of the warning). – Ze Chen Dec 10 '18 at 14:29
  • I dont know if it's the linker but the program throws /tmp/ccUPN2ER.o: In function `main': program.c:(.text+0x42): undefined reference to `exec' collect2: error: ld returned 1 exit status – ashly Dec 10 '18 at 14:36
  • I forgot to clarify that `exec` is not a function but a family of functions, see [exec-functions](https://stackoverflow.com/questions/25339617/in-function-main-undefined-reference-to-exec) and choose one. I would fix my code. – Ze Chen Dec 10 '18 at 14:39