/* ******************************************************************************* ** File Name: FTPSAMPLE.C -- sample program demonstrating usage of ** FTP library functions. ** ** Product: TCPware for OpenVMS ** ** Version: V5.6 ** ** Copyright (c) 2001, 2002 by ** Process Software LLC ** Framingham, Massachusetts ** ** Copyright (c) 1999 by ** Process Software Corporation ** Framingham, Massachusetts ** ** This software is furnished under a license for use on a ** single computer system and may be copied only with the ** inclusion of the above copyright notice. This software, or ** any other copies thereof, may not be provided or otherwise ** made available to any other person except for use on such ** system and to one who agrees to these license terms. Title ** to and ownership of the software shall at all times remain ** in Process Software LLC name. ** ** The information in this document is subject to change ** without notice and should not be construed as a commitment ** by Process Software LLC. Process Software LLC assumes no ** responsibility for any errors that may appear in this document. ** ** ** ABSTRACT: ** ** This program will FTP the RFC index file '/in-notes/rfc-index.txt' ** from the site 'venera.isi.edu'. ** ** ** BUILDING EXECUTABLES: ** ** 1. with DECC: ** $ CC/DECC FTPSAMPLE.C ** $ LINK FTPSAMPLE, SYS$INPUT/OPTIONS ** SYS$SHARE:TCPWARE_FTPLIB_SHR/SHARE ** SYS$SHARE:TCPWARE_SOCKLIB_SHR/SHARE ** ** 2. with VAXC: ** $ CC/VAXC FTPSAMPLE.C ** $ LINK FTPSAMPLE, SYS$INPUT/OPTIONS ** SYS$SHARE:TCPWARE_FTPLIB_SHR/SHARE ** SYS$SHARE:TCPWARE_SOCKLIB_SHR/SHARE ** SYS$SHARE:VAXCRTL.EXE/SHARE ** ******************************************************************************* ** REQUEST: If you have comments, please send them to "support@process.com" ******************************************************************************* */ #include #include #include #include #include #include /* for STS$M_SUCCESS */ #include "tcpware_include:ftpdef.h" /* ** Macros to check VMS success/failure status */ #define SUCCESS(status) (status & STS$M_SUCCESS) #define FAILURE(status) (!(status & STS$M_SUCCESS)) /* FTP library function prototypes */ int ftp_allocate_ccb( long *); int ftp_close_connection( long); int ftp_deallocate_ccb( long *); int ftp_get_file( long, struct dsc$descriptor_s *, struct dsc$descriptor_s *, long *, long *); int ftp_get_name_list( long, struct dsc$descriptor_s *, struct dsc$descriptor_s *); int ftp_login_user( long, struct dsc$descriptor_s *, struct dsc$descriptor_s *); int ftp_open_connection( long, unsigned long *, struct dsc$descriptor_s *, unsigned short *, unsigned long *); int ftp_quote( long, struct dsc$descriptor_s *); int ftp_put_file( long, struct dsc$descriptor_s *, struct dsc$descriptor_s *, long *, long *); int ftp_set_debug( long, long *, int (**)() ); int ftp_set_directory( long, struct dsc$descriptor_s *); /* function prototypes */ int get_file( long ccb); int get_ftp_mode( char *file_p); int output_debug_info( struct dsc$descriptor *outline_P); main () { long ccb; long status; long debug = DEBUG_REPLY; int ( *output_rtn_P)() = &output_debug_info; static $DESCRIPTOR (host_dsc, "venera.isi.edu"); ftp_allocate_ccb( &ccb); ftp_set_debug( ccb, &debug, &output_rtn_P); status = ftp_open_connection( ccb, 0, &host_dsc, 0, 0); if ( FAILURE( status) ) { printf ("failed to make connection to \"%s\"\n", host_dsc.dsc$a_pointer); } else { status = get_file( ccb); ftp_close_connection( ccb); } ftp_deallocate_ccb( &ccb); return (status); } /* ** Login to FTP server. ** Get list of files. ** Set default directory. ** Get specified file. */ int get_file( long ccb) { long status; long mode; static char file_str[] = "rfc-index.txt"; static char out_str[] = "SYS$OUTPUT:"; static $DESCRIPTOR (user_dsc, "anonymous"); static $DESCRIPTOR (pswd_dsc, "your-name@your-site"); static $DESCRIPTOR (dir_dsc, "in-notes"); static $DESCRIPTOR (file_dsc, file_str); static $DESCRIPTOR (output_dsc, out_str); status = ftp_login_user( ccb, &user_dsc, &pswd_dsc); if ( FAILURE( status) ) { printf ("failed to login as \"%s\"\n", user_dsc.dsc$a_pointer); return( status); } status = ftp_get_name_list( ccb, &dir_dsc, &output_dsc); if ( FAILURE( status) ) { printf("Can't get directory\n"); return( status); } status = ftp_set_directory( ccb, &dir_dsc); if ( FAILURE( status) ) { printf ("Failed to change directory to \"%s\"\n", dir_dsc.dsc$a_pointer); return( status); } mode = get_ftp_mode( (char *)&file_str); status = ftp_get_file( ccb, &file_dsc, 0, &mode, 0); if ( FAILURE( status) ) printf ("Failed to get file \"%s\"\n", file_str); return( status); } /* ** Define a table of file types for all those files that ** can be transferred using BINARY mode */ static char *binary_types[] = { "OBJ", "obj", "STB", "stb", "BIN", "bin", "LDA", "lda", NULL }; static char *image_types[] = { "EXE", "exe", "TSK", "tsk", "OLB", "olb", "MLB", "mlb", "SYS", "sys", "SML", "sml", "ULB", "ulb", "TLB", "tlb", "HLB", "hlb", "SAV", "sav", "DSK", "dsk", "GIF", "gif", "UID", "uid", NULL }; /* ** get_ftp_mode is a sample function to determine the transfer mode ** based upon the filespec extension. */ int get_ftp_mode( char *file_P) { int i; char *c_P; char *semi_colon_P; char temp[100]; /* ** Temporarily remove the semi-colon, if any. We will put it back ** before returning */ semi_colon_P = strchr(file_P, ';'); if (semi_colon_P != NULL) *semi_colon_P = 0; c_P = strchr (file_P, '.'); if (c_P == NULL) { if (semi_colon_P != NULL) *semi_colon_P = ';'; return( MODE_C_ASCII); } ++c_P; memset(temp, 0, sizeof(temp)); strcpy(temp, c_P); for(i=0; i < strlen(temp); i++) temp[i] = toupper(temp[i]); /* Cycle through the binary types */ for (i = 0; binary_types[i] != NULL; i++) { if (!strcmp(temp, binary_types[i])) { if (semi_colon_P != NULL) *semi_colon_P = ';'; return( MODE_C_BINARY); } } /* Cycle through the IMAGE types */ for (i = 0; image_types[i] != NULL; i++) { if (!strcmp(temp, image_types[i])) { if (semi_colon_P != NULL) *semi_colon_P = ';'; return( MODE_C_IMAGE); } } /* put back semi_colon if it was removed */ if (semi_colon_P != NULL) *semi_colon_P = ';'; return( MODE_C_ASCII); } /* ** Output the single line of debug info. ** ** The actual use of an output function, it to parse & act on the debug info ** returing from the server. */ int output_debug_info( struct dsc$descriptor *outline_P) { char *c_P; c_P = outline_P->dsc$a_pointer; *(c_P + outline_P->dsc$w_length) = '\0'; /* ensure string terminated */ printf("%s\n", c_P); return( SS$_NORMAL); }