11 #include "dprints.h" /* Debug printing & function prototypes*/
12 #include "gribfuncs.h" /* function prototypes */
15 ********************************************************************
16 * A. FUNCTION: FTP_getfile
17 * builds and executes a Bourne script file to retreive
18 * the file specified from remote site via Ftp call;
19 * Execute script to establish ftp session (under Userid 'anonymous'
20 * & passwd 'gribsimp22'):
21 * Host info is retrieved from file "$pathnm/tables.cfg" whose content
22 * is a one line entry= "eifel.nrlmry.navy.mil receive/GRIB_TABLES"
25 * int FTP_getfile (filenm, loc_pathnm, errmsg)
27 * ARGUMENTS (I=input, O=output, I&O=input and output):
28 * (I) char *filenm; Name of file to ftp
29 * (I) char *loc_pathnm; Full path leading to config file 'tables.cfg'
30 * (O) char *errmsg; Empty array, Returns filled if error occurs;
33 * 0> sucessfully ftp-ed;
34 * 1> error: create script/ftp err/missing table.cfg;
35 ********************************************************************
39 int FTP_getfile (char *filenm
, char *loc_pathnm
, char *errmsg
)
42 int FTP_getfile (filenm
, loc_pathnm
, errmsg
)
48 FILE *f1
=NULL
, *f2
=NULL
;
49 char *func
="FTP_getfile";
51 char hostnm
[100]; /* name of remote site */
52 char usernm
[100]; /* using anonymous */
53 char passwd
[100]; /* anonymous */
54 char pathnm
[100]; /* full path of remote file to get */
55 int stat
; /* return status */
56 int n
; /* working var */
58 DPRINT3 ("Entering %s (%s/%s)\n", func
, loc_pathnm
, filenm
);
61 * A.1 SET up name of local config file !$local_path/tables.cfg
62 * IF (unable to open config file)
63 * RETURN 1 !errmsg filled
67 /* USE SAME CONFIG FILE --
68 no matter if dnloading "g1tab* , or neon2gr*, or orig_ctr"
70 sprintf (filename
, "%s/tables.cfg", loc_pathnm
);
71 DPRINT1 ("Read Remote host info from '%s'\n", filename
);
72 if ((f1
=fopen (filename
, "r"))==NULL
) {
73 sprintf(errmsg
,"%s: failed to open '%s' for reading;\n",func
,filename
);
78 * A.2 READ hostname and remote pathname from file, then close it;
79 * !config entry-> "eifel.nrlmry.navy.mil receive/GRIB_TABLES"
81 * A.3 CLOSE config file;
83 n
= fscanf (f1
, "%s%s", hostnm
, pathnm
);
84 fclose(f1
); /* close Config File */
87 * A.4 IF (read failed) RETURN 1 !errmsg filled;
90 sprintf(errmsg
,"%s: Fail to read 2 args from '%s'\n", func
, filename
);
96 * A.6 SET password to "gribsimp22", userid to "anonymous"
98 strcpy (passwd
, "gribsimp22");
100 /* Ready to build Bourne script: */
103 * A.7 IF (create temp script file fails)
104 * RETURN 1 !errmsg filled
107 if ((f1
=fopen ("temp_ftp_script","w"))==NULL
) {
108 sprintf(errmsg
,"%s: failed to build FTP script\n", func
);
113 * A.8 CREATE ftp script to download Host's "receive/GRIB_TABLES/$fn"
114 * to $localPath/$fn locally;
116 * A.9 CLOSE temp file
119 "#!/bin/sh\nexec 1>&-;exec 2>&-\nftp -in %s << STOP\n" \
120 "user anonymous %s\ncd %s\nlcd %s\nget %s\nquit\n" \
122 hostnm
, passwd
, pathnm
, loc_pathnm
, filenm
);
125 DPRINT5 ("execute ftp script: \n"
126 " #!/bin/sh\n exec 1>&-;exec 2>&-\n"
127 " ftp -in %s << STOP\n user anonymous %s\n"
128 " cd %s\n lcd %s\n get %s\n quit\n STOP\n exit\n",
129 hostnm
, passwd
, pathnm
, loc_pathnm
, filenm
);
132 * A.10 EXECUTE script to download lookup file
134 * A.11 REMOVE temp script
136 fprintf(stdout
,"Attempting to get remote '%s'\n", filenm
);
137 n
= system ("chmod 755 temp_ftp_script;temp_ftp_script");
138 unlink ("temp_ftp_script");
142 * A.12 IF (execute script failed)
143 * RETURN 1 !errmsg filled
146 if (n
!=0) { /* ck Stat of Systm call */
147 sprintf(errmsg
,"%s: system call to ftp failed\n", func
);
153 * A.13 CHECK if ftp-ed file is available & readable
155 * RETURN 1 !errmsg filled
158 sprintf (filename
, "%s/%s", loc_pathnm
, filenm
);
159 if ((f2
= fopen(filename
, "rb+"))==NULL
) {
160 sprintf(errmsg
,"%s: '%s' not avail on %s in %s\n\n", func
,
161 filenm
, hostnm
, pathnm
);
165 DPRINT0("file downloaded successfully\n");
171 * A.14 CLOSE up ftp-ed file
176 * A.15 RETURN 0 !success
178 DPRINT2 ("Leaving %s, Stat=%d\n", func
, stat
);