コード例 #1
0
ファイル: copy.c プロジェクト: pkhamutou/EOPSY
int main(int argc, char **argv){
    if(argc == 1){
        fprintf(stderr, "copy: Missing arguments list\nTry 'copy -h' for help\n");
        return 1;
    }
    
    int c;
    int mflag = 0;
    
    opterr = 0;

    while((c = getopt(argc, argv, "hm")) != -1){        //if set "hm:" then -m requirs 1 argument and i have no idea how to force 2.
        switch (c){
            case 'h':
                printf("Usage:\n");
                printf(" copy <file_name> <new_file_name>\n");
                printf(" copy [-m] <file_name> <new_file_name>\n");
                return 0;
            case 'm':
                if(argc != 4){
                    fprintf(stderr, "copy: Option -m requires 2 arguments.\nTry 'copy -h' for help\n");
                    return 1;
                }
                mflag = 1;
                break;
            case '?':
                if(isprint (optopt))
                    fprintf(stderr, "copy: Unknown option '-%c'\nTry 'copy -h' for help\n", optopt);
                else
                    fprintf(stderr, "copy: Unknown option character `\\x%x`.\n", optopt);
                return 1;
            default:
                fprintf(stderr, "copy: abort()");
                abort();
        }
    }   //while
    //Possible cases: 
        //argc == 4 && mflag == 1
        //argc == 3 && mflag == 0
    if(argc != 3 && mflag == 0){
        fprintf(stderr, "copy: Wrong arguments list\nTry 'copy -h' for help\n");
        return 1;
    }
    
    int fd_from, fd_to;     //declaring file descriptors

    if((fd_from = open(argv[mflag+1], O_RDONLY)) == -1 || (fd_to = open(argv[mflag+2], O_RDWR | O_CREAT, 0666)) == -1){   //O_RDWD
        perror("open");
        return 1;
    }

    if(mflag == 0) //without -m option
        copy_read_write(fd_from, fd_to);
    else
        copy_mmap(fd_from, fd_to);

    close(fd_from);
    close(fd_to);
    return 0;
}
コード例 #2
0
ファイル: copy.solution.c プロジェクト: whyjay/SNU
int main(int argc, char *argv[])
{
  int in, out, mode, res = EXIT_FAILURE;

  // make sure the user provided the necessary command line arguments
  if (argc != 4) {
    fprintf(stderr, "Usage: %s <source> <dest> <mode>\n"
                    "\n"
                    "Copies a file from <source> to <dest>.\n"
                    "<mode> must be either 1 or 2 where\n"
                    "  mode==%d      use buffer copy algorithm\n"
                    "  mode==%d      use mmap copy algorithm\n",
                    argv[0], M_BUFFER, M_MMAP);
    exit(EXIT_FAILURE);
  }

  // first check the mode
  mode = atoi(argv[3]);
  if (!((mode == M_BUFFER) || (mode == M_MMAP))) {
    fprintf(stderr, "Invalid copy mode specified (%d)\n", mode);
    exit(EXIT_FAILURE);
  }


  // open the source file read-only and check for errors
  in = open(argv[1], O_RDONLY);
  if (in == -1) {
    perror("Error opening input file");
    exit(EXIT_FAILURE);
  }

  // check if output file exists and abort if yes
  if (access(argv[2], F_OK) == 0) {
    fprintf(stderr, "Cowardly refusing to overwrite an exiting file\n");
    exit(EXIT_FAILURE);
  }

  // open the destination file write-only and check for errors
  out = open(argv[2], O_CREAT|O_TRUNC|O_WRONLY,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  if (out == -1) {
    perror("Error opening output file");
    exit(EXIT_FAILURE);
  }

  // invoke file copy
  switch (mode) {
    case M_BUFFER: res = copy_buffer(in, out);
                   break;
    case M_MMAP:   res = copy_mmap(in, out);
                   break;
  }

  // close source & destination
  close(in);
  close(out);

  return res;
}