Merge remote-tracking branch 'origin/release-v4.6.1'
[WRF.git] / external / io_grib1 / MEL_grib1 / FTP_getfile.c
blob08b08209555a9a9184b81562c7d29c159be12684
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #if defined(_WIN32)
6 #include <io.h>
7 #else
8 #include <unistd.h>
9 #endif
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"
24 * INTERFACE:
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;
32 * RETURN CODE:
33 * 0> sucessfully ftp-ed;
34 * 1> error: create script/ftp err/missing table.cfg;
35 ********************************************************************
38 #if PROTOTYPE_NEEDED
39 int FTP_getfile (char *filenm, char *loc_pathnm, char *errmsg)
41 #else
42 int FTP_getfile (filenm, loc_pathnm, errmsg)
43 char *filenm;
44 char *loc_pathnm;
45 char *errmsg;
46 #endif
48 FILE *f1=NULL, *f2=NULL;
49 char *func="FTP_getfile";
50 char filename[200];
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
64 * ENDIF
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);
74 stat=(1); goto BYE;
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;
89 if (n != 2) {
90 sprintf(errmsg,"%s: Fail to read 2 args from '%s'\n", func, filename);
91 stat=(1); goto BYE;
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
105 * ENDIF
107 if ((f1=fopen ("temp_ftp_script","w"))==NULL) {
108 sprintf(errmsg,"%s: failed to build FTP script\n", func);
109 stat=(1); goto BYE;
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
118 fprintf (f1,
119 "#!/bin/sh\nexec 1>&-;exec 2>&-\nftp -in %s << STOP\n" \
120 "user anonymous %s\ncd %s\nlcd %s\nget %s\nquit\n" \
121 "STOP\nexit\n",
122 hostnm, passwd, pathnm, loc_pathnm, filenm);
123 fclose(f1);
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
144 * ENDIF
146 if (n!=0) { /* ck Stat of Systm call */
147 sprintf(errmsg,"%s: system call to ftp failed\n", func);
148 stat=(1); goto BYE;
153 * A.13 CHECK if ftp-ed file is available & readable
154 * IF (failed)
155 * RETURN 1 !errmsg filled
156 * ENDIF
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);
162 stat=(1); goto BYE;
165 DPRINT0("file downloaded successfully\n");
166 stat= 0;
168 BYE:
171 * A.14 CLOSE up ftp-ed file
173 if (f2) fclose(f2);
176 * A.15 RETURN 0 !success
178 DPRINT2 ("Leaving %s, Stat=%d\n", func, stat);
179 return(stat);
181 * END OF FUNCTION