首页下载资源后端基于c语言封装的ftp客户端开源库

ZIP基于c语言封装的ftp客户端开源库

qq_739244659.62KB需要积分:1

资源文件列表:

ftp开源库.zip 大约有2个文件
  1. ftp开源库/ftplib.c 30.13KB
  2. ftp开源库/ftplib.h 4.9KB

资源介绍:

ftp
/***************************************************************************/ /* */ /* ftplib.c - callable ftp access routines */ /* Copyright (C) 1996-2001, 2013, 2016 Thomas Pfau, tfpfau@gmail.com */ /* 1407 Thomas Ave, North Brunswick, NJ, 08902 */ /* */ /* This library is free software. You can redistribute it and/or */ /* modify it under the terms of the Artistic License 2.0. */ /* */ /* This library is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ /* Artistic License 2.0 for more details. */ /* */ /* See the file LICENSE or */ /* http://www.perlfoundation.org/artistic_license_2_0 */ /* */ /***************************************************************************/ #if defined(__unix__) || defined(__VMS) #include #endif #if defined(_WIN32) #include #endif #include #include #include #include #include #if defined(__unix__) #include #include #include #include #include #include #elif defined(VMS) #include #include #include #include #include #elif defined(_WIN32) #include #endif #if defined(__APPLE__) #undef _REENTRANT #endif #define BUILDING_LIBRARY #include "ftplib.h" #if defined(__UINT64_MAX) && !defined(PRIu64) #if ULONG_MAX == __UINT32_MAX #define PRIu64 "llu" #else #define PRIu64 "lu" #endif #endif #if defined(_WIN32) #define SETSOCKOPT_OPTVAL_TYPE (const char *) #else #define SETSOCKOPT_OPTVAL_TYPE (void *) #endif #define FTPLIB_BUFSIZ 8192 #define RESPONSE_BUFSIZ 1024 #define TMP_BUFSIZ 1024 #define ACCEPT_TIMEOUT 30 #define FTPLIB_CONTROL 0 #define FTPLIB_READ 1 #define FTPLIB_WRITE 2 #if !defined FTPLIB_DEFMODE #define FTPLIB_DEFMODE FTPLIB_PASSIVE #endif struct NetBuf { char *cput,*cget; int handle; // 用于通讯的socket int cavail,cleft; char *buf; int dir; netbuf *ctrl; netbuf *data; int cmode; struct timeval idletime; FtpCallback idlecb; void *idlearg; unsigned long int xfered; unsigned long int cbbytes; unsigned long int xfered1; char response[RESPONSE_BUFSIZ]; }; static char *version = (char*)"ftplib Release 4.0 07-Jun-2013, copyright 1996-2003, 2013 Thomas Pfau"; GLOBALDEF int ftplib_debug = 0; #if defined(__unix__) || defined(VMS) int net_read(int fd, char *buf, size_t len) { while ( 1 ) { int c = read(fd, buf, len); if ( c == -1 ) { if ( errno != EINTR && errno != EAGAIN ) return -1; } else { return c; } } } int net_write(int fd, const char *buf, size_t len) { int done = 0; while ( len > 0 ) { int c = write( fd, buf, len ); if ( c == -1 ) { if ( errno != EINTR && errno != EAGAIN ) return -1; } else if ( c == 0 ) { return done; } else { buf += c; done += c; len -= c; } } return done; } #define net_close close #elif defined(_WIN32) #define net_read(x,y,z) recv(x,y,z,0) #define net_write(x,y,z) send(x,y,z,0) #define net_close closesocket #endif #if defined(NEED_MEMCCPY) /* * VAX C does not supply a memccpy routine so I provide my own */ void *memccpy(void *dest, const void *src, int c, size_t n) { int i=0; const unsigned char *ip=src; unsigned char *op=dest; while (i < n) { if ((*op++ = *ip++) == c) break; i++; } if (i == n) return NULL; return op; } #endif #if defined(NEED_STRDUP) /* * strdup - return a malloc'ed copy of a string */ char *strdup(const char *src) { int l = strlen(src) + 1; char *dst = malloc(l); if (dst) strcpy(dst,src); return dst; } #endif /* * socket_wait - wait for socket to receive or flush data * * return 1 if no user callback, otherwise, return value returned by * user callback */ static int socket_wait(netbuf *ctl) { fd_set fd,*rfd = NULL,*wfd = NULL; struct timeval tv; int rv = 0; if ((ctl->dir == FTPLIB_CONTROL) || (ctl->idlecb == NULL)) return 1; if (ctl->dir == FTPLIB_WRITE) wfd = &fd; else rfd = &fd; FD_ZERO(&fd); do { FD_SET(ctl->handle,&fd); tv = ctl->idletime; rv = select(ctl->handle+1, rfd, wfd, NULL, &tv); if (rv == -1) { rv = 0; strncpy(ctl->ctrl->response, strerror(errno), sizeof(ctl->ctrl->response)); break; } else if (rv > 0) { rv = 1; break; } } while ((rv = ctl->idlecb(ctl, ctl->xfered, ctl->idlearg))); return rv; } /* * read a line of text * * return -1 on error or bytecount */ static int readline(char *buf,int max,netbuf *ctl) { int x,retval = 0; char *end,*bp=buf; int eof = 0; if ((ctl->dir != FTPLIB_CONTROL) && (ctl->dir != FTPLIB_READ)) return -1; if (max == 0) return 0; do { if (ctl->cavail > 0) { x = (max >= ctl->cavail) ? ctl->cavail : max-1; end = memccpy(bp,ctl->cget,'\n',x); if (end != NULL) x = end - bp; retval += x; bp += x; *bp = '\0'; max -= x; ctl->cget += x; ctl->cavail -= x; if (end != NULL) { bp -= 2; if (strcmp(bp,"\r\n") == 0) { *bp++ = '\n'; *bp++ = '\0'; --retval; } break; } } if (max == 1) { *buf = '\0'; break; } if (ctl->cput == ctl->cget) { ctl->cput = ctl->cget = ctl->buf; ctl->cavail = 0; ctl->cleft = FTPLIB_BUFSIZ; } if (eof) { if (retval == 0) retval = -1; break; } if (!socket_wait(ctl)) return retval; if ((x = net_read(ctl->handle,ctl->cput,ctl->cleft)) == -1) { if (ftplib_debug) perror("read"); retval = -1; break; } if (x == 0) eof = 1; ctl->cleft -= x; ctl->cavail += x; ctl->cput += x; } while (1); return retval; } /* * write lines of text * * return -1 on error or bytecount */ static int writeline(const char *buf, int len, netbuf *nData) { int x, nb=0, w; const char *ubp = buf; char *nbp; char lc=0; if (nData->dir != FTPLIB_WRITE) return -1; nbp = nData->buf; for (x=0; x < len; x++) { if ((*ubp == '\n') && (lc != '\r')) { if (nb == FTPLIB_BUFSIZ) { if (!socket_wait(nData)) return x; w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ); if (w != FTPLIB_BUFSIZ) { if (ftplib_debug) printf("net_write(1) returned %d, errno = %d\n", w, errno); return(-1); } nb = 0; } nbp[nb++] = '\r'; } if (nb == FTPLIB_BUFSIZ) { if (!socket_wait(nData)) return x; w = net_write(nData->handle, nbp, FTPLIB_BUFSIZ); if (w != FTPLIB_BUFSIZ) { if (ftplib_debug) printf("net_write(2) returned %d, errno = %d\n", w, errno); return(-1); } nb = 0; } nbp[nb++] = lc = *ubp++; } if (nb) { if (!socket_wait(nData)) return x; w = net_write(nData->handle, nbp, nb); if (w != nb) { if (ftplib_debug) printf("net_write(3) returned %d, errno = %d\n", w, errno); return(-1); } } return len; } /* * read a response from the server * * return 0 if first char doesn't match * return 1 if first char matches */ static int readresp(char c, netbuf *nControl) { char match[5]; if (readline(nControl->response,RESPONSE_BUFSIZ,nControl) == -1) { if (ftplib_debug) perror("Control socket read failed"); return 0; } if (ftplib_debug > 1) fprintf(stderr,"%s",nControl->response); if (nControl->response[3] == '-') { strncpy(match,nControl->response,3); match[3] = ' '; match[4] = '\0'; do { if (readline(nControl->response,RESPONSE_BUFSIZ,nControl) == -1) { if (ftplib_debug) perror("Control soc
100+评论
captcha