home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2317 / ftpget.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.5 KB  |  86 lines

  1. /*
  2.  * ftpget.c by Mingqi Deng, July 6, 1989
  3.  *
  4.  *    makes one attempt to get a file from a remote host.
  5.  *
  6.  *  Compile:  
  7.  *             cc -o ftpget ftpget.c
  8.  *  Execute:
  9.  *             ftpget alarm remotehost ftp_script
  10.  *    where
  11.  *      alarm      :- the number in seconds that an ftp attempt can last
  12.  *      ftp_script :- ftp command script created by nextfile.c
  13.  *
  14.  *  Example:   
  15.  *             ftpget 3600 wsmr-simtel20.army.mil 123ftp_script
  16.  *  Note   : 
  17.  *    1. The maximum number of characters contained on ftpget's command
  18.  *       line is defined in the constant ftpget_cmd_line_length. The
  19.  *       default is 160.
  20.  *    2. The host name is assumed to have no more than 80 characters. 
  21.  *       Adjust the constant hostname_length if necessary.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <signal.h>
  26.  
  27. int id,pgid,mask;
  28.  
  29. #define ftpget_cmd_line_length     160
  30. #define hostname_length             80
  31.  
  32. main(argc, argv)
  33. int argc;
  34. char *argv[];
  35. {
  36.   unsigned t_alarm;
  37.   char cmd[ftpget_cmd_line_length+1];
  38.   void  handler();
  39.   FILE *f;
  40.  
  41.   if (argc != 3) {
  42.      fprintf(stderr,"***Type 'ftpget alarm ftp.script' to run\n");
  43.      exit(99);
  44.   }
  45.         /* check if file ftp_script exists. */
  46.   f = fopen(argv[2],"r");
  47.   if (f == NULL) {
  48.      fprintf(stderr,"***Ftp script file '%s' does not exist.\n",argv[2]);
  49.      exit(99);
  50.   }
  51.   fclose(f);
  52.  
  53.   sscanf(argv[1],"%u",&t_alarm);
  54.   if (t_alarm < 300) { 
  55.      fprintf(stderr,"***'alarm'(= %d < 300) in autoftp30.sh too small!\n",t_alarm);
  56.      exit(99);
  57.   }
  58.  
  59.   mask=sigsetmask(0);   /* do not block any signals */
  60.   id=getpid();  pgid=getpgrp(id);   /* get process group id */
  61.   setpgrp(id,pgid+1);   /* modify process group id so that only this 
  62.                and all of its subprocesses (with this id)
  63.                can be terminated by a signal */
  64.   signal(SIGALRM,handler);  /* catch the alarm signal and use procedure
  65.                 "handler" to process it */
  66.              /* create a command "ftp remotehost < ftp_script" to
  67.                 be run in current shell (instead of a subshell)*/
  68.   sprintf(cmd,"exec ftp -n < '%s'",argv[2]);
  69.  
  70.   alarm(t_alarm);   /* timing the system call next: send a SIGALRM 
  71.             signal to the current process after t_alarm 
  72.             many seconds */
  73.   system(cmd);      /* execute "ftp remotehost < ftp_script" */
  74.   alarm(0);         /* stop the alarm if "cmd" ends before the alarm
  75.             call*/
  76. }
  77.  
  78. /* SIGALRM signal handling */
  79. void handler()
  80. {
  81.    fprintf(stderr,"Alarm call!\n");
  82.    killpg(pgid+1,SIGHUP);  /* kill the current process and its
  83.                 subprocess (ftp ....) */
  84.    exit(5);
  85. }
  86.