Samuel Eng
Samuel Eng

Reputation: 3

echo variable in c program

I have a program written in C language(admin-secret) that has a function called authenticate. Inside this function there is a variable called "result". How do i echo this variable using another c program?
The purpose of this is to guess the password using strncmp return value.

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

int main(int argc, char *argv[])
{     
    char command[1000] = {0};
    int result;
    sprintf(command, "/home/alice/Public/admin-secret %s; echo  %d", argv[1], result);
    system(command);
    printf("Result: %s\n" , result);
    return 0;
}

Upvotes: 0

Views: 2159

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You need to create a pipe, an easy way to do it is by using the POSIX function popen().

#include <stdio.h>

int main(int argc, char* argv[])
{
    FILE *pipe;
    char line[256];
    pipe = popen("ls", "r");
    if (pipe != NULL)
    {
        while (fgets(line, sizeof(line), pipe) != NULL)
            fprintf(stdout, "%s", line);
        pclose(pipe);
    }
    return 0;
}

You can build the command with sprintf()1 too and pass it as the first parameter to popen(), this is just to show you how you can capture the output of another program.


1You should really use snprintf() to prevent overflowing the destination array

Upvotes: 1

Related Questions