source: trunk/WRF.COMMON/WRFV3/external/io_grib1/MEL_grib1/FTP_getfile.c @ 3567

Last change on this file since 3567 was 2759, checked in by aslmd, 2 years ago

adding unmodified code from WRFV3.0.1.1, expurged from useless data +1M size

File size: 4.9 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3
4#include "dprints.h"            /* Debug printing & function prototypes*/
5#include "gribfuncs.h"          /* function prototypes */
6/*
7*
8********************************************************************
9* A. FUNCTION:  FTP_getfile
10*       builds and executes a Bourne script file to retreive
11*       the file specified from remote site via Ftp call;
12*       Execute script to establish ftp session (under Userid 'anonymous'
13*       & passwd 'gribsimp22'):
14*       Host info is retrieved from file "$pathnm/tables.cfg" whose content
15*       is a one line entry= "eifel.nrlmry.navy.mil receive/GRIB_TABLES"
16*
17*    INTERFACE:
18*       int     FTP_getfile (filenm, loc_pathnm, errmsg)
19*
20*    ARGUMENTS (I=input, O=output, I&O=input and output):
21*       (I)  char *filenm;       Name of file to ftp
22*       (I)  char *loc_pathnm;   Full path leading to config file 'tables.cfg'
23*       (O)  char *errmsg;       Empty array, Returns filled if error occurs;
24*
25*    RETURN CODE:
26*        0> sucessfully ftp-ed;
27*        1> error:  create script/ftp err/missing table.cfg;
28********************************************************************
29*/
30
31#if PROTOTYPE_NEEDED
32int     FTP_getfile (char *filenm, char *loc_pathnm, char *errmsg)
33
34#else
35int     FTP_getfile (filenm, loc_pathnm, errmsg)
36        char *filenm; 
37        char *loc_pathnm; 
38        char *errmsg;
39#endif
40{
41FILE    *f1=NULL, *f2=NULL;
42char    *func="FTP_getfile";
43char    filename[200];
44char    hostnm[100];    /* name of remote site  */
45char    usernm[100];    /* using anonymous  */
46char    passwd[100];    /* anonymous  */
47char    pathnm[100];    /* full path of remote file to get  */
48int     stat;           /* return status */
49int     n;              /* working var */
50
51  DPRINT3 ("Entering %s (%s/%s)\n", func, loc_pathnm, filenm);
52/*
53*
54* A.1       SET up name of local config file     !$local_path/tables.cfg
55*           IF (unable to open config file)
56*               RETURN 1  !errmsg filled
57*           ENDIF
58*/
59
60   /*   USE SAME CONFIG FILE --
61        no matter if dnloading "g1tab* , or neon2gr*, or orig_ctr"
62   */
63  sprintf (filename, "%s/tables.cfg", loc_pathnm);
64  DPRINT1 ("Read Remote host info from '%s'\n", filename);
65  if ((f1=fopen (filename, "r"))==NULL) {
66        sprintf(errmsg,"%s: failed to open '%s' for reading;\n",func,filename);
67        stat=(1); goto BYE;
68        }
69/*
70*
71* A.2       READ hostname and remote pathname from file, then close it;
72*           !config entry->    "eifel.nrlmry.navy.mil receive/GRIB_TABLES"
73*
74* A.3       CLOSE config file;
75*/
76   n = fscanf (f1, "%s%s", hostnm, pathnm);
77   fclose(f1);     /* close Config File */
78/*
79*
80* A.4       IF (read failed) RETURN 1  !errmsg filled;
81*/
82  if (!= 2) {
83        sprintf(errmsg,"%s: Fail to read 2 args from '%s'\n", func, filename);
84        stat=(1); goto BYE;
85        }
86
87/*
88*
89* A.6       SET password to "gribsimp22", userid to "anonymous"
90*/
91  strcpy (passwd, "gribsimp22");
92
93 /* Ready to build Bourne script: */
94/*
95*
96* A.7       IF (create temp script file fails)
97*               RETURN 1   !errmsg filled
98*           ENDIF
99*/
100  if ((f1=fopen ("temp_ftp_script","w"))==NULL) {
101        sprintf(errmsg,"%s:  failed to build FTP script\n", func);
102        stat=(1); goto BYE;
103        }
104/*
105*
106* A.8       CREATE ftp script to download Host's "receive/GRIB_TABLES/$fn"
107*           to $localPath/$fn locally;
108*
109* A.9       CLOSE temp file
110*/
111        fprintf (f1,
112                "#!/bin/sh\nexec 1>&-;exec 2>&-\nftp -in %s << STOP\n" \
113                "user anonymous %s\ncd %s\nlcd %s\nget %s\nquit\n" \
114                "STOP\nexit\n", 
115                hostnm,  passwd, pathnm, loc_pathnm, filenm);
116        fclose(f1);
117
118        DPRINT5 ("execute ftp script: \n"
119                "   #!/bin/sh\n   exec 1>&-;exec 2>&-\n"
120                "   ftp -in %s << STOP\n   user anonymous %s\n"
121                "   cd %s\n   lcd %s\n   get %s\n   quit\n   STOP\n   exit\n",
122                hostnm,  passwd, pathnm, loc_pathnm, filenm);
123/*
124*
125* A.10      EXECUTE script to download lookup file
126*
127* A.11      REMOVE temp script
128*/
129  fprintf(stdout,"Attempting to get remote '%s'\n", filenm);
130  n= system ("chmod 755 temp_ftp_script;temp_ftp_script");
131  unlink ("temp_ftp_script");
132
133/*
134*
135* A.12      IF (execute script failed)
136*               RETURN 1   !errmsg filled
137*           ENDIF
138*/
139  if (n!=0) {   /* ck Stat of Systm call */
140        sprintf(errmsg,"%s:  system call to ftp failed\n", func);
141        stat=(1); goto BYE;
142        }
143
144/*
145*
146* A.13      CHECK if ftp-ed file is available & readable
147*           IF (failed)
148*               RETURN 1   !errmsg filled
149*           ENDIF
150*/
151  sprintf (filename, "%s/%s", loc_pathnm, filenm);
152  if ((f2= fopen(filename, "rb+"))==NULL) {
153        sprintf(errmsg,"%s: '%s' not avail on %s in %s\n\n", func,
154        filenm, hostnm, pathnm);
155        stat=(1); goto BYE;
156        }
157
158  DPRINT0("file downloaded successfully\n");
159  stat= 0;
160
161BYE:
162/*
163*
164* A.14      CLOSE up ftp-ed file
165*/
166  if (f2) fclose(f2);
167
168/*
169* A.15      RETURN 0 !success
170*/
171DPRINT2 ("Leaving %s, Stat=%d\n", func, stat);
172return(stat);
173/*
174* END OF FUNCTION
175*
176*/
177}
Note: See TracBrowser for help on using the repository browser.