Thread: Append to end of file for shell in C

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    Append to end of file for shell in C

    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;
            }}


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by truecolor
    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.
    I suggest that you post the smallest and simplest compilable program that demonstrates the problem. You should also post your test input, expected output, and actual output.

    Quote Originally Posted by truecolor
    I also get a warning when I run the program: warning: multi-character character constant.
    This means that somewhere in your code (the part that you apparently left out), you wrote something like 'ab' instead of 'a' or "ab".

    By the way, please post your code as-is in plaintext, without any additional markup (whether HTML or bbcode). As long as your code is posted within [code][/code] bbcode tags, its formatting will be retained, and both syntax highlighting and line numbering will be added.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. append data to file at file's start
    By stefan63 in forum Windows Programming
    Replies: 16
    Last Post: 04-18-2012, 01:27 PM
  2. Append one file to another.
    By guillermoh in forum C Programming
    Replies: 6
    Last Post: 02-04-2008, 12:04 PM
  3. file append
    By rahulsk1947 in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 01:01 AM
  4. Append to a file
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 07-12-2005, 02:12 AM
  5. append file?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2001, 08:37 AM

Tags for this Thread