home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / ftp / archie132.lha / archie-1.3.2 / src / procquery.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-10  |  5.5 KB  |  225 lines

  1. /*
  2.  * procquery.c : Routines for processing results from Archie
  3.  *
  4.  * Originally part of the Prospero Archie client by Cliff Neuman (bcn@isi.edu).
  5.  * Modified by Brendan Kehoe (brendan@cs.widener.edu).
  6.  * Re-modified by George Ferguson (ferguson@cs.rochester.edu).
  7.  *
  8.  * Copyright (c) 1991 by the University of Washington
  9.  *
  10.  * For copying and distribution information, please see the file
  11.  * <copyright.h>.
  12.  *
  13.  */
  14.  
  15. #include <copyright.h>
  16. #include <stdio.h>
  17. #include <pfs.h>
  18. #include <perrno.h>
  19. #include <pmachine.h>
  20. #include <archie.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23.  
  24. #ifdef NEED_TIME_H
  25. # include <time.h>
  26. #else
  27. # ifndef VMS
  28. #  include <sys/time.h>
  29. # endif
  30. #endif
  31.  
  32. extern int client_dirsrv_timeout,client_dirsrv_retry;    /* dirsend.c */
  33. extern char *progname;
  34.  
  35.  
  36. /*
  37.  * Functions defined here
  38.  */
  39. void display_link(), procquery();
  40.  
  41. /*
  42.  * Data defined here
  43.  */
  44. extern int pwarn, perrno;
  45. #ifdef DEBUG
  46. int pfs_debug;
  47. #endif
  48. static struct tm *presenttime;
  49. static char lastpath[MAX_VPATH] = "\001";
  50. static char lasthost[MAX_VPATH] = "\001";
  51.  
  52. /*    -    -    -    -    -    -    -    -    */
  53. /*
  54.  * display_link : Prints the contents of the given virtual link. If
  55.  *    listflag is 0, then this uses last{host,path} to save state
  56.  *    between calls for a less verbose output. If listflag is non-zero
  57.  *    then all information is printed every time.
  58.  */
  59. void
  60. display_link(l,listflag)
  61. VLINK l;
  62. int listflag;
  63. {
  64.     PATTRIB     ap;
  65.     char    linkpath[MAX_VPATH];
  66.     int        dirflag = 0;
  67. #ifdef MSDOS
  68.     unsigned long size = 0L;
  69. #else
  70.     int        size = 0;
  71. #endif
  72.     char    *modes = "";
  73.     char    archie_date[20];
  74.     char    *gt_date = "";
  75.     int        gt_year = 0;
  76.     int        gt_mon = 0;
  77.     int        gt_day = 0;
  78.     int        gt_hour = 0;
  79.     int        gt_min = 0;
  80.     
  81.     /* Initialize local buffers */
  82.     *archie_date = '\0';
  83.  
  84.     /* Remember if we're looking at a directory */
  85.     if (sindex(l->type,"DIRECTORY"))
  86.     dirflag = 1;
  87.     else
  88.     dirflag = 0;
  89.     
  90.     /* Extract the linkpath from the filename */
  91.     strcpy(linkpath,l->filename);
  92.     *(linkpath + (strlen(linkpath) - strlen(l->name) - 1)) = '\0';
  93.     
  94.     /* Is this a new host? */
  95.     if (strcmp(l->host,lasthost) != 0) {
  96.     if (!listflag)
  97.         printf("\nHost %s\n\n",l->host);
  98.     strcpy(lasthost,l->host);
  99.     *lastpath = '\001';
  100.     }
  101.     
  102.     /* Is this a new linkpath (location)? */
  103.     if(strcmp(linkpath,lastpath) != 0) {
  104.     if (!listflag)
  105.         printf("    Location: %s\n",(*linkpath ? linkpath : "/"));
  106.     strcpy(lastpath,linkpath);
  107.     }
  108.     
  109.     /* Parse the attibutes of this link */
  110.     for (ap = l->lattrib; ap; ap = ap->next) {
  111.     if (strcmp(ap->aname,"SIZE") == 0) {
  112. #ifdef MSDOS
  113.         sscanf(ap->value.ascii,"%lu",&size);
  114. #else
  115.         sscanf(ap->value.ascii,"%d",&size);
  116. #endif
  117.     } else if(strcmp(ap->aname,"UNIX-MODES") == 0) {
  118.         modes = ap->value.ascii;
  119.     } else if(strcmp(ap->aname,"LAST-MODIFIED") == 0) {
  120.         gt_date = ap->value.ascii;
  121.         sscanf(gt_date,"%4d%2d%2d%2d%2d",>_year,
  122.            >_mon, >_day, >_hour, >_min);
  123.         if ((12 * (presenttime->tm_year + 1900 - gt_year) + 
  124.                     presenttime->tm_mon - gt_mon) > 6) 
  125.         sprintf(archie_date,"%s %2d %4d",month_sname(gt_mon),
  126.             gt_day, gt_year);
  127.         else
  128.         sprintf(archie_date,"%s %2d %02d:%02d",month_sname(gt_mon),
  129.              gt_day, gt_hour, gt_min);
  130.     }
  131.     }
  132.     
  133.     /* Print this link's information */
  134.     if (listflag)
  135. #if defined(MSDOS)
  136.     printf("%s %6lu %s %s%s\n",gt_date,size,l->host,l->filename,
  137.            (dirflag ? "/" : ""));
  138. #else
  139.     printf("%s %6d %s %s%s\n",gt_date,size,l->host,l->filename,
  140.            (dirflag ? "/" : ""));
  141. #endif
  142.     else
  143. #ifdef MSDOS
  144.     printf("      %9s %s %10lu  %s  %s\n",(dirflag ? "DIRECTORY" : "FILE"),
  145.                     modes,size,archie_date,l->name);
  146. #else
  147.     printf("      %9s %s %10d  %s  %s\n",(dirflag ? "DIRECTORY" : "FILE"),
  148.                     modes,size,archie_date,l->name);
  149. #endif /* MSDOS */
  150.  
  151.  
  152.     /* Free the attibutes */
  153.     atlfree(l->lattrib);
  154.     l->lattrib = NULL;
  155. }
  156.  
  157. /*    -    -    -    -    -    -    -    -    */
  158. /*
  159.  * procquery : Process the given query and display the results. If
  160.  *    sortflag is non-zero, then the results are sorted by increasing
  161.  *    date, else by host/filename. If listflag is non-zero then each
  162.  *    entry is printed on a separate, complete line. Note that listflag
  163.  *    is ignored by xarchie.
  164.  */
  165. void
  166. procquery(host,str,max_hits,offset,query_type,sortflag,listflag)
  167. char *host,*str;
  168. int max_hits,offset;
  169. char query_type;
  170. int sortflag,listflag;
  171. {
  172.     VLINK l;
  173.     long now;
  174.     extern int rdgram_priority;
  175.  
  176.     /* initialize data structures for this query */
  177.     (void)time(&now);
  178.     presenttime = localtime(&now);
  179.  
  180.     /* Do the query */
  181.     if (sortflag == 1)
  182.     l = archie_query(host,str,max_hits,offset,query_type,AQ_INVDATECMP,0);
  183.     else
  184.     l = archie_query(host,str,max_hits,offset,query_type,NULL,0);
  185.  
  186.     /* Error? */
  187.     if (perrno != PSUCCESS) {
  188.     if (p_err_text[perrno]) {
  189.         if (*p_err_string)
  190.         fprintf(stderr, "%s: failed: %s - %s\n", progname,
  191.                 p_err_text[perrno], p_err_string);
  192.         else
  193.             fprintf(stderr, "%s failed: %s\n", progname, p_err_text[perrno]);
  194.     } else
  195.         fprintf(stderr, "%s failed: Undefined error %d (prospero)", perrno);
  196.     }
  197.  
  198.     /* Warning? */
  199.     if (pwarn != PNOWARN) {
  200.     if (*p_warn_string)
  201.         fprintf(stderr, "%s: Warning! %s - %s\n", progname,
  202.         p_warn_text[pwarn], p_warn_string);
  203.     else
  204.         fprintf(stderr, "%s: Warning! %s\n", progname, p_warn_text[pwarn]);
  205.     }
  206.  
  207.  
  208.     /* Display the results */
  209.  
  210.     if (l == (VLINK)NULL && pwarn == PNOWARN && perrno == PSUCCESS) {
  211.     if (! listflag) puts ("No matches.");
  212. #ifdef CUTCP
  213.     netshut();
  214. #endif
  215.     exit (1);
  216.     }
  217.  
  218.     *lasthost = '\001';
  219.     *lastpath = '\001';
  220.     while (l != NULL) {
  221.     display_link(l,listflag);
  222.     l = l->next;
  223.     }
  224. }
  225.