Thread: Linux cp command C implementation

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    1

    Linux cp command C implementation

    Hi guys, so I've just started learning systems programming and have been trying to work on an implementation of the Linux cp command in the C language. I came across a program on these forums that works for the following cases:

    1. copy contents of one file to another
    2. copy file into a directory in the current directory.

    I was trying to add to my own code to the program so that I could copy multiple files into a directory in the current directory
    eg. (cp1 f1.txt f2.txt f3.txt dir) or (cp1 d1/d2/d3/f1.txt d4/d5/d6 f2.txt dir)

    Below is my code but im stuck. Im trying to loop through the argument values from av[1] to av[ac-2] in my main( ) function. Im stuck on how to do this loop though. How do i loop through those arguments? Help would be great!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <string.h>
    #include <dirent.h>
    #include <sys/types.h>
    #include <sys/stat.h>
     
    #define BUFFERSIZE 1024
    #define COPYMORE 0644
     
    void errExit(char *, char *);
    int copyFile(char *src, char *dest);
    int dostat(char *filename);
    int mode_isReg(struct stat info);
     
     
    int main(int ac, char *av[])
    {
      /* checks args */
      if(ac != 3)
      {
        fprintf(stderr, "usage: %s source destination\n", *av);
        exit(1);
      }
     
     
       char *src = av[1];
       char *dest = av[2];
     
     
       if( src[0] != '/' && dest[0] != '/' )//cp2 file1.txt file2.txt
       {
           copyFile(src, dest);
       }
       else if( src[0] != '/' && dest[0] == '/' )//cp2 file1.txt /dir 
       {
          int i;
          for(i=1; i<=strlen(dest); i++)
          {
              dest[(i-1)] = dest[i];
          }
          strcat(dest, "/");
          strcat(dest, src);
          copyFile(src, dest);
        
       }
    
    
       //LOOP THROUGH AV[1] TO AV[AC-1] 
    
    
           //SOME STUFF HERE
    
    
           //PASS STUFF TO COPYFILE( )
    
    
    
    
      else
      {
          fprintf(stderr, "usage: cp1 source destination\n");
          exit(1);
      }
    }
     
     
     
    int dostat(char *filename)
    {
        struct stat fileInfo;
         
        //printf("Next File %s\n", filename);
        if(stat(filename, &fileInfo) >=0)
        if(S_ISREG(fileInfo.st_mode))
          return 1;
        else return 0;
         
        return;
    }
     
     
     
     
    int copyFile(char *source, char *destination)
    {
      int in_fd, out_fd, n_chars;
      char buf[BUFFERSIZE];
     
     
      /* open files */
      if( (in_fd=open(source, O_RDONLY)) == -1 )
      {
        errExit("Cannot open ", source);
      }
     
     
      if( (out_fd=creat(destination, COPYMORE)) == -1 )
      {
        errExit("Cannot create ", destination);
      }
     
     
      /* copy files */
      while( (n_chars = read(in_fd, buf, BUFFERSIZE)) > 0 )
      {
        if( write(out_fd, buf, n_chars) != n_chars )
        {
          errExit("Write error to ", destination);
        }
     
     
        if( n_chars == -1 )
        {
          errExit("Read error from ", source);
        }
      }
     
     
        /* close files */
        if( close(in_fd) == -1 || close(out_fd) == -1 )
        {
          errExit("Error closing files", "");
        }
     
     
        return 1;
    }
     
     
      void errExit(char *s1, char *s2)
      {
        fprintf(stderr, "Error: %s ", s1);
        perror(s2);
        exit(1);
      }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementation of linux cp (copy) command in C language
    By eurosickwitit in forum C Programming
    Replies: 3
    Last Post: 11-17-2011, 10:40 AM
  2. Ketchup command in linux?
    By kiros88 in forum Tech Board
    Replies: 4
    Last Post: 04-21-2010, 11:56 AM
  3. How to catch the result of linux command
    By mihu in forum Linux Programming
    Replies: 10
    Last Post: 04-16-2006, 06:57 AM
  4. C implementation of unix ls command
    By sinkovich in forum Linux Programming
    Replies: 0
    Last Post: 02-24-2003, 04:38 AM
  5. cd command in linux console
    By TheUnheardHuman in forum Tech Board
    Replies: 2
    Last Post: 11-19-2002, 03:59 PM

Tags for this Thread