Develop and Download Open Source Software

Browse Subversion Repository

Contents of /branches/RB-1.1/ftpwd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (show annotations) (download) (as text)
Mon Feb 23 02:22:49 2009 UTC (15 years ago) by hirohitohigashi
File MIME type: text/x-csrc
File size: 2898 byte(s)
Creating relase branch for 1.1
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
38 /***
39 ftp_dir:
40 - int sck => Socket Descriptor
41 - int verbose => Verbose?
42 . 0 Do not Print
43 . 1 Print
44 - ret => Success?
45 . 0 Success
46 .-1 Error
47 ***/
48
49
50 int ftp_dir(int sck, int verbose)
51 {
52 char *buf;
53 int n = 0;
54 struct timeval tm; //tv_sec - tv_usec
55 fd_set readfds;
56
57
58 buf = (char *)malloc(1024*sizeof(char));
59 memset(buf, 0x0, 1024);
60
61
62 if (write(sck, "PWD\n", strlen("PWD\n")) == -1) {
63
64 if (verbose) {
65
66 if (errno == EBADF) {
67 free(buf);
68 fprintf(stderr, "Socket Descriptor Not Valid!\n");
69
70 return -1;
71 } else if (errno == EIO) {
72 free(buf);
73 fprintf(stderr, "I/O Error!\n");
74
75 return -1;
76 } else if (errno == EINTR) {
77 free(buf);
78 fprintf(stderr, "Signal Interrupted The write() Function\n");
79
80 return -1;
81 }
82
83 } else {
84
85 if (errno == EBADF) {
86 free(buf);
87
88 return -1;
89 } else if (errno == EIO) {
90 free(buf);
91
92 return -1;
93 } else if (errno == EINTR) {
94 free(buf);
95
96 return -1;
97 }
98
99 }
100
101 free(buf);
102
103 return -1;
104 }
105
106 tm.tv_sec = 3;
107 tm.tv_usec = 0;
108
109 FD_ZERO(&readfds);
110
111 FD_SET(sck, &readfds);
112
113 if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) {
114 if (verbose) {
115 perror("select()");
116 }
117 free(buf);
118
119 return -1;
120 } else {
121
122 if (FD_ISSET(sck, &readfds)) {
123
124 if ( (n = recv(sck, buf, 1022, 0)) < 0) {
125 if (verbose) {
126 perror("recv()");
127 }
128 free(buf);
129
130 return -1;
131 }
132
133 if (verbose) {
134 printf("[Server] %s\n", buf);
135 }
136 }
137 else {
138 if (verbose) {
139 printf("[-] Timeout\n");
140 }
141 return -1;
142 }
143 }
144
145 buf[n] = '\0';
146
147 if (ftp_path(buf) != 0) {
148
149 free(buf);
150 return -1;
151 }
152
153 memset(buf, 0x0, 1024);
154 free(buf);
155
156
157 return 0; //Done! Path is now setted!
158
159 }
160

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