Sword man
Sword man

Reputation: 49

Execute C language input file on linux

I don't understand how different between

a. ./target <input
b. ./target <$(cat input)
c. ./target $(<input)

./target is a C program and input is a file or payload

I want to know that how are they different and are there any more techniques or method?

Upvotes: 2

Views: 1037

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

Two of the three notations are peculiar to Bash; all three are shell notations. The programs that are run need to process the data in quite different ways.

  1. (./target <inputinput redirection): the target program needs to read standard input to get the information.

    #include <stdio.h>
    
    int main(void)
    {
        int c;
        while ((c = getchar()) != EOF)
            putchar(c);
        return 0;
    }
    
  2. (./target <$(cat input)process substitution): the target program needs to open the file name specified in a command-line argument to get the information.

    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        if (argc != 2)
        {
            fprintf(stderr, "Usage: %s file\n", argv[0]);
            return 1;
        }
        FILE *fp = fopen(argv[1], "r");
        if (fp == 0)
        {
            fprintf(stderr, "%s: failed to open file '%s' for reading\n",
                    argv[0], argv[1]);
            return 1;
        }
    
        int c;
        while ((c = getc(fp)) != EOF)
            putchar(c);
    
        fclose(fp);
        return 0;
    }
    
  3. (./target $(<input)command substitution): the target program gets the contents of the file split into words as arguments to the program, one word per argument.

    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        int count = 0;
        for (int i = 0; i < argc; i++)
        {
           count += printf(" %s", argv[i]);
           if (count > 70)
               putchar('\n'), count = 0;
        }
        return 0;
    }
    

The processing needed is quite different, therefore.

Upvotes: 2

Related Questions