home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Magazine / wwwoffle-2.1.tar.gz / wwwoffle-2.1 / wwwoffle-tools.c < prev    next >
C/C++ Source or Header  |  1998-01-17  |  9KB  |  354 lines

  1. /***************************************
  2.   $Header: /home/amb/wwwoffle/RCS/wwwoffle-tools.c 1.3 1998/01/17 21:25:15 amb Exp $
  3.  
  4.   WWWOFFLE - World Wide Web Offline Explorer - Version 2.1.
  5.   Tools for use in the cache for version 2.x.
  6.   ******************/ /******************
  7.   Written by Andrew M. Bishop
  8.  
  9.   This file Copyright 1997,98 Andrew M. Bishop
  10.   It may be distributed under the GNU Public License, version 2, or
  11.   any higher version.  See section COPYING of the GNU Public license
  12.   for conditions under which this file may be redistributed.
  13.   ***************************************/
  14.  
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. #include <time.h>
  22. #include <utime.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <dirent.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30.  
  31. #include "wwwoffle.h"
  32. #include "misc.h"
  33. #include "errors.h"
  34. #include "config.h"
  35.  
  36.  
  37. #define LS         1
  38. #define MV         2
  39. #define RM         3
  40. #define LS_SPECIAL 4
  41.  
  42. static void wwwoffle_ls(URL *Url);
  43. static void wwwoffle_ls_special(char *name);
  44. static void wwwoffle_mv(URL *Url1,URL *Url2);
  45. static void wwwoffle_rm(URL *Url);
  46.  
  47. static void ls(char *file);
  48.  
  49.  
  50. /*++++++++++++++++++++++++++++++++++++++
  51.   The main program
  52.   ++++++++++++++++++++++++++++++++++++++*/
  53.  
  54. int main(int argc,char **argv)
  55. {
  56.  struct stat buf;
  57.  URL *Url[2];
  58.  int mode=0;
  59.  int i;
  60.  char *p;
  61.  
  62.  p=argv[0]+strlen(argv[0])-1;
  63.  while(p>=argv[0] && *p!='/')
  64.     p--;
  65.  
  66.  p++;
  67.  
  68.  if(!strcmp(p,"wwwoffle-ls"))
  69.     mode=LS;
  70.  else if(!strcmp(p,"wwwoffle-mv"))
  71.     mode=MV;
  72.  else if(!strcmp(p,"wwwoffle-rm"))
  73.     mode=RM;
  74.  else
  75.    {
  76.     fprintf(stderr,"Program must be started as wwwoffle-ls, wwwoffle-mv or wwwoffle-rm\n");
  77.     exit(1);
  78.    }
  79.  
  80.  /* Initialise */
  81.  
  82.  if(mode==LS && argc!=2)
  83.    {fprintf(stderr,"Usage: wwwoffle-ls ( <dir>/<subdir> | <protocol>://<host> | <URL> | \n"
  84.                    "                     outgoing | lasttime )\n");exit(0);}
  85.  else if(mode==MV && argc!=3)
  86.    {fprintf(stderr,"Usage: wwwoffle-mv (<dir1>/<subdir1> | <protocol1>://<host1>)\n"
  87.                    "                   (<dir2>/<subdir2> | <protocol2>://<host2>)\n");exit(0);}
  88.  else if(mode==RM && argc!=2)
  89.    {fprintf(stderr,"Usage: wwwoffle-rm <URL>\n");exit(0);}
  90.  
  91.  if(stat("outgoing",&buf) || !S_ISDIR(buf.st_mode))
  92.    {fprintf(stderr,"The wwwoffle-tools programs must be started from the spool directory\n"
  93.                    "There is no 'outgoing' directory here so it can't be right.\n");exit(1);}
  94.  
  95.  InitErrorHandler(p,0,1);
  96.  
  97.  LogLevel=Inform;
  98.  
  99.  /* Get the arguments */
  100.  
  101.  for(i=1;i<argc;i++)
  102.    {
  103.     char *colon=strchr(argv[i],':');
  104.  
  105.     if(mode==LS && (!strcmp(argv[i],"outgoing") || !strcmp(argv[i],"lasttime")))
  106.       {
  107.        mode=LS_SPECIAL;
  108.       }
  109.     else if(colon)
  110.       {
  111.        Url[i-1]=SplitURL(argv[i]);
  112.       }
  113.     else
  114.       {
  115.        char *slash,*url;
  116.  
  117.        slash=strchr(argv[i],'/');
  118.        if(!slash)
  119.          {fprintf(stderr,"Cannot parse the argument '%s'\n",argv[i]);exit(1);}
  120.  
  121.        *slash=0;
  122.        url=(char*)malloc(strlen(slash+1)+strlen(argv[i])+8);
  123.        sprintf(url,"%s://%s",argv[i],slash+1);
  124.  
  125.        Url[i-1]=SplitURL(url);
  126.       }
  127.    }
  128.  
  129.  if(mode==LS)
  130.     wwwoffle_ls(Url[0]);
  131.  else if(mode==MV)
  132.     wwwoffle_mv(Url[0],Url[1]);
  133.  else if(mode==RM)
  134.     wwwoffle_rm(Url[0]);
  135.  else if(mode==LS_SPECIAL)
  136.     wwwoffle_ls_special(argv[1]);
  137.  
  138.  exit(0);
  139. }
  140.  
  141.  
  142. /*++++++++++++++++++++++++++++++++++++++
  143.   List the URLs within a directory of the cache.
  144.  
  145.   URL *Url The URL to list.
  146.   ++++++++++++++++++++++++++++++++++++++*/
  147.  
  148. static void wwwoffle_ls(URL *Url)
  149. {
  150.  if(chdir(Url->proto))
  151.    {PrintMessage(Warning,"Cannot change to directory '%s' [%!s].",Url->proto);return;}
  152.  
  153.  if(chdir(Url->host))
  154.    {PrintMessage(Warning,"Cannot change to directory '%s/%s' [%!s].",Url->proto,Url->host);chdir("..");return;}
  155.  
  156.  if(strcmp(Url->path,"/"))
  157.    {
  158.     char *name=URLToFileName(Url);
  159.  
  160.     *name='D';
  161.     ls(name);
  162.  
  163.     free(name);
  164.    }
  165.  else
  166.    {
  167.     struct dirent* ent;
  168.     DIR *dir=opendir(".");
  169.  
  170.     if(!dir)
  171.       {PrintMessage(Warning,"Cannot open current directory '%s/%s' [%!s].",Url->proto,Url->host);chdir("../..");return;}
  172.  
  173.     ent=readdir(dir);  /* skip .  */
  174.     if(!ent)
  175.       {PrintMessage(Warning,"Cannot read current directory '%s/%s' [%!s].",Url->proto,Url->host);closedir(dir);chdir("../..");return;}
  176.     ent=readdir(dir);  /* skip .. */
  177.  
  178.     while((ent=readdir(dir)))
  179.       {
  180.        if(*ent->d_name=='D')
  181.           ls(ent->d_name);
  182.       }
  183.  
  184.     closedir(dir);
  185.    }
  186.  
  187.  chdir("../..");
  188. }
  189.  
  190.  
  191. /*++++++++++++++++++++++++++++++++++++++
  192.   List the URLs within the outgoing or lasttime special directory of the cache.
  193.  
  194.   char *name The name of the directory to list.
  195.   ++++++++++++++++++++++++++++++++++++++*/
  196.  
  197. static void wwwoffle_ls_special(char *name)
  198. {
  199.  struct dirent* ent;
  200.  DIR *dir;
  201.  
  202.  if(chdir(name))
  203.    {PrintMessage(Warning,"Cannot change to directory '%s' [%!s].",name);return;}
  204.  
  205.  dir=opendir(".");
  206.  
  207.  if(!dir)
  208.    {PrintMessage(Warning,"Cannot open current directory '%s' [%!s].",name);chdir("..");return;}
  209.  
  210.  ent=readdir(dir);  /* skip .  */
  211.  if(!ent)
  212.    {PrintMessage(Warning,"Cannot read current directory '%s' [%!s].",name);closedir(dir);chdir("..");return;}
  213.  ent=readdir(dir);  /* skip .. */
  214.  
  215.  while((ent=readdir(dir)))
  216.    {
  217.     if(*ent->d_name=='D' || *ent->d_name=='O')
  218.        ls(ent->d_name);
  219.    }
  220.  
  221.  closedir(dir);
  222.  
  223.  chdir("..");
  224. }
  225.  
  226.  
  227. /*++++++++++++++++++++++++++++++++++++++
  228.   List one file.
  229.  
  230.   char *file The name of the file to ls.
  231.   ++++++++++++++++++++++++++++++++++++++*/
  232.  
  233. static void ls(char *file)
  234. {
  235.  static char *month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  236.  struct stat buf;
  237.  struct tm *tim;
  238.  time_t now=-1;
  239.  
  240.  if(now==-1)
  241.     now=time(NULL);
  242.  
  243.  if(stat(file,&buf))
  244.    {PrintMessage(Warning,"Cannot stat the file '%s' [%!s].",file);return;}
  245.  else
  246.    {
  247.     char *url=FileNameToURL(file);
  248.  
  249.     tim=localtime(&buf.st_mtime);
  250.  
  251.     if(buf.st_mtime<(now-(180*24*3600)))
  252.        printf("%s %7ld %3s %2d %d %s\n",file,(long)buf.st_size,month[tim->tm_mon],tim->tm_mday,tim->tm_year+1900,url);
  253.     else
  254.        printf("%s %7ld %3s %2d %2d:%02d %s\n",file,(long)buf.st_size,month[tim->tm_mon],tim->tm_mday,tim->tm_hour,tim->tm_min,url);
  255.  
  256.     free(url);
  257.    }
  258. }
  259.  
  260.  
  261. /*++++++++++++++++++++++++++++++++++++++
  262.   Move one URL or host to another.
  263.  
  264.   URL *Url1 The source URL.
  265.  
  266.   URL *Url2 The destination URL.
  267.   ++++++++++++++++++++++++++++++++++++++*/
  268.  
  269. static void wwwoffle_mv(URL *Url1,URL *Url2)
  270. {
  271.  struct dirent* ent;
  272.  DIR *dir;
  273.  
  274.  if(chdir(Url1->proto))
  275.    {PrintMessage(Warning,"Cannot change to directory '%s' [%!s].",Url1->proto);return;}
  276.  
  277.  if(chdir(Url1->host))
  278.    {PrintMessage(Warning,"Cannot change to directory '%s/%s' [%!s].",Url1->proto,Url1->host);chdir("..");return;}
  279.  
  280.  dir=opendir(".");
  281.  
  282.  if(!dir)
  283.    {PrintMessage(Warning,"Cannot open current directory '%s/%s' [%!s].",Url1->proto,Url1->host);chdir("../..");return;}
  284.  
  285.  ent=readdir(dir);  /* skip .  */
  286.  if(!ent)
  287.    {PrintMessage(Warning,"Cannot read current directory '%s/%s' [%!s].",Url1->proto,Url1->host);closedir(dir);chdir("../..");return;}
  288.  ent=readdir(dir);  /* skip .. */
  289.  
  290.  while((ent=readdir(dir)))
  291.    {
  292.     if(*ent->d_name=='D')
  293.       {
  294.        char *url1=FileNameToURL(ent->d_name),*url2;
  295.        URL *Url;
  296.        char *path2,*name1,*name2;
  297.        int fd2;
  298.  
  299.        Url=SplitURL(url1);
  300.        url2=(char*)malloc(strlen(Url->pathp)+strlen(Url2->host)+strlen(Url2->proto)+8);
  301.        sprintf(url2,"%s://%s%s",Url2->proto,Url2->host,Url->pathp);
  302.        FreeURL(Url);
  303.  
  304.        name1=(char*)malloc(strlen(ent->d_name)+1);
  305.        strcpy(name1,ent->d_name);
  306.  
  307.        Url=SplitURL(url2);
  308.        name2=URLToFileName(Url);
  309.  
  310.        path2=(char*)malloc(strlen(Url2->proto)+strlen(Url2->host)+strlen(name2)+16);
  311.  
  312.        sprintf(path2,"../../%s",Url2->proto);
  313.        mkdir(path2,0755);
  314.  
  315.        sprintf(path2,"../../%s/%s",Url2->proto,Url2->host);
  316.        mkdir(path2,0755);
  317.  
  318.        *name1=*name2='D';
  319.        sprintf(path2,"../../%s/%s/%s",Url2->proto,Url2->host,name2);
  320.        rename(name1,path2);
  321.  
  322.        *name1=*name2='U';
  323.        sprintf(path2,"../../%s/%s/%s",Url2->proto,Url2->host,name2);
  324.        fd2=open(path2,O_WRONLY|O_CREAT|O_TRUNC);
  325.        write_string(fd2,Url->name);
  326.        close(fd2);
  327.        unlink(name2);
  328.  
  329.        free(url1);
  330.        free(url2);
  331.        free(name1);
  332.        free(name2);
  333.        free(path2);
  334.        FreeURL(Url);
  335.       }
  336.    }
  337.  
  338.  closedir(dir);
  339.  
  340.  chdir("../..");
  341. }
  342.  
  343.  
  344. /*++++++++++++++++++++++++++++++++++++++
  345.   Delete a URL.
  346.  
  347.   URL *Url The URL to delete.
  348.   ++++++++++++++++++++++++++++++++++++++*/
  349.  
  350. static void wwwoffle_rm(URL *Url)
  351. {
  352.  DeleteWebpageSpoolFile(Url,0);
  353. }
  354.