I'm trying to get input from the user in the console, but I'm having problems with the function getline() in my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <memory.h>
#include <sys/wait.h>
#include <errno.h>
int main(int argc, char *argv[])
{
//check that the number of arguments given is valid
if (argc != 2){
printf("Error: arguments. \nThe file should only take one argument which is the name of the level\n");
exit(1);
}
char test[5];
int nb_lettres=strlen(argv[1]);
strncpy(test,argv[1]+nb_lettres-4,4);
//check that the file given is either a .tgz or a .tar
test[4]='\0';
if(strcmp(test,".tar")!=0 && strcmp(test,".tgz")!=0)
{
printf("Error: arguments. \nThe argument should be a file having the extension .tar or .tgz \n");
exit(2);
}
int status; //START OF THE PART CONTAINING THE PROBLEM
pid_t pid;
//create the folder then move to it, then extract the tar
if((pid=fork())!=0){
if(fork()){
execlp("mkdir","mkdir","leaSHdir",NULL);
}
//waiting to make sure we don't try to go in the folder before it's fully created
wait(&status);
execlp("tar","tar", "-xf", argv[1], "-C", "leaSHdir/",NULL);
}
waitpid(pid,&status,0);
printf("Extracting the files..\n");
sleep(1); //END OF THE PART CONTAINING THE PROBLEM
//Read the meta file
FILE *file;
chdir("./leaSHdir");
file=fopen("meta","r");
if (file==NULL){
// printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
printf("Error: meta. \nImpossible to read the meta file. Please check that it does exist (without looking in, vile cheater)\n");
exit(3);
}
char *line=NULL;
size_t len=0;
//Saving the commands which will be used by the user
char *result=NULL;
char **commands = malloc(5 * sizeof *commands);
int i=0;
if(commands==NULL){
printf("Error: memory. \nA problem occured with the memory while creating a pointer\n");
exit(4);
}
while(getline(&line,&len,file)!=-1){
if(strstr(line,"$")!=NULL){
commands[i]=(malloc(strlen(line)));
strcpy(commands[i],line+2);
//if the array is full,we add space for the next incoming command
if(i >= 4){
commands=realloc(commands,sizeof *commands *(i+2));
}
i++;
}
if(line[0]=='>'){
result=malloc(strlen(line));
strcpy(result,line+2);
}
}
int a = 0;
for (a = 0;a<i;a++){
printf("%s",commands[a]);
}
printf("%s",result);
printf("Erasing meta..");
unlink("meta");
printf("meta erased.\n");
int c;
while((c = getchar()) != '\n' && c != EOF){
printf("rien\n");
}
ssize_t r = getline(&line,&len,stdin);
printf("%d '%s' %d",(int) r, line, c);
char machin[2555];
scanf("%s",machin);
printf("%s test",machin);
free(commands);
free(result);
return 0;
}
When I execute this code, the last getline is completely skipped (the first one is working without any problem), which I don't see why. I've also tried using different functions (fgets, scanf) and both were also skipped. Thanks in advance for any help which can be provided :)
Edit:
Changed the faulty getline line with ssize_t r = getline(&line,&len,stdin); printf("%d '%s' %d",(int) r, line, c);
, here's the result:
cat
ls
man
Bravo! C'est ici!//this line and the 3 other lines before are the lines read by the first getline which is working
Erasing meta..meta erased.
-1 '> Bravo! C'est ici!
' -1
So basically, I don't even have time to type anything, I get this result directly without entering anything. Also the content of line
isn't changed after the second getline
considering it still contains the result from the first getline
.
Edit 2 :
Okay, I think I found from where comes the problem: Basically, it's from a part of the code i didn't put in the extract there because I though it was not related at all with my current problem, so I've edited the whole extract to put it fully. I've put two comments to mark the part containing the problem. Although I don't see what could be causing it, considering this part contains only forks.
Although, sorry for the trouble, guys, should have put the whole code at the start
Last edit:
Figured out what was the problem: if((pid=fork())!=0){
which means that once my forks ended I was working on the child process and not on the father as I thought. Once I've changed it to if((pid=fork())==0){
everything worked fine. Thanks for the help :)