如何将字符串导入C中的popen()命令?

时间:2021-02-20 07:37:52

Im working in c (more or less for the first time) for uni, and I need to generate an MD5 from a character array. The assignment specifies that this must be done by creating a pipe and executing the md5 command on the system.

我用c语言(或多或少是第一次)处理uni,我需要从一个字符数组中生成一个MD5。赋值指定必须通过在系统上创建管道并执行md5命令来实现这一点。

I've gotten this far:

我已经这么远:

FILE *in;
extern FILE * popen();
char buff[512];

/* popen creates a pipe so we can read the output
 * of the program we are invoking */
char command[260] = "md5 ";
strcat(command, (char*) file->name);
if (!(in = popen(command, "r"))) {
    printf("ERROR: failed to open pipe\n");
    end(EXIT_FAILURE);
}

Now this works perfectly (for another part of the assignment which needs to get the MD5 for a file) but I cant workout how to pipe a string into it.

现在,它可以很好地工作(对于作业的另一部分,它需要获得一个文件的MD5),但是我无法训练如何将一个字符串导入其中。

If I understand correctly, I need to do something like:

如果我理解正确的话,我需要做一些类似的事情:

FILE * file = popen("/bin/cat", "w");
fwrite("hello", 5, file);
pclose(file);

Which, I think, would execute cat, and pass "hello" into it through StdIn. Is this right?

我认为,它会执行cat,并通过StdIn传递“hello”。这是正确的吗?

3 个解决方案

#1


2  

If you need to get a string into the md5 program, then you need to know what options your md5 program works with.

如果您需要将字符串放入md5程序,那么您需要知道您的md5程序使用哪些选项。

  • If it takes a string explicitly on the command line, then use that:

    如果它在命令行上显式地接受一个字符串,则使用该字符串:

    md5 -s 'string to be hashed'
    
  • If it takes standard input if no file name is given on the command line, then use:

    如果在命令行上没有给出文件名,则需要使用标准输入:

    echo 'string to be hashed' | md5
    
  • If it absolutely insists on a file name and your system supports /dev/stdin or /dev/fd/0, then use:

    如果它绝对坚持文件名,而您的系统支持/dev/stdin或/dev/fd/0,则使用:

    echo 'string to be hashed' | md5 /dev/stdin
    
  • If none of the above apply, then you will have to create a file on disk, run md5 on it, and then remove the file afterwards:

    如果以上方法都不适用,那么您必须在磁盘上创建一个文件,在其上运行md5,然后在以后删除该文件:

    echo 'string to be hashed' > file.$$; md5 file.$$; rm -f file.$$
    

#2


1  

See my comment above:

看到我的评论上面:

FILE* file = popen("/sbin/md5","w");
fwrite("test", sizeof(char), 4, file);
pclose(file);

produces an md5 sum

生成md5和

#3


0  

Try this:

试试这个:

static char command[256];
snprintf(command, 256, "md5 -qs '%s'", "your string goes here");
FILE* md5 = popen(md5, "r");
static char result[256];
if (fgets(result, 256, md5)) {
     // got it
}

If you really want to write it to md5's stdin, and then read from md5's stdout, you're probably going to want to look around for an implementation of popen2(...). That's not normally in the C library though.

如果您真的想把它写到md5的stdin中,然后从md5的stdout中读取,您可能会想要查看一下popen2(…)的实现。这在C库中是不正常的。

#1


2  

If you need to get a string into the md5 program, then you need to know what options your md5 program works with.

如果您需要将字符串放入md5程序,那么您需要知道您的md5程序使用哪些选项。

  • If it takes a string explicitly on the command line, then use that:

    如果它在命令行上显式地接受一个字符串,则使用该字符串:

    md5 -s 'string to be hashed'
    
  • If it takes standard input if no file name is given on the command line, then use:

    如果在命令行上没有给出文件名,则需要使用标准输入:

    echo 'string to be hashed' | md5
    
  • If it absolutely insists on a file name and your system supports /dev/stdin or /dev/fd/0, then use:

    如果它绝对坚持文件名,而您的系统支持/dev/stdin或/dev/fd/0,则使用:

    echo 'string to be hashed' | md5 /dev/stdin
    
  • If none of the above apply, then you will have to create a file on disk, run md5 on it, and then remove the file afterwards:

    如果以上方法都不适用,那么您必须在磁盘上创建一个文件,在其上运行md5,然后在以后删除该文件:

    echo 'string to be hashed' > file.$$; md5 file.$$; rm -f file.$$
    

#2


1  

See my comment above:

看到我的评论上面:

FILE* file = popen("/sbin/md5","w");
fwrite("test", sizeof(char), 4, file);
pclose(file);

produces an md5 sum

生成md5和

#3


0  

Try this:

试试这个:

static char command[256];
snprintf(command, 256, "md5 -qs '%s'", "your string goes here");
FILE* md5 = popen(md5, "r");
static char result[256];
if (fgets(result, 256, md5)) {
     // got it
}

If you really want to write it to md5's stdin, and then read from md5's stdout, you're probably going to want to look around for an implementation of popen2(...). That's not normally in the C library though.

如果您真的想把它写到md5的stdin中,然后从md5的stdout中读取,您可能会想要查看一下popen2(…)的实现。这在C库中是不正常的。