void ftpmap_download(ftpmap_t *ftpmap) { int fsize = ftpmap_fsize(ftpmap); int dsize = 0, rsize = 0; FILE *fd, *file; char *filename = NULL; char *answer = NULL; char buffer[MAX_STR]; filename = (strrchr(ftpmap->path, '/'))+1; if (( file = fopen(filename, "w")) == NULL ) die(1, "Failed to write %s.", ftpmap->path); fd = ftpmap_data_tunnel(ftpmap); fprintf(ftpmap->fid, "RETR %s\r\n", ftpmap->path); answer = ftpmap_getanswer(ftpmap); if ( *answer == 0 ) return; while (( rsize = fread(buffer, 1, sizeof(buffer), fd)) > 0 ) { if ( buffer[rsize +1] == '\r' ) buffer[rsize +1] = '\0'; dsize += fwrite(buffer, 1, rsize, file); printf(":-: Downloading %s %s/%s ...\r",ftpmap->path,calc_bytes_size(dsize), calc_bytes_size(fsize)); fflush(stdout); } printf("\n:-: File saved: %s\n", filename); fclose(file); }
void ftpmap_upload(ftpmap_t *ftpmap) { FILE *lfp, *fd; int fsize = 0; int rsize = 0, dsize = 0; int buffer[MAX_STR]; char *filename = "d"; char *answer = NULL; if ( strrchr(ftpmap->path, '/')) filename = (strrchr(ftpmap->path, '/'))+1; else filename = ftpmap->path; if (( lfp = fopen(ftpmap->path, "rb")) == NULL ) die(1, "Failed to read \'%s\' ...", ftpmap->path); fseek(lfp, 0L, SEEK_END); fsize += (int)ftell(lfp); fseek(lfp, 0L, SEEK_SET); fd = ftpmap_data_tunnel(ftpmap, "w"); fprintf(ftpmap->fid, "TYPE I\r\n"); answer = ftpmap_getanswer(ftpmap); if ( *answer == 0 ) return; fprintf(ftpmap->fid, "STOR %s\r\n", filename); answer = ftpmap_getanswer(ftpmap); if ( *answer == 0 ) return; logger_write(ftpmap, ":-: %s", answer); while (( rsize = fread(buffer, 1, sizeof(buffer), lfp)) > 0 ) { if ( buffer[rsize +1 ] == '\r' ) buffer[rsize + 1] = '\0'; dsize += fwrite(buffer, 1, rsize, fd); printf(":-: Uploading %s %s/%s bytes...\r", filename, calc_bytes_size(dsize), calc_bytes_size(fsize)); fflush(stdout); } printf(":-: File \'%s\' Uploaded ...\n", filename); fclose(lfp); }