int doCopy(int argc, char **argv){
  struct location *dest, *src;
  int i;
  int r;
  if(opts(argc,argv)<0)
    return EXIT_FAILURE;
  dest=parse_location(destination);
  if(open_location(dest,WRITE)<0)
    return EXIT_FAILURE;
  for(i=0;i<nsources;++i){
    src=parse_location(sources[i]);
    if(open_location(src,READ)<0){
      return EXIT_FAILURE;
    }
    if(do_copy(src,dest,0) < 0){
        break;
    }
  }
  if(dest->is_ssh){
          r=ssh_scp_close(dest->scp);
          if(r == SSH_ERROR){
                  fprintf(stderr,"Error closing scp: %s\n",ssh_get_error(dest->session));
                  ssh_scp_free(dest->scp);
                  dest->scp=NULL;
                  return -1;
          }
  } else {
          fclose(dest->file);
          dest->file=NULL;
  }
  ssh_disconnect(dest->session);
  ssh_finalize();
  return 0;
}
int main(int argc, char **argv) {
    struct location *dest, *src;
    int i;
    int r;
    if (opts(argc, argv) < 0) {
        r = EXIT_FAILURE;
        goto end;
    }

    dest = parse_location(destination);
    if (dest == NULL) {
        r = EXIT_FAILURE;
        goto end;
    }

    if (open_location(dest, WRITE) < 0) {
        location_free(dest);
        r = EXIT_FAILURE;
        goto end;
    }

    for (i = 0; i < nsources; ++i) {
        src = parse_location(sources[i]);
        if (src == NULL) {
            r = EXIT_FAILURE;
            goto close_dest;
        }

        if (open_location(src, READ) < 0) {
            location_free(src);
            r = EXIT_FAILURE;
            goto close_dest;
        }

        if (do_copy(src, dest, 0) < 0) {
            close_location(src);
            location_free(src);
            break;
        }

        close_location(src);
        location_free(src);
    }

    r = 0;

close_dest:
    close_location(dest);
    location_free(dest);
end:
    return r;
}