summaryrefslogtreecommitdiff
path: root/libftpconn/cwd.c
blob: 89156a582ec9c3e0e8c3045046ae60df222380a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* Get/set connection current working directory

   Copyright (C) 1997 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program 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 GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <ftpconn.h>
#include "priv.h"

static error_t
_cache_cwd (struct ftp_conn *conn, int reopen)
{
  int reply;
  const char *txt;
  error_t err =
    (reopen ? ftp_conn_cmd_reopen : ftp_conn_cmd) (conn, "pwd", 0, &reply, &txt);

  if (! err)
    if (reply == REPLY_DIR_NAME)
      {
	char *cwd = malloc (strlen (txt));
	if (! cwd)
	  err = ENOMEM;
	else if (sscanf (txt, "\"%[^\"]\"", cwd) != 1)
	  err = EGRATUITOUS;
	else
	  {
	    if (conn->cwd)
	      free (conn->cwd);
	    conn->cwd = cwd;
	  }
      }
    else
      err = unexpected_reply (conn, reply, txt, 0);

  return err;
}

/* Return a malloced string containing CONN's working directory in CWD.  */
error_t
ftp_conn_get_cwd (struct ftp_conn *conn, char **cwd)
{
  error_t err = 0;
  if (! conn->cwd)
    err = _cache_cwd (conn, 1);
  if (! err)
    {
      *cwd = strdup (conn->cwd);
      if (! *cwd)
	err = ENOMEM;
    }
  return err;
}

/* Return a malloced string containing CONN's working directory in CWD.  */
error_t
ftp_conn_cwd (struct ftp_conn *conn, const char *cwd)
{
  error_t err = 0;
  if (conn->cwd && strcmp (conn->cwd, cwd) == 0)
    err = 0;
  else
    {
      int reply;
      const char *txt;
      err = ftp_conn_cmd_reopen (conn, "cwd", cwd, &reply, &txt);
      if (! err)
	if (reply == REPLY_FCMD_OK)
	  err = _cache_cwd (conn, 0);
	else
	  err = unexpected_reply (conn, reply, txt, ftp_conn_poss_file_errs);
    }
  return err;
}

/* Return a malloced string containing CONN's working directory in CWD.  */
error_t
ftp_conn_cdup (struct ftp_conn *conn)
{
  int reply;
  const char *txt;
  error_t err = ftp_conn_cmd_reopen (conn, "cdup", 0, &reply, &txt);
  if (! err)
    if (reply == REPLY_OK)
      err = _cache_cwd (conn, 0);
    else
      err = unexpected_reply (conn, reply, txt, ftp_conn_poss_file_errs);
  return err;
}