File: wwwoffle-tools.c

package info (click to toggle)
wwwoffle 2.3a-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,452 kB
  • ctags: 1,064
  • sloc: ansic: 10,102; lex: 1,395; sh: 510; makefile: 263; perl: 78
file content (422 lines) | stat: -rw-r--r-- 10,246 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/***************************************
  $Header: /home/amb/wwwoffle/RCS/wwwoffle-tools.c 1.8 1998/08/16 15:26:14 amb Exp $

  WWWOFFLE - World Wide Web Offline Explorer - Version 2.3.
  Tools for use in the cache for version 2.x.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1997,98 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <time.h>
#include <utime.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "wwwoffle.h"
#include "misc.h"
#include "errors.h"
#include "config.h"


#define LS         1
#define LS_SPECIAL 2
#define MV         3
#define RM         4
#define READ       5
#define WRITE      6

static void wwwoffle_ls(URL *Url);
static void wwwoffle_ls_special(char *name);
static void wwwoffle_mv(URL *Url1,URL *Url2);
static void wwwoffle_rm(URL *Url);
static void wwwoffle_read(URL *Url);
static void wwwoffle_write(URL *Url);

static void ls(char *file);


/*++++++++++++++++++++++++++++++++++++++
  The main program
  ++++++++++++++++++++++++++++++++++++++*/

int main(int argc,char **argv)
{
 struct stat buf;
 URL *Url[2];
 int mode=0;
 int i;
 char *p;

 p=argv[0]+strlen(argv[0])-1;
 while(p>=argv[0] && *p!='/')
    p--;

 p++;

 if(!strcmp(p,"wwwoffle-ls"))
    mode=LS;
 else if(!strcmp(p,"wwwoffle-mv"))
    mode=MV;
 else if(!strcmp(p,"wwwoffle-rm"))
    mode=RM;
 else if(!strcmp(p,"wwwoffle-read"))
    mode=READ;
 else if(!strcmp(p,"wwwoffle-write"))
    mode=WRITE;
 else
   {
    fprintf(stderr,"Program must be started as: wwwoffle-ls, wwwoffle-mv, wwwoffle-rm\n");
    fprintf(stderr,"                            wwwoffle-read or wwwoffle-write\n");
    exit(1);
   }

 /* Initialise */

 if(mode==LS && argc!=2)
   {fprintf(stderr,"Usage: wwwoffle-ls ( <dir>/<subdir> | <protocol>://<host> | <URL> | \n"
                   "                     outgoing | lasttime )\n");exit(0);}
 else if(mode==MV && argc!=3)
   {fprintf(stderr,"Usage: wwwoffle-mv (<dir1>/<subdir1> | <protocol1>://<host1>)\n"
                   "                   (<dir2>/<subdir2> | <protocol2>://<host2>)\n");exit(0);}
 else if(mode==RM && argc!=2)
   {fprintf(stderr,"Usage: wwwoffle-rm <URL>\n");exit(0);}
 else if(mode==READ && argc!=2)
   {fprintf(stderr,"Usage: wwwoffle-read <URL>\n");exit(0);}
 else if(mode==WRITE && argc!=2)
   {fprintf(stderr,"Usage: wwwoffle-write <URL>\n");exit(0);}

 if(stat("outgoing",&buf) || !S_ISDIR(buf.st_mode))
   {fprintf(stderr,"The wwwoffle-tools programs must be started from the spool directory\n"
                   "There is no 'outgoing' directory here so it can't be right.\n");exit(1);}

 InitErrorHandler(p,0,1);

 LogLevel=Inform;

 /* Get the arguments */

 for(i=1;i<argc;i++)
   {
    char *colon=strchr(argv[i],':');
    char *slash=strchr(argv[i],'/');

    if(mode==LS && (!strcmp(argv[i],"outgoing") || !strcmp(argv[i],"lasttime")))
      {
       mode=LS_SPECIAL;
      }
    else if(colon && slash && colon<slash)
      {
       Url[i-1]=SplitURL(argv[i]);
      }
    else
      {
       char *slash,*url;

       slash=strchr(argv[i],'/');
       if(!slash)
         {fprintf(stderr,"Cannot parse the argument '%s'\n",argv[i]);exit(1);}

       *slash=0;
       url=(char*)malloc(strlen(slash+1)+strlen(argv[i])+8);
       sprintf(url,"%s://%s",argv[i],slash+1);

       Url[i-1]=SplitURL(url);
      }
   }

 if(mode==LS)
    wwwoffle_ls(Url[0]);
 else if(mode==LS_SPECIAL)
    wwwoffle_ls_special(argv[1]);
 else if(mode==MV)
    wwwoffle_mv(Url[0],Url[1]);
 else if(mode==RM)
    wwwoffle_rm(Url[0]);
 else if(mode==READ)
    wwwoffle_read(Url[0]);
 else if(mode==WRITE)
    wwwoffle_write(Url[0]);

 exit(0);
}


/*++++++++++++++++++++++++++++++++++++++
  List the URLs within a directory of the cache.

  URL *Url The URL to list.
  ++++++++++++++++++++++++++++++++++++++*/

static void wwwoffle_ls(URL *Url)
{
 if(chdir(Url->proto))
   {PrintMessage(Warning,"Cannot change to directory '%s' [%!s].",Url->proto);return;}

 if(chdir(Url->host))
   {PrintMessage(Warning,"Cannot change to directory '%s/%s' [%!s].",Url->proto,Url->host);chdir("..");return;}

 if(strcmp(Url->path,"/"))
   {
    char *name=URLToFileName(Url);

    *name='D';
    ls(name);

    free(name);
   }
 else
   {
    struct dirent* ent;
    DIR *dir=opendir(".");

    if(!dir)
      {PrintMessage(Warning,"Cannot open current directory '%s/%s' [%!s].",Url->proto,Url->host);chdir("../..");return;}

    ent=readdir(dir);  /* skip .  */
    if(!ent)
      {PrintMessage(Warning,"Cannot read current directory '%s/%s' [%!s].",Url->proto,Url->host);closedir(dir);chdir("../..");return;}
    ent=readdir(dir);  /* skip .. */

    while((ent=readdir(dir)))
      {
       if(*ent->d_name=='D' && ent->d_name[strlen(ent->d_name)-1]!='~')
          ls(ent->d_name);
      }

    closedir(dir);
   }

 chdir("../..");
}


/*++++++++++++++++++++++++++++++++++++++
  List the URLs within the outgoing or lasttime special directory of the cache.

  char *name The name of the directory to list.
  ++++++++++++++++++++++++++++++++++++++*/

static void wwwoffle_ls_special(char *name)
{
 struct dirent* ent;
 DIR *dir;

 if(chdir(name))
   {PrintMessage(Warning,"Cannot change to directory '%s' [%!s].",name);return;}

 dir=opendir(".");

 if(!dir)
   {PrintMessage(Warning,"Cannot open current directory '%s' [%!s].",name);chdir("..");return;}

 ent=readdir(dir);  /* skip .  */
 if(!ent)
   {PrintMessage(Warning,"Cannot read current directory '%s' [%!s].",name);closedir(dir);chdir("..");return;}
 ent=readdir(dir);  /* skip .. */

 while((ent=readdir(dir)))
   {
    if((*ent->d_name=='D' || *ent->d_name=='O') && ent->d_name[strlen(ent->d_name)-1]!='~')
       ls(ent->d_name);
   }

 closedir(dir);

 chdir("..");
}


/*++++++++++++++++++++++++++++++++++++++
  List one file.

  char *file The name of the file to ls.
  ++++++++++++++++++++++++++++++++++++++*/

static void ls(char *file)
{
 static char *month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
 struct stat buf;
 time_t now=-1;

 if(now==-1)
    now=time(NULL);

 if(stat(file,&buf))
   {PrintMessage(Warning,"Cannot stat the file '%s' [%!s].",file);return;}
 else
   {
    char *url=FileNameToURL(file);

    if(url)
      {
       struct tm *tim=localtime(&buf.st_mtime);

       if(buf.st_mtime<(now-(180*24*3600)))
          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);
       else
          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);

       free(url);
      }
   }
}


/*++++++++++++++++++++++++++++++++++++++
  Move one URL or host to another.

  URL *Url1 The source URL.

  URL *Url2 The destination URL.
  ++++++++++++++++++++++++++++++++++++++*/

static void wwwoffle_mv(URL *Url1,URL *Url2)
{
 struct dirent* ent;
 DIR *dir;

 if(chdir(Url1->proto))
   {PrintMessage(Warning,"Cannot change to directory '%s' [%!s].",Url1->proto);return;}

 if(chdir(Url1->host))
   {PrintMessage(Warning,"Cannot change to directory '%s/%s' [%!s].",Url1->proto,Url1->host);chdir("..");return;}

 dir=opendir(".");

 if(!dir)
   {PrintMessage(Warning,"Cannot open current directory '%s/%s' [%!s].",Url1->proto,Url1->host);chdir("../..");return;}

 ent=readdir(dir);  /* skip .  */
 if(!ent)
   {PrintMessage(Warning,"Cannot read current directory '%s/%s' [%!s].",Url1->proto,Url1->host);closedir(dir);chdir("../..");return;}
 ent=readdir(dir);  /* skip .. */

 while((ent=readdir(dir)))
   {
    if(*ent->d_name=='D')
      {
       char *url1=FileNameToURL(ent->d_name);

       if(url1)
         {
          char *url2;
          URL *Url;
          char *path2,*name1,*name2;
          int fd2;

          Url=SplitURL(url1);
          url2=(char*)malloc(strlen(Url->pathp)+strlen(Url2->host)+strlen(Url2->proto)+8);
          sprintf(url2,"%s://%s%s",Url2->proto,Url2->host,Url->pathp);
          FreeURL(Url);

          name1=ent->d_name;

          Url=SplitURL(url2);
          name2=URLToFileName(Url);

          path2=(char*)malloc(strlen(Url2->proto)+strlen(Url2->host)+strlen(name2)+16);

          sprintf(path2,"../../%s",Url2->proto);
          mkdir(path2,0755);

          sprintf(path2,"../../%s/%s",Url2->proto,Url2->host);
          mkdir(path2,0755);

          *name1=*name2='D';
          sprintf(path2,"../../%s/%s/%s",Url2->proto,Url2->host,name2);
          rename(name1,path2);

          *name1=*name2='U';
          sprintf(path2,"../../%s/%s/%s",Url2->proto,Url2->host,name2);
          fd2=open(path2,O_WRONLY|O_CREAT|O_TRUNC);
          write_string(fd2,Url->name);
          close(fd2);
          unlink(name2);

          free(url1);
          free(url2);
          free(name2);
          free(path2);
          FreeURL(Url);
         }
      }
   }

 closedir(dir);

 chdir("../..");
}


/*++++++++++++++++++++++++++++++++++++++
  Delete a URL.

  URL *Url The URL to delete.
  ++++++++++++++++++++++++++++++++++++++*/

static void wwwoffle_rm(URL *Url)
{
 DeleteWebpageSpoolFile(Url,0);
}


/*++++++++++++++++++++++++++++++++++++++
  Read a URL and output on stdout.

  URL *Url The URL to read.
  ++++++++++++++++++++++++++++++++++++++*/

static void wwwoffle_read(URL *Url)
{
 char buffer[256];
 int n,spool=OpenWebpageSpoolFile(1,Url);

 if(spool==-1)
    return;

 init_buffer(spool);

 while((n=read_data(spool,buffer,256))>0)
    write_data(1,buffer,n);

 close(spool);
}


/*++++++++++++++++++++++++++++++++++++++
  Write a URL from the input on stdin.

  URL *Url The URL to write.
  ++++++++++++++++++++++++++++++++++++++*/

static void wwwoffle_write(URL *Url)
{
 char buffer[256];
 int n,spool=OpenWebpageSpoolFile(0,Url);

 if(spool==-1)
    return;

 init_buffer(0);

 while((n=read_data(0,buffer,256))>0)
    write_data(spool,buffer,n);

 close(spool);
}