1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | #include <unistd.h> |
22 | #include <errno(*__errno_location ()).h> |
23 | #include <string.h> |
24 | |
25 | #include <ftpconn.h> |
26 | #include "priv.h" |
27 | |
28 | static error_t |
29 | _cache_cwd (struct ftp_conn *conn, int reopen) |
30 | { |
31 | int reply; |
32 | const char *txt; |
33 | error_t err = |
34 | (reopen ? ftp_conn_cmd_reopen : ftp_conn_cmd) (conn, "pwd", 0, &reply, &txt); |
| |
35 | |
36 | if (! err) |
| |
| |
37 | { |
38 | if (reply == REPLY_DIR_NAME257) |
| 6 | | Assuming 'reply' is equal to 257 | |
|
| |
39 | { |
40 | char *cwd = malloc (strlen (txt)); |
| |
41 | if (! cwd) |
| 9 | | Assuming 'cwd' is non-null | |
|
| |
42 | err = ENOMEM((0x10 << 26) | ((12) & 0x3fff)); |
43 | else if (sscanf (txt, "\"%[^\"]\"", cwd) != 1) |
| |
44 | err = EGRATUITOUS((0x10 << 26) | ((105) & 0x3fff)); |
| 12 | | Within the expansion of the macro 'EGRATUITOUS':
|
a | Memory is never released; potential leak of memory pointed to by 'cwd' |
|
45 | else |
46 | { |
47 | if (conn->cwd) |
48 | free (conn->cwd); |
49 | conn->cwd = cwd; |
50 | } |
51 | } |
52 | else |
53 | err = unexpected_reply (conn, reply, txt, 0); |
54 | } |
55 | |
56 | return err; |
57 | } |
58 |
|
59 | |
60 | error_t |
61 | ftp_conn_get_cwd (struct ftp_conn *conn, char **cwd) |
62 | { |
63 | error_t err = 0; |
64 | if (! conn->cwd) |
| |
65 | err = _cache_cwd (conn, 1); |
| |
66 | if (! err) |
67 | { |
68 | *cwd = strdup (conn->cwd); |
69 | if (! *cwd) |
70 | err = ENOMEM((0x10 << 26) | ((12) & 0x3fff)); |
71 | } |
72 | return err; |
73 | } |
74 | |
75 | |
76 | error_t |
77 | ftp_conn_cwd (struct ftp_conn *conn, const char *cwd) |
78 | { |
79 | error_t err = 0; |
80 | if (conn->cwd && strcmp (conn->cwd, cwd) == 0) |
81 | err = 0; |
82 | else |
83 | { |
84 | int reply; |
85 | const char *txt; |
86 | err = ftp_conn_cmd_reopen (conn, "cwd", cwd, &reply, &txt); |
87 | if (! err) |
88 | { |
89 | if (reply == REPLY_FCMD_OK250) |
90 | err = _cache_cwd (conn, 0); |
91 | else |
92 | err = unexpected_reply (conn, reply, txt, ftp_conn_poss_file_errs); |
93 | } |
94 | } |
95 | return err; |
96 | } |
97 | |
98 | |
99 | error_t |
100 | ftp_conn_cdup (struct ftp_conn *conn) |
101 | { |
102 | int reply; |
103 | const char *txt; |
104 | error_t err = ftp_conn_cmd_reopen (conn, "cdup", 0, &reply, &txt); |
105 | if (! err) |
106 | { |
107 | if (reply == REPLY_OK200) |
108 | err = _cache_cwd (conn, 0); |
109 | else |
110 | err = unexpected_reply (conn, reply, txt, ftp_conn_poss_file_errs); |
111 | } |
112 | return err; |
113 | } |