The code is a copy of shell. It needs to make commands like
ls -a > test.txt | sort < test.txt
When I compiled the program, it didn't work as intended. So I thought of trying gdb. When I used gdb I got the right answer with the right output. How is it possible?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int ind=0; // Indicator that shows if the app is still working
// Some details to make it look like shell
/*char *user;
user=getlogin();*/
/*uid_t user;
user=getuid();
struct passwd *nick;
nick=getpwuid(user);
char *login;
login=(nick->pw_name);*/
// For some reason after using anything related to the user, the program messes up.
printf("\n");
printf("\033[0;32muser\033[0m");
putchar(':');
printf("\033[0;34m~\033[0m");
printf("$ ");
while(ind==0)
{
// Reading the input and organizing it.
// 1) Reading from stdin and putting it in an array.
int max_size=16;
char *chars=malloc(max_size*sizeof(char));
int cur_size=max_size;
int cur_len=0;
int max_len=0;
int word_amount=2;
int com_amount=1;
if(chars==NULL)
{
fprintf(stderr, "Something was wrong with the memory. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
int c;
int i=0;
c=getchar();
if(c==EOF || c=='\n')
{
ind=1;
continue;
}
while(c!='\n')
{
if(c=='|')
{
com_amount++;
}
if(c==' ')
{
word_amount++;
if(cur_len>max_len)
{
max_len=cur_len;
}
cur_len=0;
}
else
{
cur_len++;
}
chars[i]=(char)c;
i++;
if(i==cur_size)
{
cur_size+=max_size;
chars=realloc(chars, cur_size);
if(chars==NULL)
{
fprintf(stderr, "Something was wrong with the memory. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
}
c=getchar();
}
if(word_amount==2)
{
max_len=cur_len;
}
chars[i]='\0';
max_len+=2;
//Testing
/*i=0;
while(chars[i]!='\0')
{
printf("%d :%c\n", i, chars[i]);
i++;
}*/
// 2) Making it from a char array to string array using the counter.
// max_len is the size of the longest word + 2 (Just in case)
// word_amount is the amount of the words + 1 (Just in case)
int ind[com_amount];
char *argv[word_amount];
char file_name[com_amount][word_amount];
for(i=0; i<word_amount; i++)
{
argv[i]=malloc(max_len);
if(argv[i]==NULL)
{
fprintf(stderr, "Something was wrong with the memory. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
}
int j=0;
int w=0;
int s=0;
int y=0;
for(i=0; i<word_amount; i++)
{
ind[i]=0;
}
/*for(i=0; i<com_amount; i++)
{
file_name[i]=NULL;
}*/
i=0;
while(chars[i]!='\0')
{
if(chars[i]=='>')
{
ind[s]=1;
while(chars[i]!='|' && chars[i]!='\0')
{
i++;
if(chars[i]==' ')
{
while(chars[i]==' ')
{
i++;
}
}
file_name[s][y]=chars[i];
y++;
}
file_name[s][y-1]='\0';
y=0;
}
else if(chars[i]=='<')
{
ind[s]=2;
while(chars[i]!='|' && chars[i]!='\0')
{
i++;
if(chars[i]==' ')
{
while(chars[i]==' ')
{
i++;
}
}
file_name[s][y]=chars[i];
y++;
}
file_name[s][y]='\0';
y=0;
}
if(chars[i]=='|')
{
s++;
}
if(chars[i]!=' ')
{
argv[j][w]=chars[i];
w++;
i++;
}
else if(chars[i]==' ')
{
while(chars[i]==' ' && chars[i]!='\0')
{
i++;
}
if(chars[i]=='\0')
{
break;
}
argv[j][w]='\0';
j++;
w=0;
}
}
if(j==0)
{
argv[j][w]='\0';
argv[j+1]=NULL;
}
else
{
if(ind[s]==0)
{
argv[j+1]=NULL;
}
else
{
argv[j]=NULL;
}
}
//Testing
/*for(i=0; argv[i]!=NULL; i++)
{
printf("%d: %s\n", i, argv[i]);
}*/
/*for(i=0; i<com_amount; i++)
{
printf("%d: %s\n", i, file_name[i]);
}*/
// 3) Finally, making an array of pointers, that will be pointing at those words without '|'
j=0;
char **args[com_amount];
args[0]=argv;
for(i=1; (i<(word_amount-2)) && (j<(com_amount-1)); i++)
{
if(((argv[i][0]=='|') && (argv[i][1]=='\0')) || (argv[i][0]=='\n'))
{
argv[i]=NULL;
j++;
args[j]=argv+i+1;
}
}
//Testing
/*for(i=0; i<com_amount; i++)
{
for(j=0; args[i][j]!=NULL; j++)
{
printf("%d: %s\n", i, args[i][j]);
}
}*/
//Preparation done, we have an array of commands. Now to the pipes and forks.
printf("\033[0;32muser\033[0m");
putchar(':');
printf("\033[0;34m~\033[0m");
printf("$ ");
pid_t son[com_amount];
int *fds=NULL;
fds=(int *)malloc((com_amount-1)*2*sizeof(int));
for(i=0; i<(com_amount-1); i++)
{
if(pipe(fds+2*i)==-1)
{
fprintf(stderr, "Something went wrong while making a pipe. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
}
i=0;
if(ind[i]==1)
{
int fd;
fd=open(file_name[i], O_WRONLY | O_CREAT | O_TRUNC, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fd, 1);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else if(ind[i]==2)
{
int fd;
fd=open(file_name[i], O_RDONLY, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fd, 0);
if(com_amount>1)
{
dup2(fds[2*i+1], 1);
}
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else
{
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
if(com_amount>1)
{
dup2(fds[2*i+1], 1);
}
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
execvp(args[i][0], args[i]);
return 2;
}
}
for(int i=1; i<(com_amount-1); i++)
{
if(ind[i-1]==1)
{
if(ind[i]==1)
{
int fd;
fd=open(file_name[i], O_WRONLY | O_CREAT | O_TRUNC, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fd, 1);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else if(ind[i]==2)
{
int fd;
fd=open(file_name[i], O_RDONLY, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fd, 0);
dup2(fds[2*i+1], 1);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else
{
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fds[2*(i-1)], 0);
dup2(fds[2*i+1], 1);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
execvp(args[i][0], args[i]);
return 2;
}
}
}
else
{
if(ind[i]==1)
{
int fd;
fd=open(file_name[i], O_WRONLY | O_CREAT | O_TRUNC, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fds[2*(i-1)], 0);
dup2(fd, 1);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else if(ind[i]==2)
{
int fd;
fd=open(file_name[i], O_RDONLY, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fd, 0);
dup2(fds[2*i+1], 1);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else
{
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fds[2*(i-1)], 0);
dup2(fds[2*i+1], 1);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
execvp(args[i][0], args[i]);
return 2;
}
}
}
}
if(com_amount>1)
{
i=com_amount-1;
if(ind[i-1]==1)
{
if(ind[i]==1)
{
int fd;
fd=open(file_name[i], O_WRONLY | O_CREAT | O_TRUNC, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fd, 1);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else if(ind[i]==2)
{
int fd;
fd=open(file_name[i], O_RDONLY, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fd, 0);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else
{
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fds[2*(i-1)], 0);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
execvp(args[i][0], args[i]);
return 2;
}
}
}
else
{
if(ind[i]==1)
{
int fd;
fd=open(file_name[i], O_WRONLY | O_CREAT | O_TRUNC, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fds[2*(i-1)], 0);
dup2(fd, 1);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else if(ind[i]==2)
{
int fd;
fd=open(file_name[i], O_RDONLY, 0600);
if(fd==-1)
{
fprintf(stderr, "Something went wrong while creating a file. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fd, 0);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
close(fd);
execvp(args[i][0], args[i]);
return 2;
}
close(fd);
}
else
{
son[i]=fork();
if(son[i]==-1)
{
fprintf(stderr, "Something went wrong while making a process. Error code: %d: %s\n", errno, strerror(errno));
return 1;
}
if(son[i]==0)
{
dup2(fds[2*(i-1)], 0);
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
execvp(args[i][0], args[i]);
return 2;
}
}
}
}
for(j=0; j<2*(com_amount-1); j++)
{
close(fds[j]);
}
while(wait(NULL) != -1);
}
printf("Back to main shell\n\n");
return 0;
}
When compiling it and just running it the answer is nothing, in gdb it outputs the sorted list of files in stdout.