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", ""));
278 if(readline("File: ", line2
, sizeof(line2
)) < 0)
283 return(DOcommand("STAT", file
));
309 fd
= open(local
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
312 printf("Could not open local file %s. Error %s\n", local
, strerror(errno
));
316 s
= DOdata("LIST", path
, RETR
, fd
);
347 fd
= open(local
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
350 printf("Could not open local file %s. Error %s\n", local
, strerror(errno
));
354 s
= DOdata("NLST", path
, RETR
, fd
);
364 char *file
, *localfile
;
374 if(readline("Remote File: ", line2
, sizeof(line2
)) < 0)
382 localfile
= cmdargv
[2];
384 fd
= open(localfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
387 printf("Could not open local file %s. Error %s\n", localfile
, strerror(errno
));
391 s
= DOdata("RETR", file
, RETR
, fd
);
400 char *file
, *localfile
;
412 if(readline("Remote File: ", line2
, sizeof(line2
)) < 0)
420 localfile
= cmdargv
[2];
422 fd
= open(localfile
, O_RDWR
);
425 printf("Could not open local file %s. Error %s\n", localfile
, strerror(errno
));
430 if(asciisize(fd
, &filesize
)) {
431 printf("Could not determine ascii file size of %s\n", localfile
);
436 filesize
= lseek(fd
, 0, SEEK_END
);
438 sprintf(restart
, "%u", filesize
);
440 s
= DOcommand("REST", restart
);
447 s
= DOdata("RETR", file
, RETR
, fd
);
454 char *ttime(time_t t
)
457 static char tbuf
[16];
461 sprintf(tbuf
, "%04d%02d%02d%02d%02d.%02d",
462 tm
->tm_year
+1900, tm
->tm_mon
+1, tm
->tm_mday
,
463 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
468 static int cloneit(file
, mode
)
476 static unsigned short lcrc
;
477 static unsigned short ccrc
;
478 static unsigned long csize
;
480 static unsigned long maj
;
481 static unsigned long min
;
482 static unsigned long uid
;
483 static unsigned long gid
;
484 static unsigned long fmode
;
485 static unsigned long size
;
486 static unsigned long mtime
;
488 unsigned short crc(char *fname
);
491 /* see if file exists locally */
492 ss
= stat(file
, &st
);
496 s
= DOcommand("SITE FDET", file
);
502 sscanf(reply
, "%*d %c%lu%lu%lu%lu%lu%lu%lu",
503 &ft
, &maj
, &min
, &uid
, &gid
, &fmode
, &size
, &mtime
);
508 s
= DOcommand("SITE CCRC", file
);
513 sscanf(reply
, "%*hu %*s%u%lu", &ccrc
, &csize
);
514 if(ss
< 0) return(-1);
516 if(size
!= csize
|| size
!= st
.st_size
|| ccrc
!= lcrc
)
519 if(ss
< 0 && ft
== 'd') {
520 s
= mkdir(file
, fmode
);
521 printf("mkdir %s\n", file
);
523 if((ss
< 0) && (ft
== 'b' || ft
== 'c' || ft
== 'p')) {
524 s
= mknod(file
, fmode
, maj
<< 8 | min
);
525 printf("mknod %s %lu %lu\n", file
, maj
, min
);
529 ss
= stat(file
, &st
);
532 if(st
.st_uid
!= uid
|| st
.st_gid
!= gid
) {
533 s
= chown(file
, uid
, gid
);
534 printf("chown %lu:%lu %s\n", uid
, gid
, file
);
536 if(st
.st_mode
!= fmode
) {
537 s
= chmod(file
, fmode
);
538 printf("chmod %04lo %s\n", fmode
, file
);
540 if(st
.st_mtime
!= mtime
) {
543 s
= utime(file
, &ut
);
544 printf("touch -m -t %s %s\n", ttime(mtime
), file
);
564 if(readline("Files: ", line2
, sizeof(line2
)) < 0)
571 fd
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
574 printf("Could not open local file %s. Error %s\n", name
, strerror(errno
));
578 s
= DOdata("NLST", files
, RETR
, fd
);
582 if(s
== 226 || s
== 250) {
583 fp
= fopen(name
, "r");
585 if(fp
== (FILE *)NULL
) {
586 printf("Unable to open file listing.\n");
589 while(fgets(line2
, sizeof(line2
), fp
) != (char *)NULL
) {
590 p
= line2
+ strlen(line2
) - 1;
591 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
592 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
593 printf("Retrieving file: %s\n", line2
); fflush(stdout
);
594 fd
= open(line2
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
596 printf("Unable to open local file %s\n", line2
);
598 s
= DOdata("RETR", line2
, RETR
, fd
);
612 char *file
, *remotefile
;
622 if(readline("Local File: ", line2
, sizeof(line2
)) < 0)
630 remotefile
= cmdargv
[2];
632 fd
= open(file
, O_RDONLY
);
635 printf("Could not open local file %s. Error %s\n", file
, strerror(errno
));
639 s
= DOdata("APPE", remotefile
, STOR
, fd
);
648 char *file
, *remotefile
;
658 if(readline("Local File: ", line2
, sizeof(line2
)) < 0)
666 remotefile
= cmdargv
[2];
668 fd
= open(file
, O_RDONLY
);
671 printf("Could not open local file %s. Error %s\n", file
, strerror(errno
));
675 s
= DOdata("STOR", remotefile
, STOR
, fd
);
684 char *file
, *remotefile
;
687 off_t filesize
, rmtsize
;
696 if(readline("Local File: ", line2
, sizeof(line2
)) < 0)
704 remotefile
= cmdargv
[2];
706 s
= DOcommand("SIZE", remotefile
);
711 rmtsize
= atol(reply
+4);
713 fd
= open(file
, O_RDONLY
);
716 printf("Could not open local file %s. Error %s\n", file
, strerror(errno
));
721 filesize
= asciisetsize(fd
, rmtsize
);
723 filesize
= lseek(fd
, rmtsize
, SEEK_SET
);
725 if(filesize
!= rmtsize
) {
726 printf("Could not set file start of %s\n", file
);
731 sprintf(restart
, "%u", rmtsize
);
733 s
= DOcommand("REST", restart
);
740 s
= DOdata("STOR", remotefile
, STOR
, fd
);
749 char *file
, *remotefile
;
759 if(readline("Local File: ", line2
, sizeof(line2
)) < 0)
767 remotefile
= cmdargv
[2];
769 fd
= open(file
, O_RDONLY
);
772 printf("Could not open local file %s. Error %s\n", file
, strerror(errno
));
776 s
= DOdata("STOU", remotefile
, STOR
, fd
);
797 if(readline("Files: ", line2
, sizeof(line2
)) < 0)
802 name
= dir(files
, 0);
804 fp
= fopen(name
, "r");
806 if(fp
== (FILE *)NULL
) {
807 printf("Unable to open listing file.\n");
811 while(fgets(line2
, sizeof(line2
), fp
) != (char *)NULL
) {
812 p
= line2
+ strlen(line2
) - 1;
813 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
814 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
815 printf("Sending file: %s\n", line2
); fflush(stdout
);
816 fd
= open(line2
, O_RDONLY
);
818 printf("Unable to open local file %s\n", line2
);
820 s
= DOdata("STOR", line2
, STOR
, fd
);
831 static int asciisize(fd
, filesize
)
841 while((cnt
= read(fd
, buffer
, sizeof(buffer
))) > 0) {
842 p
= buffer
; pp
= buffer
+ cnt
;
857 static off_t
asciisetsize(fd
, filesize
)
866 while(sp
< filesize
) {
867 s
= read(fd
, buffer
, 1);
894 fd
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
897 printf("Could not open local file %s. Error %s\n", name
, strerror(errno
));
901 s
= DOdata("NLST", files
, RETR
, fd
);
905 if(s
== 226 || s
== 250) {
906 fp
= fopen(name
, "r");
908 if(fp
== (FILE *)NULL
) {
909 printf("Unable to open file listing.\n");
912 while(fgets(line2
, sizeof(line2
), fp
) != (char *)NULL
) {
913 p
= line2
+ strlen(line2
) - 1;
914 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
915 if(p
>= line2
&& (*p
== '\r' || *p
== '\n')) *p
-- = '\0';
917 if(cloneit(line2
, 1)) {
918 printf("Retrieving file: %s\n", line2
); fflush(stdout
);
919 fd
= open(line2
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
921 printf("Unable to open local file %s\n", line2
);
923 s
= DOdata("RETR", line2
, RETR
, fd
);
927 s
= cloneit(line2
, 2);