C Linux程序,尝试从文件中实现Useradd

C Linux程序,尝试从文件中实现Useradd,c,linux,C,Linux,我试图从一个文件中实现useradd。“acc.txt”包含用户的姓名、姓氏和密码。在手动执行同一命令后,它工作了,但在从该程序执行时不工作 查看printf后(“%s”,命令);命令应该被执行,但是我得到了一个分段错误 #include <stdlib.h> #include <string.h> int main() { const char name[] = "acc.txt"; FILE *file = fopen(name,

我试图从一个文件中实现useradd。“acc.txt”包含用户的姓名、姓氏和密码。在手动执行同一命令后,它工作了,但在从该程序执行时不工作

查看printf后(“%s”,命令);命令应该被执行,但是我得到了一个分段错误

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

int main() {
    const char name[] = "acc.txt";
    FILE *file = fopen(name, "r");
    
    if(file == NULL)
    {
        perror(name);
        return EXIT_FAILURE;
    }
    
    char line[1200+1];
    while (fgets(line, sizeof line, file) != NULL) {
        char login[32]="";
        char full_name[50];
        char password[16];

        char name[30];
        char surname[30];
        
            sscanf(line,"%[^;];%[^;];%s",name,surname,password);
            
            strncpy(login,name,1);
            strcat(login,surname);
                    
            strcpy(full_name,name);
            strcat(full_name," ");
            strcat(full_name,surname);
            
            char command[100];
            sprintf(command,"/usr/sbin/useradd -m -p %s -c '%s' -s /bin/bash -g student %s",password, full_name,login);
            system(command);
        }
        fclose(file);   
    }

我假设内容行:
jon;雌鹿;密码2

将生成以下命令:

/usr/sbin/useradd-m-p pass2-c'jon doe'-s/bin/bash-g student jdoe

如果我的假设是正确的,那么这段代码将不会泄漏内存,并且还会执行所述结果命令:

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

void parseLineToCommand(char* line, char **command){

    char *temp = NULL; 
    char fullname [100];
    char name[50] ;
    char lastname[50] ;
    char password[100] ;
    char ret[1000];
    strcpy(ret, "/usr/sbin/useradd -m -p ");

    if (line[strlen(line) - 1] == '\n')
        line[strlen(line) - 1] = '\0';

    // Get first name
    temp = strchr(line, ';');
    temp[0] = '\0';
    strcpy(fullname, line);
    strcpy(name, line);
    line = temp + 1;

    // get last name
    temp = strchr(line, ';');
    temp[0] = '\0';
    strcat(fullname, " ");
    strcat(fullname, line);
    strcpy(lastname, line);

    // get password
    strcpy(password, temp + 1);
   

    // AT THIS POINT, EVERYTHING IS PARSED CORRECTLY -> fullname has "jon doe" and password "pass1" for example


    strcat(ret,password);
    strcat(ret, " -c \'");
    strcat(ret, fullname);
    strcat(ret, "\' -s /bin/bash -g student  ");

    // add login?? first character of name + last name?
    ret[strlen(ret)-1] = fullname[0];
    strcat(ret, lastname);

    if(*command == NULL){
        *command = malloc(sizeof(char)*strlen(ret)+1);
        strcpy(*command,ret );
    }
    else
    {
        *command = realloc(*command,sizeof(char)*strlen(ret)+1);
        strcpy(*command, ret);
    }

    
}

int main()
{
    FILE *file;
    char *command = NULL;
    char line[1201] = "";

    if ( (file = fopen("acc.txt", "r")) == NULL)
    {
        perror("acc.txt");
        return EXIT_FAILURE;
    }
    
    while (fgets(line, sizeof(line), file) != NULL){

        // parse the line, store in command
        parseLineToCommand(line,&command);

        // Print the command to the console (debugging)
        //printf("%s\n", command);
        system(command);
    }
    free(command);
    fclose(file);
    return 0;
}
#包括
#包括
#包括
void parseLineToCommand(char*行,char**命令){
char*temp=NULL;
字符全名[100];
字符名[50];
char lastname[50];
字符密码[100];
char-ret[1000];
strcpy(ret,“/usr/sbin/useradd-m-p”);
如果(行[strlen(行)-1]=='\n')
行[strlen(行)-1]='\0';
//取名字
temp=strchr(行“;”);
温度[0]='\0';
strcpy(全名,行);
strcpy(名称、行);
线路=温度+1;
//姓
temp=strchr(行“;”);
温度[0]='\0';
strcat(全名“”);
strcat(全名,行);
strcpy(姓氏,行);
//获取密码
strcpy(密码,temp+1);
//此时,所有内容都被正确解析->全名有“jondoe”和密码“pass1”
strcat(ret,密码);
strcat(ret,“-c\”);
strcat(ret,全名);
strcat(ret,“\'-s/bin/bash-g student”);
//添加登录名??姓名的第一个字符+姓氏?
ret[strlen(ret)-1]=全名[0];
strcat(ret,lastname);
如果(*命令==NULL){
*command=malloc(sizeof(char)*strlen(ret)+1;
strcpy(*命令,ret);
}
其他的
{
*command=realloc(*command,sizeof(char)*strlen(ret)+1);
strcpy(*命令,ret);
}
}
int main()
{
文件*文件;
char*command=NULL;
字符行[1201]=“”;
if((file=fopen(“acc.txt”,“r”))==NULL)
{
perror(“acc.txt”);
返回退出失败;
}
while(fgets(line,sizeof(line),file)!=NULL){
//解析行,存储在命令中
parseLineToCommand(行和命令);
//将命令打印到控制台(调试)
//printf(“%s\n”,命令);
系统(指挥部);
}
自由(指挥);
fclose(文件);
返回0;
}
肯定不是我最干净的解析代码,但它可以完成这项工作。

strncpy()长度为1看起来可疑。您应该打印字符串操作的每个步骤,以便控制一切顺利进行。我不确定登录是否是您所期望的。至少printf命令。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void parseLineToCommand(char* line, char **command){

    char *temp = NULL; 
    char fullname [100];
    char name[50] ;
    char lastname[50] ;
    char password[100] ;
    char ret[1000];
    strcpy(ret, "/usr/sbin/useradd -m -p ");

    if (line[strlen(line) - 1] == '\n')
        line[strlen(line) - 1] = '\0';

    // Get first name
    temp = strchr(line, ';');
    temp[0] = '\0';
    strcpy(fullname, line);
    strcpy(name, line);
    line = temp + 1;

    // get last name
    temp = strchr(line, ';');
    temp[0] = '\0';
    strcat(fullname, " ");
    strcat(fullname, line);
    strcpy(lastname, line);

    // get password
    strcpy(password, temp + 1);
   

    // AT THIS POINT, EVERYTHING IS PARSED CORRECTLY -> fullname has "jon doe" and password "pass1" for example


    strcat(ret,password);
    strcat(ret, " -c \'");
    strcat(ret, fullname);
    strcat(ret, "\' -s /bin/bash -g student  ");

    // add login?? first character of name + last name?
    ret[strlen(ret)-1] = fullname[0];
    strcat(ret, lastname);

    if(*command == NULL){
        *command = malloc(sizeof(char)*strlen(ret)+1);
        strcpy(*command,ret );
    }
    else
    {
        *command = realloc(*command,sizeof(char)*strlen(ret)+1);
        strcpy(*command, ret);
    }

    
}

int main()
{
    FILE *file;
    char *command = NULL;
    char line[1201] = "";

    if ( (file = fopen("acc.txt", "r")) == NULL)
    {
        perror("acc.txt");
        return EXIT_FAILURE;
    }
    
    while (fgets(line, sizeof(line), file) != NULL){

        // parse the line, store in command
        parseLineToCommand(line,&command);

        // Print the command to the console (debugging)
        //printf("%s\n", command);
        system(command);
    }
    free(command);
    fclose(file);
    return 0;
}