home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / sun / misc / 5930 < prev    next >
Encoding:
Text File  |  1992-12-21  |  7.9 KB  |  269 lines

  1. Xref: sparky comp.sys.sun.misc:5930 alt.sources:2817
  2. Newsgroups: comp.sys.sun.misc,alt.sources
  3. Path: sparky!uunet!usc!cs.utexas.edu!bcm!aio!gothamcity!kjenks
  4. From: kjenks@gothamcity.jsc.nasa.gov
  5. Subject: Re: Automated FTP?
  6. Message-ID: <1992Dec19.171840.19641@aio.jsc.nasa.gov>
  7. Originator: kjenks@gothamcity
  8. Keywords: getit
  9. Sender: kjenks@gothamcity.jsc.nasa.gov
  10. Organization: NASA/JSC/GM2, Space Shuttle Program Office
  11. References: <1992Dec11.194130.14879@elroy.jpl.nasa.gov> <1992Dec14.020745.15321@maths.tcd.ie> <EMORENO.92Dec15102206@scheria.NMSU.Edu>
  12. Date: Sat, 19 Dec 1992 17:18:40 GMT
  13. Lines: 254
  14.  
  15. I wrote a little program called "getit" which is also available for
  16. anonymous FTP (of course!) at
  17.   export.lcs.mit.edu:contrib/getit.tar.Z
  18.  
  19. Here's the README, then the source code.  Enjoy, and tell all of your
  20. friends to post their anon-FTP file references in "getit" format!
  21.  
  22. -- Ken Jenks, NASA/JSC/GM2, Space Shuttle Program Office
  23.       kjenks@gothamcity.jsc.nasa.gov  (713) 483-4368
  24.  
  25.      "We at NASA develop cutting-edge technology for our aeronautics and
  26.       space programs.  We view technology transfer as a way of life.  
  27.       It's one of our top priorities." -- Daniel S. Goldin, NASA Administrator
  28.  
  29. -------------------------------------------------------------------
  30. "getit" is a short C program which uses anonymous FTP to retrieve a
  31. file specified on the command line.  To use it, run getit as either:
  32.    getit host:path/file 
  33. -or-
  34.    getit path/file@host
  35.  
  36. For example, this file is available via anonymous FTP using
  37.    getit contrib/getit.tar.Z@export.lcs.mit.edu
  38. which can also be written as
  39.    getit export.lcs.mit.edu:contrib/getit.tar.Z
  40.  
  41. To "make" the UNIX version of getit, simply use "cc -o getit getit.c"
  42.  
  43. The MS-DOS version, getitexe.exe, uses FTP Software's PC/TCP network
  44. drivers.  It assumes that the "FTP.EXE" program is in the DOS path.  To
  45. recompile getitexe.exe, you'll need Borland's Turbo C.  Note that the
  46. password getitexe.exe uses for anonymous FTP SHOULD be your e-mail ID,
  47. but that will have to be hard-coded in this program.  When you get this
  48. loaded on your PC, you'll probably want to rename it to "getit.exe" so
  49. it's easier to type.  As always, make sure you check for viruses.
  50. Practice safe downloading.
  51.  
  52. Ken Jenks
  53. Space Shuttle Program Office
  54. NASA Lyndon B. Johnson Space Center
  55. Houston, Texas
  56. (713) 483-4368
  57. kjenks@gothamcity.jsc.nasa.gov
  58.  
  59. -------- Begin source code --------------------------------------------
  60. /* 
  61.  *  getit.c (UNIX version)
  62.  *        - use anonymous FTP to get a file named on the command line
  63.  *        - file reference formats: (1) host:path/file, (2) path/file@host.
  64.  *
  65.  *        - written by NASA/JSC/GM2/Ken Jenks
  66.  *        - send comments to kjenks@gothamcity.jsc.nasa.gov
  67.  *
  68.  */
  69. #include <stdio.h>
  70. #include <string.h>
  71.  
  72. #ifndef lint
  73. static char rcsid[] = "$Header: getit.c,v 1.0 92/08/18 13:08:50 kcj Exp $";
  74. #endif
  75.  
  76. char *status_message;
  77. char *full_ref;
  78. char host_name[2048],path_name[2048],file_name[2048],mail_name[2048];
  79.  
  80. extern void exit();
  81. extern char *malloc(), *index(), *rindex();
  82. extern int system();
  83.  
  84. int main(argc, argv)
  85. int argc;
  86. char *argv[];
  87. {
  88.   FILE *fp;
  89.  
  90.   char *p, *b, status_msg_buff[2048], *program;
  91.   int dir, ascent, descent, error_level;
  92.  
  93.   status_message = status_msg_buff;
  94.   *status_message = '\0';
  95.  
  96.   program = argv[0];
  97.  
  98.   error_level = 0; /* Assume error code is 0 unless errors occur */
  99.  
  100.   /* 
  101.    * If there is no file reference on command line, use false reference
  102.    * so we give usage & exit
  103.    */
  104.   if(argc == 2) {
  105.     full_ref=strncpy(malloc(strlen(argv[1])+1),argv[1],strlen(argv[1]));
  106.     full_ref[strlen(argv[1])]='\0';
  107.   }
  108.   else
  109.     full_ref = "(none)";
  110.  
  111. #ifdef DEBUG
  112.   fprintf(stderr,"%s: full reference is %s\n",program,full_ref);
  113. #endif
  114.  
  115.   /* Now, I'll determine the host, path and file names from the
  116.    * full reference.
  117.    */
  118.  
  119.   /* Case 1: full_ref = "host:path/file"
  120.    */
  121.   if (index(full_ref,':')) {
  122.  
  123.     strncpy(host_name,full_ref,index(full_ref,':')-full_ref);
  124.     strcpy(path_name,index(full_ref,':')+1);
  125.  
  126.     /* Case 1a: Either path has a / in it or not.
  127.     */
  128.     if(rindex(path_name,'/')) {
  129.       strcpy(file_name,rindex(path_name,'/')+1);
  130.       *rindex(path_name,'/') = '\0';
  131.  
  132.     /* Case 1b: No / in path 
  133.     */
  134.     } else {
  135.       strcpy(file_name, path_name);
  136.       strcpy(path_name, ".");
  137.     }
  138.  
  139.   /* Case 2: full_ref = "path/file@host"
  140.   */
  141.   } else if (index(full_ref,'@')) {
  142.  
  143.     strncpy(path_name,full_ref,index(full_ref,'@')-full_ref);
  144.     strcpy(host_name,index(full_ref,'@')+1);
  145.  
  146.     /* Case 2a: Either path has a / in it or not.
  147.     */
  148.     if(rindex(path_name,'/')) {
  149.       strcpy(file_name,rindex(path_name,'/')+1);
  150.       *rindex(path_name,'/') = '\0';
  151.  
  152.     /* Case 2b: No / in path 
  153.     */
  154.     } else {                   /* No / in path */
  155.       strcpy(file_name, path_name);
  156.       strcpy(path_name, ".");
  157.     }
  158.  
  159.   /* Case 3: full_ref = none of the above
  160.   */
  161.   } else {
  162.     sprintf(status_message,
  163.       "Error: can't interpret file reference:\n%s\n\n\"%s\" provides rapid access to files using anonymous FTP.\nUsage:\n   %s host:path/file\n-or-\n   %s path/file@host\n",
  164.       full_ref,program,program,program);
  165.     error_level = 1;
  166.     goto show_status;
  167.   }
  168.  
  169.   /* Remove extraneous CR from end of strings
  170.   */
  171.   if(strlen(file_name))
  172.     if(file_name[strlen(file_name)-1] == 10)
  173.       file_name[strlen(file_name)-1] = '\0';
  174.  
  175.   if(strlen(path_name))
  176.     if(path_name[strlen(path_name)-1] == 10)
  177.       path_name[strlen(path_name)-1] = '\0';
  178.  
  179.   if(strlen(host_name))
  180.     if(host_name[strlen(host_name)-1] == 10)
  181.       host_name[strlen(host_name)-1] = '\0';
  182.  
  183.   /* Perform the command to get user's e-mail ID and capture the results
  184.   */
  185.   system("echo `whoami`@`hostname`.`domainname` > getit_mail_file");
  186.  
  187.   /* Read e-mail ID for use as password in anonymous FTP
  188.   */
  189.   if((fp = fopen("getit_mail_file","r")) == NULL) {
  190.     fprintf(stdout, "Warning: can't determine e-mail ID for password");
  191.     strcpy(mail_name,"can't-find-e-mail-ID");
  192.   } else
  193.     fscanf(fp,"%s",mail_name);
  194.   strcpy(mail_name+strlen(mail_name),"(getit)");
  195.  
  196. #ifdef DEBUG
  197.   fprintf(stderr,"  host: %s\n",host_name);
  198.   fprintf(stderr,"  path: %s\n",path_name);
  199.   fprintf(stderr,"  file: %s\n",file_name);
  200.   fprintf(stderr,"  pswd: %s\n",mail_name);
  201. #endif
  202.  
  203.   /* Open scratch file to hold FTP commands
  204.   */
  205.   if((fp = fopen("getit_scratch_file","w")) == NULL) {
  206.     sprintf(status_message,"Error: can't open scratch file %s",
  207.       "getit_scratch_file");
  208.     error_level = 2;
  209.     goto show_status;
  210.   }
  211.  
  212.   /* Build the commands needed to execute the anonymous FTP
  213.   */
  214.   fprintf(fp,"#! /bin/sh\n");
  215.   fprintf(fp,"/bin/echo \"Getting file %s from path %s at host %s...\"\n",
  216.     file_name,path_name,host_name);
  217.   fprintf(fp,"/usr/ucb/ftp -inv << EOF\n");
  218.   fprintf(fp,"open %s\n",host_name);
  219.   fprintf(fp,"\n");
  220.   fprintf(fp,"user anonymous %s\n",mail_name);
  221.   fprintf(fp,"binary\n"); /* Turn binary ON for all transfers */
  222.   fprintf(fp,"prompt\n"); /* Turn prompt OFF for possible mget */
  223.   if((*path_name != '.') || (strlen(path_name) != 1))
  224.     fprintf(fp,"cd %s\n",path_name);
  225.  
  226.   /* Do either mget or get, depening on existance of '*' in file_name
  227.   */
  228.   if(index(file_name,'*'))
  229.     fprintf(fp,"mget %s\n", file_name); 
  230.   else
  231.     fprintf(fp,"get %s\n", file_name); 
  232.  
  233.   fprintf(fp,"quit\n");
  234.   fprintf(fp,"EOF\n");
  235.   fclose(fp);
  236.  
  237.   /* Perform the FTP and capture the results
  238.   */
  239.   system("sh getit_scratch_file > getit_error_file");
  240.  
  241.   /* Read any resulting error messages
  242.   */
  243.   if((fp = fopen("getit_error_file","r")) == NULL) {
  244.     sprintf(status_message,"Warning: can't read error file %s",
  245.       "getit_error_file");
  246.     error_level = 3;
  247.     goto show_status;
  248.   }
  249.  
  250.   status_message = status_msg_buff;
  251.   while(feof(fp) == 0)
  252.     *(status_message++) = fgetc(fp);
  253.   *(--status_message) = '\0';
  254.   status_message = status_msg_buff;
  255.  
  256.   fclose(fp);
  257.  
  258. show_status:
  259.  
  260.   system("rm -f getit_*_file > /dev/null");
  261.  
  262.   /* If there is no message to be displayed, exit quietly. 
  263.    */
  264.   if(status_message)
  265.     if(strlen(status_message))
  266.       fprintf(stderr,"%s\n",status_message);
  267.   exit(error_level);
  268. }
  269.