~ubuntu-branches/ubuntu/precise/wget/precise-proposed

« back to all changes in this revision

Viewing changes to src/ftp-ls.c

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2005-06-26 16:46:25 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050626164625-jjcde8hyztx7xq7o
Tags: 1.10-2
* wget-fix_error--save-headers patch from upstream
  (closes: Bug#314728)
* don't pattern-match server redirects patch from upstream
  (closes: Bug#163243)
* correct de.po typos
  (closes: Bug#313883)
* wget-E_html_behind_file_counting fix problem with adding the
  numbers after the html extension
* updated Standards-Version: to 3.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
#include "utils.h"
48
48
#include "ftp.h"
49
49
#include "url.h"
 
50
#include "convert.h"            /* for html_quote_string prototype */
 
51
 
 
52
extern FILE *output_stream;
50
53
 
51
54
/* Converts symbolic permissions to number-style ones, e.g. string
52
55
   rwxr-xr-x to 755.  For now, it knows nothing of
119
122
  dir = l = NULL;
120
123
 
121
124
  /* Line loop to end of file: */
122
 
  while ((line = read_whole_line (fp)))
 
125
  while ((line = read_whole_line (fp)) != NULL)
123
126
    {
124
127
      len = clean_line (line);
125
128
      /* Skip if total...  */
198
201
         This tactic is quite dubious when it comes to
199
202
         internationalization issues (non-English month names), but it
200
203
         works for now.  */
201
 
      while ((tok = strtok (NULL, " ")))
 
204
      while ((tok = strtok (NULL, " ")) != NULL)
202
205
        {
203
206
          --next;
204
207
          if (next < 0)         /* a month name was not encountered */
210
213
                 size, and the filename is three tokens away.  */
211
214
              if (i != 12)
212
215
                {
 
216
                  wgint size;
 
217
 
 
218
                  /* Back up to the beginning of the previous token
 
219
                     and parse it with str_to_wgint.  */
213
220
                  char *t = tok - 2;
214
 
                  long mul = 1;
215
 
 
216
 
                  for (cur.size = 0; t > line && ISDIGIT (*t); mul *= 10, t--)
217
 
                    cur.size += mul * (*t - '0');
 
221
                  while (t > line && ISDIGIT (*t))
 
222
                    --t;
218
223
                  if (t == line)
219
224
                    {
220
 
                      /* Something is seriously wrong.  */
 
225
                      /* Something has gone wrong during parsing. */
221
226
                      error = 1;
222
227
                      break;
223
228
                    }
 
229
                  errno = 0;
 
230
                  size = str_to_wgint (t, NULL, 10);
 
231
                  if (size == WGINT_MAX && errno == ERANGE)
 
232
                    /* Out of range -- ignore the size.  #### Should
 
233
                       we refuse to start the download.  */
 
234
                    cur.size = 0;
 
235
                  else
 
236
                    cur.size = size;
 
237
 
224
238
                  month = i;
225
239
                  next = 5;
226
240
                  DEBUGP (("month: %s; ", months[month]));
358
372
      if (error || ignore)
359
373
        {
360
374
          DEBUGP (("Skipping.\n"));
361
 
          FREE_MAYBE (cur.name);
362
 
          FREE_MAYBE (cur.linkto);
 
375
          xfree_null (cur.name);
 
376
          xfree_null (cur.linkto);
363
377
          xfree (line);
364
378
          continue;
365
379
        }
366
380
 
367
381
      if (!dir)
368
382
        {
369
 
          l = dir = (struct fileinfo *)xmalloc (sizeof (struct fileinfo));
 
383
          l = dir = xnew (struct fileinfo);
370
384
          memcpy (l, &cur, sizeof (cur));
371
385
          l->prev = l->next = NULL;
372
386
        }
373
387
      else
374
388
        {
375
389
          cur.prev = l;
376
 
          l->next = (struct fileinfo *)xmalloc (sizeof (struct fileinfo));
 
390
          l->next = xnew (struct fileinfo);
377
391
          l = l->next;
378
392
          memcpy (l, &cur, sizeof (cur));
379
393
          l->next = NULL;
436
450
  dir = l = NULL;
437
451
 
438
452
  /* Line loop to end of file: */
439
 
  while ((line = read_whole_line (fp)))
 
453
  while ((line = read_whole_line (fp)) != NULL)
440
454
    {
441
455
      len = clean_line (line);
442
456
 
454
468
      /* First column: mm-dd-yy. Should atoi() on the month fail, january
455
469
         will be assumed.  */
456
470
      tok = strtok(line, "-");
 
471
      if (tok == NULL) continue;
457
472
      month = atoi(tok) - 1;
458
473
      if (month < 0) month = 0;
459
474
      tok = strtok(NULL, "-");
 
475
      if (tok == NULL) continue;
460
476
      day = atoi(tok);
461
477
      tok = strtok(NULL, " ");
 
478
      if (tok == NULL) continue;
462
479
      year = atoi(tok);
463
480
      /* Assuming the epoch starting at 1.1.1970 */
464
481
      if (year <= 70) year += 100;
466
483
      /* Second column: hh:mm[AP]M, listing does not contain value for
467
484
         seconds */
468
485
      tok = strtok(NULL,  ":");
 
486
      if (tok == NULL) continue;
469
487
      hour = atoi(tok);
470
488
      tok = strtok(NULL,  "M");
 
489
      if (tok == NULL) continue;
471
490
      min = atoi(tok);
472
491
      /* Adjust hour from AM/PM. Just for the record, the sequence goes
473
492
         11:00AM, 12:00PM, 01:00PM ... 11:00PM, 12:00AM, 01:00AM . */
497
516
         directories as the listing does not give us a clue) and filetype
498
517
         here. */
499
518
      tok = strtok(NULL, " ");
500
 
      while (*tok == '\0')  tok = strtok(NULL, " ");
 
519
      if (tok == NULL) continue;
 
520
      while ((tok != NULL) && (*tok == '\0'))  tok = strtok(NULL, " ");
 
521
      if (tok == NULL) continue;
501
522
      if (*tok == '<')
502
523
        {
503
524
          cur.type  = FT_DIRECTORY;
507
528
        }
508
529
      else
509
530
        {
 
531
          wgint size;
510
532
          cur.type  = FT_PLAINFILE;
511
 
          cur.size  = atoi(tok);
 
533
          errno = 0;
 
534
          size = str_to_wgint (tok, NULL, 10);
 
535
          if (size == WGINT_MAX && errno == ERANGE)
 
536
            cur.size = 0;       /* overflow */
 
537
          else
 
538
            cur.size = size;
512
539
          cur.perms = 0644;
513
 
          DEBUGP(("File, size %ld bytes\n", cur.size));
 
540
          DEBUGP(("File, size %s bytes\n", number_to_static_string (cur.size)));
514
541
        }
515
542
 
516
543
      cur.linkto = NULL;
518
545
      /* And put everything into the linked list */
519
546
      if (!dir)
520
547
        {
521
 
          l = dir = (struct fileinfo *)xmalloc (sizeof (struct fileinfo));
 
548
          l = dir = xnew (struct fileinfo);
522
549
          memcpy (l, &cur, sizeof (cur));
523
550
          l->prev = l->next = NULL;
524
551
        }
525
552
      else
526
553
        {
527
554
          cur.prev = l;
528
 
          l->next = (struct fileinfo *)xmalloc (sizeof (struct fileinfo));
 
555
          l->next = xnew (struct fileinfo);
529
556
          l = l->next;
530
557
          memcpy (l, &cur, sizeof (cur));
531
558
          l->next = NULL;
532
559
        }
533
560
 
534
 
      xfree(line);
 
561
      xfree (line);
535
562
    }
536
563
 
537
564
  fclose(fp);
589
616
 
590
617
  /* Skip empty line. */
591
618
  line = read_whole_line (fp);
592
 
  if (line)
593
 
    xfree (line);
 
619
  xfree_null (line);
594
620
 
595
621
  /* Skip "Directory PUB$DEVICE[PUB]" */
596
622
  line = read_whole_line (fp);
597
 
  if (line)
598
 
    xfree (line);
 
623
  xfree_null (line);
599
624
 
600
625
  /* Skip empty line. */
601
626
  line = read_whole_line (fp);
602
 
  if (line)
603
 
    xfree (line);
 
627
  xfree_null (line);
604
628
 
605
629
  /* Line loop to end of file: */
606
 
  while ((line = read_whole_line (fp)))
 
630
  while ((line = read_whole_line (fp)) != NULL)
607
631
    {
608
632
      char *p;
609
633
      i = clean_line (line);
678
702
      /* Third/Second column: Date DD-MMM-YYYY. */
679
703
 
680
704
      tok = strtok(NULL, "-");
 
705
      if (tok == NULL) continue;
681
706
      DEBUGP(("day: '%s'\n",tok));
682
707
      day = atoi(tok);
683
708
      tok = strtok(NULL, "-");
695
720
      /* Uknown months are mapped to January */
696
721
      month = i % 12 ; 
697
722
      tok = strtok (NULL, " ");
 
723
      if (tok == NULL) continue;
698
724
      year = atoi (tok) - 1900;
699
725
      DEBUGP(("date parsed\n"));
700
726
 
701
727
      /* Fourth/Third column: Time hh:mm[:ss] */
702
728
      tok = strtok (NULL, " ");
703
 
      hour = min = sec = 0;
 
729
      if (tok == NULL) continue;
 
730
      min = sec = 0;
704
731
      p = tok;
705
732
      hour = atoi (p);
706
733
      for (; *p && *p != ':'; ++p);
730
757
      /* Skip the fifth column */
731
758
 
732
759
      tok = strtok(NULL, " ");
 
760
      if (tok == NULL) continue;
733
761
 
734
762
      /* Sixth column: Permissions */
735
763
 
736
764
      tok = strtok(NULL, ","); /* Skip the VMS-specific SYSTEM permissons */
 
765
      if (tok == NULL) continue;
737
766
      tok = strtok(NULL, ")");
738
767
      if (tok == NULL)
739
768
        {
783
812
  switch (system_type)
784
813
    {
785
814
    case ST_UNIX:
786
 
      return ftp_parse_unix_ls (file, FALSE);
 
815
      return ftp_parse_unix_ls (file, 0);
787
816
    case ST_WINNT:
788
817
      {
789
818
        /* Detect whether the listing is simulating the UNIX format */
802
831
        if (c >= '0' && c <='9')
803
832
          return ftp_parse_winnt_ls (file);
804
833
        else
805
 
          return ftp_parse_unix_ls (file, TRUE);
 
834
          return ftp_parse_unix_ls (file, 1);
806
835
      }
807
836
    case ST_VMS:
808
837
      return ftp_parse_vms_ls (file);
809
838
    case ST_MACOS:
810
 
      return ftp_parse_unix_ls (file, TRUE);
 
839
      return ftp_parse_unix_ls (file, 1);
811
840
    default:
812
841
      logprintf (LOG_NOTQUIET, _("\
813
842
Unsupported listing type, trying Unix listing parser.\n"));
814
 
      return ftp_parse_unix_ls (file, FALSE);
 
843
      return ftp_parse_unix_ls (file, 0);
815
844
    }
816
845
}
817
846
 
827
856
  char *upwd;
828
857
  char *htclfile;               /* HTML-clean file name */
829
858
 
830
 
  if (!opt.dfp)
 
859
  if (!output_stream)
831
860
    {
832
861
      fp = fopen (file, "wb");
833
862
      if (!fp)
837
866
        }
838
867
    }
839
868
  else
840
 
    fp = opt.dfp;
 
869
    fp = output_stream;
841
870
  if (u->user)
842
871
    {
843
872
      char *tmpu, *tmpp;        /* temporary, clean user and passwd */
844
873
 
845
874
      tmpu = url_escape (u->user);
846
875
      tmpp = u->passwd ? url_escape (u->passwd) : NULL;
847
 
      upwd = (char *)xmalloc (strlen (tmpu)
848
 
                             + (tmpp ? (1 + strlen (tmpp)) : 0) + 2);
849
 
      sprintf (upwd, "%s%s%s@", tmpu, tmpp ? ":" : "", tmpp ? tmpp : "");
 
876
      if (tmpp)
 
877
        upwd = concat_strings (tmpu, ":", tmpp, "@", (char *) 0);
 
878
      else
 
879
        upwd = concat_strings (tmpu, "@", (char *) 0);
850
880
      xfree (tmpu);
851
 
      FREE_MAYBE (tmpp);
 
881
      xfree_null (tmpp);
852
882
    }
853
883
  else
854
884
    upwd = xstrdup ("");
865
895
        {
866
896
          /* #### Should we translate the months?  Or, even better, use
867
897
             ISO 8601 dates?  */
868
 
          static char *months[] = {
 
898
          static const char *months[] = {
869
899
            "Jan", "Feb", "Mar", "Apr", "May", "Jun",
870
900
            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
871
901
          };
896
926
          break;
897
927
        }
898
928
      htclfile = html_quote_string (f->name);
899
 
      fprintf (fp, "<a href=\"ftp://%s%s:%hu", upwd, u->host, u->port);
 
929
      fprintf (fp, "<a href=\"ftp://%s%s:%d", upwd, u->host, u->port);
900
930
      if (*u->dir != '/')
901
931
        putc ('/', fp);
902
932
      fprintf (fp, "%s", u->dir);
910
940
        putc ('/', fp);
911
941
      fprintf (fp, "</a> ");
912
942
      if (f->type == FT_PLAINFILE)
913
 
        fprintf (fp, _(" (%s bytes)"), legible (f->size));
 
943
        fprintf (fp, _(" (%s bytes)"), with_thousand_seps (f->size));
914
944
      else if (f->type == FT_SYMLINK)
915
945
        fprintf (fp, "-> %s", f->linkto ? f->linkto : "(nil)");
916
946
      putc ('\n', fp);
919
949
    }
920
950
  fprintf (fp, "</pre>\n</body>\n</html>\n");
921
951
  xfree (upwd);
922
 
  if (!opt.dfp)
 
952
  if (!output_stream)
923
953
    fclose (fp);
924
954
  else
925
955
    fflush (fp);