Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C Argv check which argv path came from what input

If i input paths with wildcards as command line parameters, like:
testprogramm a* b*
and the directory contains the following content:
aa ab ba bb
my argv string will contain:
{"testprogramm","aa","ab","ba","bb"}

However, i want to differentiate between files that originated from the first argument (a*) and the second (b*), how do i do that? What i am searching for is a method that can tell me by the example above that "aa" and "ab" came from the first argument and "ba" and "bb" from the second.

I know that the cp.c commands can do this, but i couldn’t figure out how, as the source code is quite nested.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Edit: standard copy (from gnu core utils) cannot differentiate, only embedded (shells, where programm and utils are the same file) can.

>Solution :

./program a* "|"  b*

And then search the delimiter from your program:

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

int main(int argc, char *argv[])
{
    int group = 0;

    for (int i = 1; i < argc; i++)
    {
        if (strcmp(argv[i], "|") == 0)
        {
            group++;
        }
        else
        {
            printf("group: %d file: %s\n", group, argv[i]);
        }
    }
    return 0;
}

The output is:

group: 0 file: aa
group: 0 file: ab
group: 1 file: ba
group: 1 file: bb
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading