summaryrefslogtreecommitdiffstats
path: root/lib/libftp/Ftpfopen.c
blob: b7b96d59f56db274670d596bd1d6e096c48ad9f0 (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
/*
		      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  existing  programs.
Commercial  usage is  also  possible  with  participation of it's author.



*/

#include <FtpLibrary.h>

#define NFSD 256

static int fds_types[NFDS];
static int init=0;

enum {T_EMPTY=0,T_FILE,T_STREAM,T_PIPE,T_FULL};

FILE *Ftpfopen(char *filename,char *mode)
{
  FILE *fp;
  
  if (!init)
    { 
      bzero(fds_types,NFDS*sizeof(fds_types[0]));
      init=1;
    }

  if (!strcmp(filename,"*STDIN*") || (!strcmp(filename,"-") && (mode[0]=='r')) )
    {
      fds_types[fileno(stdin)]=T_STREAM;
      return stdin;
    }
  
  if (!strcmp(filename,"*STDOUT*") || (!strcmp(filename,"-") && (mode[0]=='w')))
    {
      fds_types[fileno(stdout)]=T_STREAM;
      return stdout;
    }
  
  if (strcmp(filename,"*STDERR*")==0)
    {
      fds_types[fileno(stderr)]=T_STREAM;
      return stderr;
    }
  


  if (*filename=='|') 
    {
      fp=popen(filename+1,mode);
      if (fp==NULL) return fp;
      fds_types[fileno(fp)]=T_PIPE;
      return fp;
    }

  fp=FtpFullOpen(filename,mode);
  if (fp==NULL) return fp;
  fds_types[fileno(fp)]=T_FILE;
  return fp;
  
}

int Ftpfclose(FILE *fp)
{

  if (!init)
    { 
      bzero(fds_types,NFDS*sizeof(fds_types[0]));
      init=1;
    }

  switch (fds_types[fileno(fp)])
    {
    
    case T_FILE:
      
      return FtpFullClose(fp);
      
    case T_STREAM:

      return fflush(fp);
      
    case T_PIPE:
      
      return pclose(fp);
      
    default:

      return -1;
    }

}
OpenPOWER on IntegriCloud