I am trying to write the shell program in C for I/O redirection. The input and output is working fine, but I also have to append output to the end of a file with the '>>', but it's not working. Whenever, I use >> in the command line, it just overwrites the output to the file. And I also get a warning when I run the program: warning: multi-character character constant. I would really appreciate it if someone can please assist me in the right direction to fix this problem.
Code:
// Check for redirected input
input = redirect_input(args,&input_filename);
switch(input){
case-1:
printf("Syntax error!\n");
continue;
break;
case0:
break;
case1:
printf("Redirecting input from: %s\n", input_filename);
break;
}
// Check for redirected output
output = redirect_output(args,&output_filename);
switch(output){
case-1:
printf("Syntax error!\n");
continue;
break;
case0:
break;
case1:
printf("Redirecting output to: %s\n", output_filename);
break;
}
// Check for redirected append output
append = redirect_append(args,&append_filename);
switch(append){
case-1:
printf("Syntax error!\n");
continue;
break;
case0:
break;
case1:
printf("Redirecting output to: %s\n", output_filename);
break;
}
// Do the command
do_command(args, block,
input, input_filename,
output, output_filename,
append, append_filename);
}
}
/*
* Do the command
*/
int do_command(char**args,int block,
int input,char*input_filename,
int output,char*output_filename,
int append,char*append_filename){
int result;
pid_t child_id;
int status;
// Fork the child process
child_id = fork();
// Check for errors in fork()
switch(child_id){
case EAGAIN:
perror("Error EAGAIN: ");
return;
case ENOMEM:
perror("Error ENOMEM: ");
return;
}
if(child_id ==0){
// Set up redirection in the child process
if(input)
freopen(input_filename,"r", stdin);
if(output)
freopen(output_filename,"w+", stdout);
if(append)
freopen(append_filename,"a", stdout);
// Execute the command
result = execvp(args[0], args);
exit(-1);
}
// Wait for the child process to complete, if necessary
if(block){
printf("Waiting for child, pid = %d\n", child_id);
result = waitpid(child_id,&status,0);
}
}
/*
* Check for input redirection
*/
int redirect_input(char**args,char**input_filename){
int i;
int j;
for(i =0; args[i]!= NULL; i++){
// Look for the <
if(args[i][0]=='<'){
//free(args[i]);
// Read the filename
if(args[i+1]!= NULL){
*input_filename = args[i+1];
}else{
return-1;
}
// Adjust the rest of the arguments in the array
for(j = i; args[j-1]!= NULL; j++){
args[j]= args[j+2];
}
return1;
}
}
return0;
}
/*
* Check for output redirection
*/
int redirect_output(char**args,char**output_filename){
int i;
int j;
for(i =0; args[i]!= NULL; i++){
// Look for the >
if(args[i][0]=='>'){
//free(args[i]);
// Get the filename
if(args[i+1]!= NULL){
*output_filename = args[i+1];
}else{
return-1;
}
// Adjust the rest of the arguments in the array
for(j = i; args[j-1]!= NULL; j++){
args[j]= args[j+2];
}
return1;
}
}
return0;
}
/*
* Check for append redirection
*/
int redirect_append(char**args,char**append_filename){
int i;
int j;
for(i =0; args[i]!= NULL; i++){
// Look for the >>
if(args[i][0]=='>'&& args[i][1]=='>'){
//free(args[i]);
// Read the filename
if(args[i+2]!= NULL){
*append_filename = args[i+2];
}else{
return-1;
}
// Adjust the rest of the arguments in the array
for(j = i; args[j-1]!= NULL; j++){
args[j]= args[j+2];
}
return1;
}}