home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / UNIX / ARCHIE / CLIENTS / C_ARCHI5.TAR / archie-1.4 / procquery.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-20  |  6.5 KB  |  287 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@cygnus.com).
  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 <stdio.h>
  16. #include <pfs.h>
  17. #include <perrno.h>
  18. #include <pmachine.h>
  19. #include <archie.h>
  20.  
  21. #ifdef NEED_TIME_H
  22. # include <time.h>
  23. # ifdef WANT_BOTH_TIME
  24. #  include <sys/time.h>
  25. # endif
  26. #else
  27. # ifndef VMS
  28. #  include <sys/time.h>
  29. # endif
  30. #endif
  31.  
  32. /* Defined in dirsend.c.  */
  33. extern int client_dirsrv_timeout, client_dirsrv_retry;
  34. /* In archie.c.  */
  35. extern char *program_name;
  36. extern FILE *archie_out;
  37.  
  38. /*
  39.  * Functions defined here
  40.  */
  41. static void display_link (), alex_name ();
  42. void procquery ();
  43.  
  44. /*
  45.  * Data defined here
  46.  */
  47. extern int pwarn, perrno;
  48. extern int listflag, sortflag, alex;
  49.  
  50. #ifdef DEBUG
  51. int pfs_debug;
  52. #endif
  53. static struct tm *presenttime;
  54. static char lastpath[MAX_VPATH] = "\001";
  55. static char lasthost[MAX_VPATH] = "\001";
  56.  
  57. /* Print the contents of the given virtual link.  */
  58. static void
  59. display_link (l)
  60.      VLINK l;
  61. {
  62.     PATTRIB     ap;
  63.     char    linkpath[MAX_VPATH];
  64.     int        dirflag = 0;
  65. #ifdef MSDOS
  66.     unsigned long size = 0L;
  67. #else
  68.     int        size = 0;
  69. #endif
  70.     char    *modes = "";
  71.     char    archie_date[20];
  72.     char    *gt_date = "";
  73.     int        gt_year = 0;
  74.     int        gt_mon = 0;
  75.     int        gt_day = 0;
  76.     int        gt_hour = 0;
  77.     int        gt_min = 0;
  78.     int        special = listflag || alex;
  79.  
  80.     /* Initialize local buffers */
  81.     *archie_date = '\0';
  82.  
  83.     /* Remember if we're looking at a directory */
  84.     if (sindex(l->type,"DIRECTORY"))
  85.     dirflag = 1;
  86.     else
  87.     dirflag = 0;
  88.     
  89.     /* Extract the linkpath from the filename */
  90.     strcpy(linkpath,l->filename);
  91.     *(linkpath + (strlen(linkpath) - strlen(l->name) - 1)) = '\0';
  92.     
  93.     /* Is this a new host? */
  94.     if (strcmp(l->host,lasthost) != 0) {
  95.     if (!special)
  96.         fprintf (archie_out, "\nHost %s\n\n",l->host);
  97.     strcpy(lasthost,l->host);
  98.     *lastpath = '\001';
  99.     }
  100.     
  101.     /* Is this a new linkpath (location)? */
  102.     if(strcmp(linkpath,lastpath) != 0) {
  103.     if (!special)
  104.         fprintf (archie_out,
  105.              "    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.       fprintf (archie_out,
  137.            "%s %6lu %s %s%s\n",gt_date,size,l->host,l->filename,
  138.            (dirflag ? "/" : ""));
  139. #else
  140.     fprintf (archie_out,
  141.          "%s %6d %s %s%s\n",gt_date,size,l->host,l->filename,
  142.          (dirflag ? "/" : ""));
  143. #endif
  144.     else if (alex)
  145.       alex_name (l->host, l->filename, dirflag);
  146.     else
  147. #ifdef MSDOS
  148.     fprintf (archie_out,
  149.          "      %9s %s %10lu  %s  %s\n",(dirflag ? "DIRECTORY" : "FILE"),
  150.          modes,size,archie_date,l->name);
  151. #else
  152.     fprintf (archie_out,
  153.          "      %9s %s %10d  %s  %s\n",(dirflag ? "DIRECTORY" : "FILE"),
  154.          modes,size,archie_date,l->name);
  155. #endif /* MSDOS */
  156.  
  157.  
  158.     /* Free the attibutes */
  159.     atlfree(l->lattrib);
  160.     l->lattrib = NULL;
  161. }
  162.  
  163. /*    -    -    -    -    -    -    -    -    */
  164. /*
  165.  * procquery : Process the given query and display the results. If
  166.  *    sortflag is non-zero, then the results are sorted by increasing
  167.  *    date, else by host/filename. If listflag is non-zero then each
  168.  *    entry is printed on a separate, complete line. Note that listflag
  169.  *    is ignored by xarchie.
  170.  */
  171. void
  172. procquery(host,str,max_hits,offset,query)
  173. char *host,*str;
  174. int max_hits,offset;
  175. Query query;
  176. {
  177.     VLINK l;
  178.     long now;
  179.  
  180.     /* initialize data structures for this query */
  181.     (void)time(&now);
  182.     presenttime = localtime(&now);
  183.  
  184.     /* Do the query */
  185.     if (sortflag == 1)
  186.     l = archie_query(host,str,max_hits,offset,query,AQ_INVDATECMP,0);
  187.     else
  188.     l = archie_query(host,str,max_hits,offset,query,NULL,0);
  189.  
  190.     /* Error? */
  191.     if (perrno != PSUCCESS) {
  192.     if (p_err_text[perrno]) {
  193.         if (*p_err_string)
  194.         fprintf(stderr, "%s: failed: %s - %s\n",
  195.             program_name, p_err_text[perrno], p_err_string);
  196.         else
  197.             fprintf(stderr, "%s failed: %s\n",
  198.             program_name, p_err_text[perrno]);
  199.     } else
  200.         fprintf(stderr, "%s failed: Undefined error %d (prospero)",
  201.             program_name, perrno);
  202.     }
  203.  
  204.     /* Warning? */
  205.     if (pwarn != PNOWARN) {
  206.     if (*p_warn_string)
  207.         fprintf(stderr, "%s: Warning! %s - %s\n", program_name,
  208.         p_warn_text[pwarn], p_warn_string);
  209.     else
  210.         fprintf(stderr, "%s: Warning! %s\n", program_name, p_warn_text[pwarn]);
  211.     }
  212.  
  213.  
  214.     /* Display the results */
  215.  
  216.     if (l == (VLINK)NULL && pwarn == PNOWARN && perrno == PSUCCESS) {
  217.     if (! listflag) puts ("No matches.");
  218. #ifdef CUTCP
  219.     netshut();
  220. #endif
  221.     exit (1);
  222.     }
  223.  
  224.     *lasthost = '\001';
  225.     *lastpath = '\001';
  226.     while (l != NULL) {
  227.     display_link (l);
  228.     l = l->next;
  229.     }
  230. }
  231.  
  232. /* Given a dotted hostname, return its Alex root.  */
  233. static char *
  234. alex_reverse (string, len)
  235.      char *string;
  236.      int len;
  237. {
  238.   register char *p, *q;
  239.   register int i = 0;
  240.   char *buf = (char *) malloc (len);
  241.  
  242.   p = buf;
  243.   q = (char *) (string + len - 1);
  244.   while (q > string)
  245.     {
  246.       for (i = 0; q > string; q--, i++)
  247.     if (*q == '.')
  248.       {
  249.         q++;
  250.         break;
  251.       }
  252.       if (q == string)
  253.     i++;
  254.       strncpy (p, q, i);
  255.       p += i;
  256.       *p++ = '/';
  257.       i = 0;
  258.       q -= 2;
  259.     }
  260.   *--p = '\0';
  261.   return buf;
  262. }
  263.  
  264. /* Emit a string that's the Alex filename for the given host and file.  */
  265. static void
  266. alex_name (host, file, dirp)
  267.      char *host, *file;
  268.      int dirp;
  269. {
  270.   int hostlen = strlen (host);
  271.   int len = 6 + hostlen + strlen (file) + dirp +  1;
  272.   char *buf = (char *) malloc (len);
  273.  
  274.   if (! buf)
  275.     exit (99);
  276.  
  277.   sprintf (buf, "/alex/%s%s", alex_reverse (host, hostlen), file);
  278.   if (dirp)
  279.     {
  280.       len -= dirp;
  281.       buf[len - 1] = '/';
  282.       buf[len] = '\0';
  283.     }
  284.  
  285.   fputs (buf, archie_out);
  286. }
  287.