summaryrefslogtreecommitdiffstats
path: root/lib/libftp/FtpIO.c
blob: b7f6af5dc6d132f30accb51d8376d1c476034a9e (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*		      Library for ftpd clients.(libftp)
			Copyright by Oleg Orel
			 All rights reserved.

This  library is desined  for  free,  non-commercial  software  creation.
It is changeable and can be improved. The author would greatly appreciate
any  advises, new  components  and  patches  of  the  exist
ing  programs.
Commercial  usage is  also  possible  with  participation of it's author.



*/

#include "FtpLibrary.h"
#include <unistd.h>

int FtpRead(FTP *con)
{
  int c;

  if ( con -> mode == 'I' )
    return FtpGetc(con,FTPDATA(con));

  if ( con->ch != EOF )
    {
      c=con->ch;
      con->ch=EOF;
      return c;
    }

  c=FtpGetc(con,FTPDATA(con));

  if ( c == Ctrl('M') )
    {
      c = FtpGetc(con,FTPDATA(con));

      if ( c == Ctrl('J') )
	return '\n';
      con->ch = c;
      return Ctrl('M');
    }
  return c;
}

int FtpWrite(FTP *ftp,char c)
{

  if ( ftp -> mode == 'I' || c != '\n' )
    return FtpPutc(ftp,FTPDATA(ftp),c);

  FtpPutc(ftp,FTPDATA(ftp),Ctrl('M'));
  return FtpPutc(ftp,FTPDATA(ftp),Ctrl('J'));
}


int FtpGetc(FTP *ftp,FILE *fp)
{
  fd_set fds;
  char c;

  FD_ZERO(&fds);
  FD_SET(fileno(fp),&fds);

  if (select(getdtablesize(), &fds, 0, 0, &(ftp->timeout))<1)
    return EXIT(ftp,QUIT);

  if (read(fileno(fp),&c,1)<1)
    return EOF;

  if (ftp->hash!=NULL) (*ftp->hash)(ftp,1);

  return (int)c;
}


STATUS FtpPutc(FTP *ftp,FILE *fp,char c)
{
  fd_set fds;

  FD_ZERO(&fds);
  FD_SET(fileno(fp),&fds);

  if (select(getdtablesize(), 0, &fds, 0, &(ftp->timeout))<1)
    return EXIT(ftp,QUIT);

  if (write(fileno(fp),&c,1)!=1)
    return EXIT(ftp,QUIT);

  if (ftp->hash!=NULL) (*ftp->hash)(ftp,1);
  return 0;
}


STATUS FtpReadBlock(FTP *ftp, char *buffer, int size)
{
  fd_set fds;
  register int rsize;

  FD_ZERO(&fds);
  FD_SET(fileno(FTPDATA(ftp)),&fds);

  if (select(getdtablesize(), &fds,0, 0, &(ftp->timeout))<1)
    return EXIT(ftp,QUIT);


  if ((rsize=read(fileno(FTPDATA(ftp)),buffer,size))<0)
    return EXIT(ftp,QUIT);

  if (ftp->hash!=NULL && rsize!=0) (*ftp->hash)(ftp,rsize);

  if (ftp->mode == 'A')
    {
      char buffer2[size];
      register int i,ii;

      for (i=0,ii=0;i<rsize;i++,ii++)
	if (buffer[i]==Ctrl('M')&&buffer[i+1]==Ctrl('J'))
	  buffer2[ii]='\n',i++;
	else
	  buffer2[ii]=buffer[i];

      rsize=ii;
      bcopy(buffer2,buffer,rsize);
    }
  return rsize;
}


STATUS FtpWriteBlock(FTP *ftp, char *buffer, int size)
{
  fd_set fds;
  register int wsize;
  char buffer2[size*2];

  FD_ZERO(&fds);
  FD_SET(fileno(FTPDATA(ftp)),&fds);

  if (select(getdtablesize(), 0, &fds, 0, &(ftp->timeout))<1)
    return EXIT(ftp,QUIT);


  if (ftp->mode=='A')
    {
      register int i,ii;

      for(i=0,ii=0;i<size;i++,ii++)
	if (buffer[i]=='\n')
	  buffer2[ii++]=Ctrl('M'),buffer2[ii]=Ctrl('J');
	else
	  buffer2[ii]=buffer[i];
      buffer=buffer2;
      size=ii;
    }

  if ((wsize=write(fileno(FTPDATA(ftp)),buffer,size))!=size)
    return EXIT(ftp,QUIT);

  if ( ftp->hash!=NULL && wsize!=0 ) (*ftp->hash)(ftp,wsize);

  return wsize;
}












OpenPOWER on IntegriCloud