summaryrefslogtreecommitdiff
path: root/libftpconn/set-type.c
blob: 6c500ce60c4a7feabb632ac67610601c97c1bbfb (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
/* Set connection type

   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"

/* Set the ftp connection type of CONN to TYPE, or return an error.  */
error_t
ftp_conn_set_type (struct ftp_conn *conn, const char *type)
{
  error_t err = 0;

  if (! type)
    return EINVAL;

  if (!conn->type || strcmp (type, conn->type) != 0)
    {
      type = strdup (type);
      if (! type)
	err = ENOMEM;
      else
	{
	  int reply;
	  error_t err = ftp_conn_cmd (conn, "type", type, &reply, 0);

	  if (!err && reply != REPLY_OK && reply != REPLY_CLOSED)
	    err = unexpected_reply (conn, reply, 0, 0);

	  if (!err || err == EPIPE)
	    {
	      if (conn->type)
		free ((char *)conn->type);
	      conn->type = type;
	    }
	}
    }    

  return err;
}