1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1990-2011 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Eclipse Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.eclipse.org/org/documents/epl-v10.html *
11 * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * *
19 ***********************************************************************/
20 /*
21 * File: ifs_gopher.c
22 */
23
24 #include "ifs_agent.h"
25 #include <stdio.h>
26
27 struct {
28 int version;
29 } gopher_data;
30
31 int
GopherXfer(srv,rpath,tmpfile)32 GopherXfer( srv, rpath, tmpfile )
33 struct server_info *srv;
34 char *rpath;
35 char *tmpfile;
36 {
37 struct mount_item *mitem = srv->mitem;
38 NetFile *nFile;
39 FILE *fout;
40 char buf[ STRLEN ];
41 int port, len;
42
43 if( (port = mitem->port) <= 0 )
44 port = 70;
45 if( (nFile = NetConnect( srv, mitem->host, port )) == NULL ) {
46 logit( "<gopher>: connect failed!\n" );
47 cserrno = E_CONNECT;
48 return -1;
49 }
50 NetWrite( nFile, rpath, strlen(rpath) );
51 logit( rpath );
52
53 if( NetGets( nFile, buf, sizeof(buf) ) == NULL ||
54 strmatch( buf, "*\terror.host\t*" ) ) {
55 /* file/directory not found */
56 logit( buf );
57 cserrno = E_CONNECT;
58 NetClose( nFile );
59 return -1;
60 }
61 logit( buf );
62
63 if( (fout = fopen( tmpfile, "w" )) == NULL ) {
64 cserrno = E_OPENDEST;
65 NetClose( nFile );
66 return -1;
67 }
68 fprintf( fout, "%s", buf );
69 while( (len = NetRead( nFile, buf, sizeof(buf) )) > 0 ) {
70 fwrite( buf, 1, len, fout );
71 }
72 fclose( fout );
73 NetClose( nFile );
74 return 0;
75 }
76
77 int
GopherXferFile(srv,tmpfile)78 GopherXferFile( srv, tmpfile )
79 struct server_info *srv;
80 char *tmpfile;
81 {
82 char buf[ STRLEN ];
83
84 sfsprintf( buf, sizeof(buf), "0%s\r\n", srv->rpath );
85 if( GopherXfer( srv, buf, tmpfile ) == -1 )
86 return -1;
87 MakePath( srv->lpath );
88 rename( tmpfile, srv->lpath );
89 return 0;
90 }
91
92 int
GopherXferDir(srv,tmpfile)93 GopherXferDir( srv, tmpfile )
94 struct server_info *srv;
95 char *tmpfile;
96 {
97 char buf[ STRLEN ];
98
99 sfsprintf( buf, sizeof(buf), "1%s\r\n", srv->rpath );
100 if( GopherXfer( srv, buf, tmpfile ) == -1 )
101 return -1;
102 sfsprintf( buf, sizeof(buf), "%s/._dir", srv->lpath );
103 MakePath( buf );
104 chdir( srv->lpath );
105 rename( tmpfile, "._dir" );
106 symlink( srv->mitem->timeout, "._cache_time" );
107 return 0;
108 }
109
110 int
GopherGetFile(srv)111 GopherGetFile( srv )
112 struct server_info *srv;
113 {
114 char tmpfile[ STRLEN ];
115 char *lpath = srv->lpath;
116 char *rpath = srv->rpath;
117 int ans;
118
119 MakeTmpFile( lpath, tmpfile, sizeof(tmpfile) );
120 if( *rpath == '\0' || DashD( lpath ) ) {
121 ans = GopherXferDir( srv, tmpfile );
122 } else if( DashF( lpath ) ) {
123 ans = GopherXferFile( srv, tmpfile );
124 } else {
125 if( (ans = GopherXferDir( srv, tmpfile )) == -1 ) {
126 ans = GopherXferFile( srv, tmpfile );
127 }
128 }
129 unlink( tmpfile );
130 return ans;
131 }
132
133 int
GopherNop()134 GopherNop()
135 {
136 return 0;
137 }
138
139 int
GopherInit(tbl)140 GopherInit( tbl )
141 struct agent_item *tbl;
142 {
143 tbl->localdata = (char *) &gopher_data;
144 tbl->connect = GopherNop;
145 tbl->disconnect = GopherNop;
146 tbl->listdents = GopherGetFile;
147 tbl->getfile = GopherGetFile;
148 tbl->putfile = GopherNop;
149 tbl->userdef = GopherNop;
150 return 0;
151 }
152
153