]> sjero.net Git - wget/blobdiff - src/ftp-ls.c
Fix compiler warnings
[wget] / src / ftp-ls.c
index 2ff57cc05b2d63c9355a088a100cf81c4d2b0ca7..d9077a375e55541d86bd142eb982d4da4d8060cf 100644 (file)
@@ -68,16 +68,17 @@ symperms (const char *s)
    replaces all <TAB> character with <SPACE>. Returns the length of the
    modified line. */
 static int
-clean_line(char *line)
+clean_line (char *line, int len)
 {
-  int len = strlen (line);
-  if (!len) return 0;
-  if (line[len - 1] == '\n')
+  if (len <= 0) return 0;
+
+  while (len > 0 && (line[len - 1] == '\n' || line[len - 1] == '\r'))
     line[--len] = '\0';
+
   if (!len) return 0;
-  if (line[len - 1] == '\r')
-    line[--len] = '\0';
+
   for ( ; *line ; line++ ) if (*line == '\t') *line = ' ';
+
   return len;
 }
 
@@ -102,8 +103,9 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   int hour, min, sec, ptype;
   struct tm timestruct, *tnow;
   time_t timenow;
+  size_t bufsize = 0;
 
-  char *line, *tok, *ptok;      /* tokenizer */
+  char *line = NULL, *tok, *ptok;      /* tokenizer */
   struct fileinfo *dir, *l, cur; /* list creation */
 
   fp = fopen (file, "rb");
@@ -115,22 +117,16 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   dir = l = NULL;
 
   /* Line loop to end of file: */
-  while ((line = read_whole_line (fp)) != NULL)
+  while ((len = getline (&line, &bufsize, fp)) > 0)
     {
-      len = clean_line (line);
+      len = clean_line (line, len);
       /* Skip if total...  */
       if (!strncasecmp (line, "total", 5))
-        {
-          xfree (line);
-          continue;
-        }
+        continue;
       /* Get the first token (permissions).  */
       tok = strtok (line, " ");
       if (!tok)
-        {
-          xfree (line);
-          continue;
-        }
+        continue;
 
       cur.name = NULL;
       cur.linkto = NULL;
@@ -203,7 +199,7 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
           if (next < 0)         /* a month name was not encountered */
             {
               for (i = 0; i < 12; i++)
-                if (!strcmp (tok, months[i]))
+                if (!strcasecmp (tok, months[i]))
                   break;
               /* If we got a month, it means the token before it is the
                  size, and the filename is three tokens away.  */
@@ -368,7 +364,6 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
           DEBUGP (("Skipping.\n"));
           xfree_null (cur.name);
           xfree_null (cur.linkto);
-          xfree (line);
           continue;
         }
 
@@ -416,10 +411,9 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
       timestruct.tm_isdst = -1;
       l->tstamp = mktime (&timestruct); /* store the time-stamp */
       l->ptype = ptype;
-
-      xfree (line);
     }
 
+  xfree (line);
   fclose (fp);
   return dir;
 }
@@ -431,9 +425,11 @@ ftp_parse_winnt_ls (const char *file)
   int len;
   int year, month, day;         /* for time analysis */
   int hour, min;
+  size_t bufsize = 0;
   struct tm timestruct;
 
-  char *line, *tok;             /* tokenizer */
+  char *line = NULL, *tok;             /* tokenizer */
+  char *filename;
   struct fileinfo *dir, *l, cur; /* list creation */
 
   fp = fopen (file, "rb");
@@ -445,23 +441,20 @@ ftp_parse_winnt_ls (const char *file)
   dir = l = NULL;
 
   /* Line loop to end of file: */
-  while ((line = read_whole_line (fp)) != NULL)
+  while ((len = getline (&line, &bufsize, fp)) > 0)
     {
-      len = clean_line (line);
-
-      /* Extracting name is a bit of black magic and we have to do it
-         before `strtok' inserted extra \0 characters in the line
-         string. For the moment let us just suppose that the name starts at
-         column 39 of the listing. This way we could also recognize
-         filenames that begin with a series of space characters (but who
-         really wants to use such filenames anyway?). */
+      len = clean_line (line, len);
+
+      /* Name begins at 39 column of the listing if date presented in `mm-dd-yy'
+         format or at 41 column if date presented in `mm-dd-yyyy' format. Thus,
+         we cannot extract name before we parse date. Using this information we
+         also can recognize filenames that begin with a series of space
+         characters (but who really wants to use such filenames anyway?). */
       if (len < 40) continue;
-      tok = line + 39;
-      cur.name = xstrdup(tok);
-      DEBUGP (("Name: '%s'\n", cur.name));
+      filename = line + 39;
 
-      /* First column: mm-dd-yy. Should atoi() on the month fail, january
-         will be assumed.  */
+      /* First column: mm-dd-yy or mm-dd-yyyy. Should atoi() on the month fail,
+         january will be assumed.  */
       tok = strtok(line, "-");
       if (tok == NULL) continue;
       month = atoi(tok) - 1;
@@ -473,7 +466,20 @@ ftp_parse_winnt_ls (const char *file)
       if (tok == NULL) continue;
       year = atoi(tok);
       /* Assuming the epoch starting at 1.1.1970 */
-      if (year <= 70) year += 100;
+      if (year <= 70)
+        {
+          year += 100;
+        }
+      else if (year >= 1900)
+        {
+          year -= 1900;
+          filename += 2;
+        }
+      /* Now it is possible to determine the position of the first symbol in
+         filename. */
+      cur.name = xstrdup(filename);
+      DEBUGP (("Name: '%s'\n", cur.name));
+
 
       /* Second column: hh:mm[AP]M, listing does not contain value for
          seconds */
@@ -553,10 +559,9 @@ ftp_parse_winnt_ls (const char *file)
           memcpy (l, &cur, sizeof (cur));
           l->next = NULL;
         }
-
-      xfree (line);
     }
 
+  xfree (line);
   fclose(fp);
   return dir;
 }
@@ -677,11 +682,12 @@ ftp_parse_vms_ls (const char *file)
   FILE *fp;
   int dt, i, j, len;
   int perms;
+  size_t bufsize = 0;
   time_t timenow;
   struct tm *timestruct;
   char date_str[ 32];
 
-  char *line, *tok;             /* tokenizer */
+  char *line = NULL, *tok; /* tokenizer */
   struct fileinfo *dir, *l, cur; /* list creation */
 
   fp = fopen (file, "r");
@@ -694,52 +700,34 @@ ftp_parse_vms_ls (const char *file)
 
   /* Skip blank lines, Directory heading, and more blank lines. */
 
-  j = 0; /* Expecting initial blank line(s). */
-  while (1)
+  for (j = 0; (i = getline (&line, &bufsize, fp)) > 0; )
     {
-      line = read_whole_line (fp);
-      if (line == NULL)
+      i = clean_line (line, i);
+      if (i <= 0)
+        continue; /* Ignore blank line. */
+
+      if ((j == 0) && (line[i - 1] == ']'))
+        {
+          /* Found Directory heading line.  Next non-blank line
+          is significant. */
+          j = 1;
+        }
+      else if (!strncmp (line, "Total of ", 9))
         {
-        break;
+          /* Found "Total of ..." footing line.  No valid data
+             will follow (empty directory). */
+          i = 0; /* Arrange for early exit. */
+          break;
         }
       else
         {
-          i = clean_line (line);
-          if (i <= 0)
-            {
-              xfree (line); /* Free useless line storage. */
-              continue; /* Blank line.  Keep looking. */
-            }
-          else
-            {
-              if ((j == 0) && (line[ i- 1] == ']'))
-                {
-                  /* Found Directory heading line.  Next non-blank line
-                  is significant.
-                  */
-                  j = 1;
-                }
-              else if (!strncmp (line, "Total of ", 9))
-                {
-                  /* Found "Total of ..." footing line.  No valid data
-                     will follow (empty directory).
-                  */
-                  xfree (line); /* Free useless line storage. */
-                  line = NULL; /* Arrange for early exit. */
-                  break;
-                }
-              else
-                {
-                  break; /* Must be significant data. */
-                }
-            }
-          xfree (line); /* Free useless line storage. */
+          break; /* Must be significant data. */
         }
     }
 
   /* Read remainder of file until the next blank line or EOF. */
 
-  while (line != NULL)
+  while (i > 0)
     {
       char *p;
 
@@ -830,9 +818,8 @@ ftp_parse_vms_ls (const char *file)
       if (tok == NULL)
         {
           DEBUGP (("Getting additional line.\n"));
-          xfree (line);
-          line = read_whole_line (fp);
-          if (!line)
+          i = getline (&line, &bufsize, fp);
+          if (i <= 0)
             {
               DEBUGP (("EOF.  Leaving listing parser.\n"));
               break;
@@ -841,14 +828,14 @@ ftp_parse_vms_ls (const char *file)
           /* Second line must begin with " ".  Otherwise, it's a first
              line (and we may be confused).
           */
+          i = clean_line (line, i);
           if (i <= 0)
             {
               /* Blank line.  End of significant file listing. */
               DEBUGP (("Blank line.  Leaving listing parser.\n"));
-              xfree (line); /* Free useless line storage. */
               break;
             }
-          else if (line[ 0] != ' ')
+          else if (line[0] != ' ')
             {
               DEBUGP (("Non-blank in column 1.  Must be a new file name?\n"));
               continue;
@@ -860,7 +847,6 @@ ftp_parse_vms_ls (const char *file)
                 {
                   /* Unexpected non-empty but apparently blank line. */
                   DEBUGP (("Null token.  Leaving listing parser.\n"));
-                  xfree (line); /* Free useless line storage. */
                   break;
                 }
             }
@@ -899,7 +885,7 @@ ftp_parse_vms_ls (const char *file)
                        (sizeof( date_str)- strlen (date_str) - 1));
               DEBUGP (("Date time: >%s<\n", date_str));
             }
-          else if (strchr ( tok, '[') != NULL)
+          else if (strchr (tok, '[') != NULL)
             {
               /* Owner.  (Ignore.) */
               DEBUGP (("Owner.\n"));
@@ -909,7 +895,8 @@ ftp_parse_vms_ls (const char *file)
               /* Protections (permissions). */
               perms = 0;
               j = 0;
-              for (i = 0; i < strlen( tok); i++)
+              /*FIXME: Should not be using the variable like this. */
+              for (i = 0; i < (int) strlen(tok); i++)
                 {
                   switch (tok[ i])
                     {
@@ -1003,21 +990,19 @@ ftp_parse_vms_ls (const char *file)
           l->next = NULL;
         }
 
-      /* Free old line storage.  Read a new line. */
-      xfree (line);
-      line = read_whole_line (fp);
-      if (line != NULL)
+      i = getline (&line, &bufsize, fp);
+      if (i > 0)
         {
-          i = clean_line (line);
+          i = clean_line (line, i);
           if (i <= 0)
-           {
+            {
               /* Blank line.  End of significant file listing. */
-              xfree (line); /* Free useless line storage. */
-             break;
-           }
+              break;
+            }
         }
     }
 
+  xfree (line);
   fclose (fp);
   return dir;
 }