Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk_1/ftpcwd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (hide annotations) (download) (as text)
Mon Feb 23 02:05:06 2009 UTC (15 years ago) by hirohitohigashi
Original Path: trunk/ftpcwd.c
File MIME type: text/x-csrc
File size: 3131 byte(s)
changed the terminator of the ftp command from CR to CRLF.
and solved race condition in ftp_get().

1 hirohitohigashi 1 /*
2    
3     libftp 1.0 (stable): this is an FTP library to simplify the work to a Developer
4     who want to work with FTP servers (RFC 959).
5     Copyright (C) 2007/2008 omnipresent - omnipresent[at]email.it
6    
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18     along with this program. If not, see <http://www.gnu.org/licenses/>.
19    
20     */
21    
22     #include <stdio.h>
23     #include <string.h>
24     #include <errno.h>
25     #include <stdlib.h>
26     #include <unistd.h>
27     #include <sys/types.h>
28     #include <sys/time.h>
29     #include <sys/socket.h>
30     #include <sys/stat.h>
31     #include <netinet/in.h>
32     #include <netdb.h>
33     #include <fcntl.h>
34    
35     #include "ftp_main.h"
36    
37     #define DIR_TOO_LONG 2
38     #define NAME_TOO_LONG 2
39    
40     /***
41     ftp_cwd:
42     - int sck => Socket Descriptor
43     - int verbose => Verbose?
44     . 0 Do not Print
45     . 1 Print
46     - ret => Success?
47     . 0 Success
48     .-1 Error
49     . 2 Error
50     ***/
51    
52    
53    
54    
55 hirohitohigashi 7 int ftp_cwd (int sck, char *Directory, int verbose)
56     {
57 hirohitohigashi 1
58 hirohitohigashi 7 char buffer[1024]={'\0'};
59     char *buf;
60     int n = 0;
61     struct timeval tm; //tv_sec - tv_usec
62     fd_set readfds;
63 hirohitohigashi 1
64    
65 hirohitohigashi 7 buf = (char *)malloc(1024*sizeof(char));
66 hirohitohigashi 1
67 hirohitohigashi 7 memset(buffer, 0x0, 1024);
68     memset(buf, 0x0, 1024);
69    
70     if ( strlen(Directory) > 1015 ) {
71 hirohitohigashi 1 free(buf);
72    
73     return DIR_TOO_LONG;
74 hirohitohigashi 7 }
75 hirohitohigashi 1
76    
77 hirohitohigashi 11 sprintf(buffer, "CWD %s\r\n", Directory);
78 hirohitohigashi 1
79    
80 hirohitohigashi 7 if (write(sck, buffer, strlen(buffer)) == -1) {
81 hirohitohigashi 1
82     if (verbose) {
83    
84 hirohitohigashi 7 if (errno == EBADF) {
85 hirohitohigashi 1 fprintf(stderr, "Socket Descriptor Not Valid!\n");
86     free(buf);
87    
88     return -1;
89 hirohitohigashi 7 } else if (errno == EIO) {
90 hirohitohigashi 1 fprintf(stderr, "I/O Error!\n");
91     free(buf);
92    
93     return -1;
94 hirohitohigashi 7 } else if (errno == EINTR) {
95 hirohitohigashi 1 fprintf(stderr, "Signal Interrupted The write() Function\n");
96     free(buf);
97    
98     return -1;
99 hirohitohigashi 7 }
100 hirohitohigashi 1
101     } else {
102    
103 hirohitohigashi 7 if (errno == EBADF) {
104 hirohitohigashi 1 free(buf);
105    
106     return -1;
107 hirohitohigashi 7 } else if (errno == EIO) {
108 hirohitohigashi 1 free(buf);
109    
110     return -1;
111 hirohitohigashi 7 } else if (errno == EINTR) {
112 hirohitohigashi 1 free(buf);
113    
114     return -1;
115 hirohitohigashi 7 }
116 hirohitohigashi 1
117     }
118    
119 hirohitohigashi 7 free(buf);
120 hirohitohigashi 1
121 hirohitohigashi 7 return -1;
122 hirohitohigashi 1
123 hirohitohigashi 7 }
124 hirohitohigashi 1
125 hirohitohigashi 7 sleep(1); //wait..
126     tm.tv_sec = 3;
127     tm.tv_usec = 0;
128 hirohitohigashi 1
129 hirohitohigashi 7 FD_ZERO(&readfds);
130 hirohitohigashi 1
131 hirohitohigashi 7 FD_SET(sck, &readfds);
132 hirohitohigashi 1
133 hirohitohigashi 7 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
134     if (verbose) {
135     perror("select()");
136     }
137     free(buf);
138    
139     return -1;
140     } else {
141    
142     if (FD_ISSET(sck, &readfds)) {
143    
144     if ( (n = recv(sck, buf, 1022, 0)) < 0) {
145 hirohitohigashi 1 if (verbose) {
146 hirohitohigashi 7 perror("recv()");
147 hirohitohigashi 1 }
148     free(buf);
149    
150     return -1;
151 hirohitohigashi 7 }
152 hirohitohigashi 1
153 hirohitohigashi 7 if (verbose)
154     printf("[Server] %s\n", buf);
155 hirohitohigashi 1
156 hirohitohigashi 7 }
157     else {
158     if (verbose)
159     printf("[-] Timeout\n");
160 hirohitohigashi 1
161 hirohitohigashi 7 return -1;
162 hirohitohigashi 1 }
163 hirohitohigashi 7 }
164 hirohitohigashi 1
165 hirohitohigashi 7 buf[n] = '\0';
166 hirohitohigashi 1
167 hirohitohigashi 7 if (ftp_dir_handler(buf, 0) != 0) {
168     free(buf);
169 hirohitohigashi 1
170 hirohitohigashi 7 return -1; //Error
171     }
172 hirohitohigashi 1
173 hirohitohigashi 7 free(buf);
174 hirohitohigashi 1
175 hirohitohigashi 7 return 0;
176 hirohitohigashi 1
177     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26