Home > database >  How to take argument from c and use it as a function
How to take argument from c and use it as a function

Time:05-06

I would like to take an argument, that is given to me through the terminal and use it for zipping the files. For example, I have main.c file that is look something like that.

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

// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...

int main(int argc, char **argv)
{
    if(argc > 2){
        printf("Enough arguments\n");
        // tisk argumentu
        printf("%s\n\n", argv[1]);

        // tisk argumentů
        for (int i; i < argc; i  ){
            printf("%s\n", argv[i]);
        }
    }
    else{
        printf("Insufficient number of arguments\n");
    } 
    return 0;
}

and I give it through the terminal these parameters (gzip text1.txt text2.txt) like this.

$ gcc main.c -o main
$ ./main gzip text1.txt text2.txt

My question is how to take the parameter gzip which is in argv[1] and apply this function to the text1.txt and text2.txt.

This is what I have now. Problem is, that execlp takes place only once and I wanted to make this possible for all arguments (text1.txt, text2.txt --> later for more than two) now I am able to make it possible only for one bcs I hard code it there (argv[2]) --> that is the text1.txt and I need to make it possible for n files. Is there someone who will be able to answer the question "For what I should replace the argv[2] to gzip all files instead only one?"

#include <sys/types.h>
// we use unistd (in windows is process.h) library that allows us to execute program (such as gzip, ping etc) inside our program
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...

int main(int argc, char* argv[])
{
    
    if(argc > 2){
        printf("Enough arguments\n");
        // printing second argument
        printf("Program we use to compress: %s\n\n", argv[1]);

        // printing all arguments
        for (int i = 0; i < argc; i  ){
            printf("%d.argument: %s\n", i, argv[i]);
        }

        int tmp;
        for (int i = 0; i < argc   1; i  ){
            if (i < 2){   
                tmp   ;
            }
            else{
                fork();
                 // execl (we use execlp to avoid defining path to pragram, or execvp where v stants for vector to pass more parametrs as argument) has firt two argument defined as program we want to execute to executables that we've sort as arguments to the reminal
                execlp(
                argv[1],
                argv[1],
                argv[2],
                NULL
                );
                // if the program lands here we've got a problem because something went wrong so we use library errno to define an error that occurs
                int err = errno;
                if (err == 2){
                    printf("File with that name not found or path to that file was wrong!\n");
                    break;
                }
            }
        }
    }
    else{
        printf("Insufficient number of arguments\n");
    } 
    return 0;
}

po úpravě

#include <sys/types.h>
// we use unistd (in windows is process.h) library that allows us to execute program (such as gzip, ping etc) inside our program
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...

int main(int argc, char* argv[])
{
    
    if(argc > 2){
        printf("Enough arguments\n");
        // printing second argument
        printf("Program we use to compress: %s\n\n", argv[1]);

        // printing all arguments
        for (int i = 0; i < argc; i  ){
            printf("%d.argument: %s\n", i, argv[i]);
        }

        for (int i = 2; i < argc   1; i  ){
            // execl (we use execlp to avoid defining path to pragram, or execvp where v stants for vector to pass more parametrs as argument) has firt two argument defined as program we want to execute to executables that we've sort as arguments to the reminal
            execlp(
            argv[1],
            argv[1],
            argv[i],
            NULL
            );
            // if the program lands here we've got a problem because something went wrong so we use library errno to define an error that occurs
            int err = errno;
            if (err == 2){
                printf("File with that name not found or path to that file was wrong!\n");
                break;
            }
        }
    }
    else{
        printf("Insufficient number of arguments\n");
    } 
    return 0;
}

CodePudding user response:

The execv function takes an array of char *, the last of which must be NULL. The argv array fits this definition, so you can pass in the address of argv[1] which gives you the command specified and its parameters.

Also, you should check the return value of fork and only call execv in the child process.

if (fork() == 0) {
    // child
    execv(argv[1], &argv[1]);
    perror("exec failed");
    _exit(1);
} else {
    // parent
    wait(NULL);
}

CodePudding user response:

There it is! Finall working solution

#include <sys/types.h>
// we use unistd (in windows is process.h) library that allows us to execute program (such as gzip, ping etc) inside our program
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

// gzip text1 = 1st process, gzip text2 = 2nd process, gzip text3 = 3rd ...

int main(int argc, char* argv[])
{
    
    if (argc > 2){
        printf("Enough arguments\n");
        // printing second argument
        printf("Program we use to compress: %s\n\n", argv[1]);

        // printing all arguments
        printf("Arguments we use:\n");
        for (int i = 0; i < argc; i  ){
            printf("%d.argument: %s\n", i, argv[i]);
        }

        // we start our loop from third argument
        for (int i = 2; i < argc   1; i  ){
            
            // splitting our program to n processes
            int id = fork();
            
            if (id == 0){
                // execl (we use execlp to avoid defining path to pragram, or execvp where v stants for vector to pass more parametrs as argument) has firt two argument defined as program we want to execute to executables that we've sort as arguments to the reminal
                execlp(
                argv[1], // program we use
                argv[1], // program we use
                argv[i], // the document to which the program is applied
                NULL
                );
                // if the program lands here we've got a problem because something went wrong so we use library errno to define an error that occurs
                int err = errno;
                if (err == 2){
                    printf("File with that name not found or path to that file was wrong!\n");
                    break;
                }
            }
        }
    }
    else{
        printf("Insufficient number of arguments\n");
    } 
    return 0;
}
  •  Tags:  
  • c
  • Related