1 /* file.c Copyright 1992-2000 by Michael Temari All Rights Reserved
3 * This file is part of ftp.
6 * 01/25/96 Initial Release Michael Temari, <Michael@TemWare.Com>
25 static char *dir(char *path
, int full
);
26 static int asciisize(int fd
, off_t
*filesize
);
27 static off_t
asciisetsize(int fd
, off_t filesize
);
28 static int cloneit(char *file
, int mode
);
30 #if (__WORD_SIZE == 4)
31 static char buffer
[8192];
33 static char buffer
[2048];
35 static char line2
[512];
37 static char *dir(path
, full
)
47 sprintf(cmd
, "ls -l %s > %s", path
, name
);
49 sprintf(cmd
, "ls -dA %s > %s", path
, name
);
63 s
= DOcommand("TYPE", "A");
77 s
= DOcommand("TYPE", "I");
91 s
= DOcommand("MODE", "B");
105 s
= DOcommand("MODE", "S");
119 s
= DOcommand("PWD", "");
121 if(s
== 500 || s
== 502)
122 s
= DOcommand("XPWD", "");
138 if(readline("Path: ", line2
, sizeof(line2
)) < 0)
143 if(!strcmp(path
, ".."))
144 s
= DOcommand("CDUP", "");
146 s
= DOcommand("CWD", path
);
148 if(s
== 500 || s
== 502) {
149 if(!strcmp(path
, ".."))
150 s
= DOcommand("XCUP", "");
152 s
= DOcommand("XCWD", path
);
169 if(readline("Directory: ", line2
, sizeof(line2
)) < 0)
174 s
= DOcommand("MKD", path
);
176 if(s
== 500 || s
== 502)
177 s
= DOcommand("XMKD", path
);
193 if(readline("Directory: ", line2
, sizeof(line2
)) < 0)
198 s
= DOcommand("RMD", path
);
200 if(s
== 500 || s
== 502)
201 s
= DOcommand("XRMD", path
);
216 if(readline("File: ", line2
, sizeof(line2
)) < 0)
221 return(DOcommand("DELE", file
));
234 if(readline("File: ", line2
, sizeof(line2
)) < 0)
239 return(DOcommand("MDTM", file
));
252 if(readline("File: ", line2
, sizeof(line2
)) < 0)
257 return(DOcommand("SIZE", file
));
266 printf("You must \"OPEN\" a connection first.\n");
269 return(DOcommand("STAT", ""));
277 if(readline("File: ", line2
, sizeof(line2
)) < 0)
282 return(DOcommand("STAT", file
));
308 fd
= open(local
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
311 printf("Could not open local file %s. Error %s\n", local
, strerror(errno
));
315 s
= DOdata("LIST", path
, RETR
, fd
);
346 fd
= open(local
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
349 printf("Could not open local file %s. Error %s\n", local
, strerror(errno
));
353 s
= DOdata("NLST", path
, RETR
, fd
);
363 char *file
, *localfile
;
373 if(readline("Remote File: ", line2
, sizeof(line2
)) < 0)
381 localfile
= cmdargv
[2];
383 fd
= open(localfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
386 printf("Could not open local file %s. Error %s\n", localfile
, strerror(errno
));
390 s
= DOdata("RETR", file
, RETR
, fd
);
399 char *file
, *localfile
;
411 if(readline("Remote File: ", line2
, sizeof(line2
)) < 0)
419 localfile
= cmdargv
[2];
421 fd
= open(localfile
, O_RDWR
);
424 printf("Could not open local file %s. Error %s\n", localfile
, strerror(errno
));
429 if(asciisize(fd
, &filesize
)) {
430 printf("Could not determine ascii file size of %s\n", localfile
);
435 filesize
= lseek(fd
, 0, SEEK_END
);
437 sprintf(restart
, "%lu", filesize
);
439 s
= DOcommand("REST", restart
);
446 s
= DOdata("RETR", file
, RETR
, fd
);
453 char *ttime(time_t t
)
456 static char tbuf
[16];
460 sprintf(tbuf
, "%04d%02d%02d%02d%02d.%02d",
461 tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
,
462 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
467 static int cloneit(file
, mode
)
475 static unsigned short lcrc
;
476 static unsigned short ccrc
;
477 static unsigned long csize
;
479 static unsigned long maj
;
480 static unsigned long min
;
481 static unsigned long uid
;
482 static unsigned long gid
;
483 static unsigned long fmode
;
484 static unsigned long size
;
485 static unsigned long mtime
;
487 unsigned short crc(char *fname
);
490 /* see if file exists locally */
491 ss
= stat(file
, &st
);
495 s
= DOcommand("SITE FDET", file
);
501 sscanf(reply
, "%*d %c%lu%lu%lu%lu%lu%lu%lu",
502 &ft
, &maj
, &min
, &uid
, &gid
, &fmode
, &size
, &mtime
);
507 s
= DOcommand("SITE CCRC", file
);
512 sscanf(reply
, "%*d %*s%u%lu", &ccrc
, &csize
);
513 if(ss
< 0) return(-1);
515 if(size
!= csize
|| size
!= st
.st_size
|| ccrc
!= lcrc
)
518 if(ss
< 0 && ft
== 'd') {
519 s
= mkdir(file
, fmode
);
520 printf("mkdir %s\n", file
);
522 if((ss
< 0) && (ft
== 'b' || ft
== 'c' || ft
== 'p')) {
523 s
= mknod(file
, fmode
, maj
<< 8 | min
);
524 printf("mknod %c %u %u\n", file
, maj
, min
);
528 ss
= stat(file
, &st
);
531 if(st
.st_uid
!= uid
|| st
.st_gid
!= gid
) {
532 s
= chown(file
, uid
, gid
);
533 printf("chown %u:%u %s\n", uid
, gid
, file
);
535 if(st
.st_mode
!= fmode
) {
536 s
= chmod(file
, fmode
);
537 printf("chmod %04o %s\n", fmode
, file
);
539 if(st
.st_mtime
!= mtime
) {
542 s
= utime(file
, &ut
);
543 printf("touch -m -t %s %s\n", ttime(mtime
), file
);
563 if(readline("Files: ", line2
, sizeof(line2
)) < 0)
570 fd
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
573 printf("Could not open local file %s. Error %s\n", name
, strerror(errno
));
577 s
= DOdata("NLST", files
, RETR
, fd
);
581 if(s
== 226 || s
== 250) {
582 fp
= fopen(name
, "r");
584 if(fp
== (FILE *)NULL
) {
585 printf("Unable to open file listing.\n");
588 while(fgets(line2
, sizeof(line2
), fp
) != (char *)NULL
) {
589 p
= line2
+ strlen(line2
) - 1;
590 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
591 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
592 printf("Retrieving file: %s\n", line2
); fflush(stdout
);
593 fd
= open(line2
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
595 printf("Unable to open local file %s\n", line2
);
597 s
= DOdata("RETR", line2
, RETR
, fd
);
611 char *file
, *remotefile
;
621 if(readline("Local File: ", line2
, sizeof(line2
)) < 0)
629 remotefile
= cmdargv
[2];
631 fd
= open(file
, O_RDONLY
);
634 printf("Could not open local file %s. Error %s\n", file
, strerror(errno
));
638 s
= DOdata("APPE", remotefile
, STOR
, fd
);
647 char *file
, *remotefile
;
657 if(readline("Local File: ", line2
, sizeof(line2
)) < 0)
665 remotefile
= cmdargv
[2];
667 fd
= open(file
, O_RDONLY
);
670 printf("Could not open local file %s. Error %s\n", file
, strerror(errno
));
674 s
= DOdata("STOR", remotefile
, STOR
, fd
);
683 char *file
, *remotefile
;
686 off_t filesize
, rmtsize
;
695 if(readline("Local File: ", line2
, sizeof(line2
)) < 0)
703 remotefile
= cmdargv
[2];
705 s
= DOcommand("SIZE", remotefile
);
710 rmtsize
= atol(reply
+4);
712 fd
= open(file
, O_RDONLY
);
715 printf("Could not open local file %s. Error %s\n", file
, strerror(errno
));
720 filesize
= asciisetsize(fd
, rmtsize
);
722 filesize
= lseek(fd
, rmtsize
, SEEK_SET
);
724 if(filesize
!= rmtsize
) {
725 printf("Could not set file start of %s\n", file
);
730 sprintf(restart
, "%lu", rmtsize
);
732 s
= DOcommand("REST", restart
);
739 s
= DOdata("STOR", remotefile
, STOR
, fd
);
748 char *file
, *remotefile
;
758 if(readline("Local File: ", line2
, sizeof(line2
)) < 0)
766 remotefile
= cmdargv
[2];
768 fd
= open(file
, O_RDONLY
);
771 printf("Could not open local file %s. Error %s\n", file
, strerror(errno
));
775 s
= DOdata("STOU", remotefile
, STOR
, fd
);
796 if(readline("Files: ", line2
, sizeof(line2
)) < 0)
801 name
= dir(files
, 0);
803 fp
= fopen(name
, "r");
805 if(fp
== (FILE *)NULL
) {
806 printf("Unable to open listing file.\n");
810 while(fgets(line2
, sizeof(line2
), fp
) != (char *)NULL
) {
811 p
= line2
+ strlen(line2
) - 1;
812 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
813 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
814 printf("Sending file: %s\n", line2
); fflush(stdout
);
815 fd
= open(line2
, O_RDONLY
);
817 printf("Unable to open local file %s\n", line2
);
819 s
= DOdata("STOR", line2
, STOR
, fd
);
830 static int asciisize(fd
, filesize
)
840 while((cnt
= read(fd
, buffer
, sizeof(buffer
))) > 0) {
841 p
= buffer
; pp
= buffer
+ cnt
;
856 static off_t
asciisetsize(fd
, filesize
)
865 while(sp
< filesize
) {
866 s
= read(fd
, buffer
, 1);
893 fd
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
896 printf("Could not open local file %s. Error %s\n", name
, strerror(errno
));
900 s
= DOdata("NLST", files
, RETR
, fd
);
904 if(s
== 226 || s
== 250) {
905 fp
= fopen(name
, "r");
907 if(fp
== (FILE *)NULL
) {
908 printf("Unable to open file listing.\n");
911 while(fgets(line2
, sizeof(line2
), fp
) != (char *)NULL
) {
912 p
= line2
+ strlen(line2
) - 1;
913 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
914 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
916 if(cloneit(line2
, 1)) {
917 printf("Retrieving file: %s\n", line2
); fflush(stdout
);
918 fd
= open(line2
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
920 printf("Unable to open local file %s\n", line2
);
922 s
= DOdata("RETR", line2
, RETR
, fd
);
926 s
= cloneit(line2
, 2);