我试图从C:
运行git pushsystem("git push");
当它要求
时username:
password:
我想给它一个用户名和一个github身份验证令牌。我怎么做到这一点?我试图寻找解决方案,但是当我解决这个问题时,我似乎无法正确理解。请注意,我在char*
&#39>中存储了用户名和身份验证令牌:
char *username = "whatever";
char *token = "whatever";
system("git push");
答案 0 :(得分:0)
execv can be used to accomplish this .
int execv(const char *file, char *const argv[]);
示例: 以下示例说明了如何使用execv来执行ls shell命令:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
main()
{
pid_t pid;
char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL};
if ((pid = fork()) == -1)
perror("fork error");
else if (pid == 0) {
execv("/bin/ls", parmList);
printf("Return not expected. Must be an execv error.n");
}
}
您可以根据代码进行更改
答案 1 :(得分:0)
如果使用system(char *command);
创建输入数据是不可能的(或者可能,但我认为太难了),您需要使用popen(const char *command, const char *type);
创建新流程。 (popen documentation here)。这是我写的一个小例子:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char username[] = "something";
char password[] = "something";
FILE *pf; // file descriptor of the git process
pf = popen("git push", "w"); // create git process in write mode
if (!pf) {
printf("Process couldn't be created!");
return 1;
}
// this will send username and password (and the '\n' char too) to the git process
fputs(username, pf);
fputs(password, pf);
pclose(pf); // close git process
return 0;
}