changeset 102:3b573c8ef706

2003-2-6 Brian Masney <masneyb@gftp.org> * lib/gftp.h lib/protocols.c lib/rfc2068.c - put in new parse_time() function that should work across all locales. It uses strptime() internally. * lib/misc.c - remove note from close statement
author masneyb
date Fri, 07 Feb 2003 01:24:13 +0000
parents 5f9ad9facf0e
children d1258edb6d02
files ChangeLog lib/gftp.h lib/misc.c lib/protocols.c lib/rfc2068.c
diffstat 5 files changed, 43 insertions(+), 179 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Feb 07 01:00:10 2003 +0000
+++ b/ChangeLog	Fri Feb 07 01:24:13 2003 +0000
@@ -1,4 +1,10 @@
 2003-2-6 Brian Masney <masneyb@gftp.org>
+	* lib/gftp.h lib/protocols.c lib/rfc2068.c - put in new 
+	parse_time() function that should work across all locales.
+	It uses strptime() internally.
+
+	* lib/misc.c - remove note from close statement
+
 	* lib/config_file.c lib/gftp.h lib/options.h - remove ssh1_sftp_path
 	option
 
@@ -474,7 +480,7 @@
 
 	* cvsclean - added this script
 
-	* *.[ch] - added $Id: ChangeLog,v 1.56 2003/02/07 01:00:09 masneyb Exp $ tags
+	* *.[ch] - added $Id: ChangeLog,v 1.57 2003/02/07 01:24:12 masneyb Exp $ tags
 
 	* debian/* - updated files from Debian maintainer
 
--- a/lib/gftp.h	Fri Feb 07 01:00:10 2003 +0000
+++ b/lib/gftp.h	Fri Feb 07 01:24:13 2003 +0000
@@ -790,6 +790,9 @@
 void gftp_calc_kbs 			( gftp_transfer * tdata, 
 				 	  ssize_t num_read );
 
+time_t parse_time 			( char *str,
+					  char **endpos );
+
 int gftp_parse_ls 			( gftp_request * request,
 					  const char *lsoutput, 
 					  gftp_file *fle );
--- a/lib/misc.c	Fri Feb 07 01:00:10 2003 +0000
+++ b/lib/misc.c	Fri Feb 07 01:24:13 2003 +0000
@@ -17,9 +17,10 @@
 /*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA      */
 /*****************************************************************************/
 
+static const char cvsid[] = "$Id$";
+
 #include "gftp.h"
 #include "options.h"
-static const char cvsid[] = "$Id$";
 
 /* FIXME - this isn't right for all locales. Use glib's printf instead */
 char *
@@ -448,7 +449,7 @@
   if (fle->destfile)
     g_free (fle->destfile);
   if (fle->fd > 0)
-    close (fle->fd); /* FIXME - need to log a failure */
+    close (fle->fd);
   g_free (fle);
 }
 
--- a/lib/protocols.c	Fri Feb 07 01:00:10 2003 +0000
+++ b/lib/protocols.c	Fri Feb 07 01:24:13 2003 +0000
@@ -339,7 +339,7 @@
       ret = request->get_next_file (request, fle, fd);
 
 #if GLIB_MAJOR_VERSION > 1
-      if (fle->file != NULL && !g_utf8_validate (fle->file, -1, NULL))
+      if (ret >= 0 && fle->file != NULL && !g_utf8_validate (fle->file, -1, NULL))
         {
           error = NULL;
           if ((tempstr = g_locale_to_utf8 (fle->file, -1, &bread, 
@@ -1024,126 +1024,42 @@
 }
 
 
-static time_t
-parse_time (char **str)
+time_t
+parse_time (char *str, char **endpos)
 {
-  /* FIXME - doesn't work with all locales */
-  const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
-    "Aug", "Sep", "Oct", "Nov", "Dec" };
-  char *startpos, *endpos, *datepos;
-  struct tm curtime, tt;
+  struct tm curtime, *loctime;
+  char *tmppos;
   time_t t;
-  int i;
 
-  startpos = *str;
-  memset (&tt, 0, sizeof (tt));
-  tt.tm_isdst = -1;
-  if (isdigit ((int) startpos[0]) && startpos[2] == '-')
+  memset (&curtime, 0, sizeof (curtime));
+  curtime.tm_isdst = -1;
+  if (isdigit ((int) str[0]) && str[2] == '-')
     {
       /* This is how DOS will return the date/time */
       /* 07-06-99  12:57PM */
-      if ((endpos = strchr (startpos, '-')) == NULL)
-	{
-	  g_free (str);
-	  return (0);
-	}
-      tt.tm_mon = strtol (startpos, NULL, 10) - 1;
 
-      startpos = endpos + 1;
-      if ((endpos = strchr (startpos, '-')) == NULL)
-	{
-	  g_free (str);
-	  return (0);
-	}
-      tt.tm_mday = strtol (startpos, NULL, 10);
-
-      startpos = endpos + 1;
-      if ((endpos = strchr (startpos, ' ')) == NULL)
-	{
-	  g_free (str);
-	  return (0);
-	}
-      tt.tm_year = strtol (startpos, NULL, 10);
-
-      while (*endpos == ' ')
-	endpos++;
-
-      startpos = endpos + 1;
-      if ((endpos = strchr (startpos, ':')) == NULL)
-	{
-	  g_free (str);
-	  return (0);
-	}
-      tt.tm_hour = strtol (startpos, NULL, 10);
-
-      startpos = endpos + 1;
-      while ((*endpos != 'A') && (*endpos != 'P'))
-	endpos++;
-      if (*endpos == 'P')
-	{
-	  if (tt.tm_hour == 12)
-	    tt.tm_hour = 0;
-	  else
-	    tt.tm_hour += 12;
-	}
-      tt.tm_min = strtol (startpos, NULL, 10);
+      tmppos = strptime (str, "%m-%d-%y %I:%M%p", &curtime);
     }
   else
     {
       /* This is how most UNIX, Novell, and MacOS ftp servers send their time */
-      /* Jul 06 12:57 */
-      t = time (NULL);
-      curtime = *localtime (&t);
-
-      /* Get the month */
-      if ((endpos = strchr (startpos, ' ')) == NULL)
-	return (0);
-      for (i = 0; i < 12; i++)
-	{
-	  if (strncmp (months[i], startpos, 3) == 0)
-	    {
-	      tt.tm_mon = i;
-	      break;
-	    }
-	}
-
-      /* Skip the blanks till we get to the next entry */
-      startpos = endpos + 1;
-      while (*startpos == ' ')
-	startpos++;
-
-      /* Get the day */
-      if ((endpos = strchr (startpos, ' ')) == NULL)
-	return (0);
-      tt.tm_mday = strtol (startpos, NULL, 10);
+      /* Jul 06 12:57 or Jul  6  1999 */
 
-      /* Skip the blanks till we get to the next entry */
-      startpos = endpos + 1;
-      while (*startpos == ' ')
-	startpos++;
-
-      if ((datepos = strchr (startpos, ':')) != NULL)
-	{
-	  /* No year was specified. We will use the current year */
-	  tt.tm_year = curtime.tm_year;
+      if (strchr (str, ':') != NULL)
+        {
+          tmppos = strptime (str, "%h %d %H:%M", &curtime);
+          t = time (NULL);
+          loctime = localtime (&t);
+          curtime.tm_year = loctime->tm_year;
+        }
+      else
+        tmppos = strptime (str, "%h %d %Y", &curtime);
+    }
 
-	  /* If the date is in the future, than the year is from last year */
-	  if ((tt.tm_mon > curtime.tm_mon) ||
-	      ((tt.tm_mon == curtime.tm_mon) && tt.tm_mday > curtime.tm_mday))
-	    tt.tm_year--;
+  if (endpos != NULL)
+    *endpos = tmppos;
 
-	  /* Get the hours and the minutes */
-	  tt.tm_hour = strtol (startpos, NULL, 10);
-	  tt.tm_min = strtol (datepos + 1, NULL, 10);
-	}
-      else
-	{
-	  /* Get the year. The hours and minutes will be assumed to be 0 */
-	  tt.tm_year = strtol (startpos, NULL, 10) - 1900;
-	}
-    }
-  *str = startpos;
-  return (mktime (&tt));
+  return (mktime (&curtime));
 }
 
 
@@ -1295,7 +1211,7 @@
   while (*startpos == ' ')
     startpos++;
 
-  if ((fle->datetime = parse_time (&startpos)) == 0)
+  if ((fle->datetime = parse_time (startpos, &startpos)) == 0)
     return (GFTP_EFATAL);
 
   /* Skip the blanks till we get to the next entry */
@@ -1327,7 +1243,7 @@
   char *startpos;
 
   startpos = str;
-  if ((fle->datetime = parse_time (&startpos)) == 0)
+  if ((fle->datetime = parse_time (startpos, &startpos)) == 0)
     return (GFTP_EFATAL);
 
   /* No such thing on Windoze.. */
@@ -1376,7 +1292,7 @@
   fle->size = strtol (startpos, NULL, 10);
 
   startpos = goto_next_token (startpos);
-  if ((fle->datetime = parse_time (&startpos)) == 0)
+  if ((fle->datetime = parse_time (startpos, &startpos)) == 0)
     return (GFTP_EFATAL);
 
   startpos = goto_next_token (startpos);
--- a/lib/rfc2068.c	Fri Feb 07 01:00:10 2003 +0000
+++ b/lib/rfc2068.c	Fri Feb 07 01:24:13 2003 +0000
@@ -475,12 +475,8 @@
 static int
 parse_html_line (char *tempstr, gftp_file * fle)
 {
-  char months[13][3] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
-                        "Sep", "Oct", "Nov", "Dec"};
-  char *stpos, *pos, month[4];
-  struct tm t;
+  char *stpos, *pos;
   long units;
-  int i;
 
   memset (fle, 0, sizeof (*fle));
 
@@ -555,68 +551,10 @@
       pos++;
     }
 
-  /* Now get the date */
-  memset (&t, 0, sizeof (t));
-  memset (month, 0, sizeof (month));
-  if (strchr (pos, ':') != NULL)
-    {
-      if (*pos == '[')
-	pos++;
-      sscanf (pos, "%02d-%3s-%04d %02d:%02d", &t.tm_mday, month, &t.tm_year,
-	      &t.tm_hour, &t.tm_min);
-      while (*pos != ' ' && *pos != '\0')
-	pos++;
-      if (*pos == '\0')
-	return (1);
-
-      while (*pos == ' ')
-	pos++;
-
-      while (*pos != ' ' && *pos != '\0')
-	pos++;
-      if (*pos == '\0')
-	return (1);
+  if (*pos == '[')
+    pos++;
 
-      t.tm_year -= 1900;
-    }
-  else
-    {
-      pos++;
-      strncpy (month, pos, 3);
-      for (i=0; i<3 && *pos != '\0'; i++)
-        pos++;
-      if (*pos == '\0')
-	return (1);
-
-      while (*pos == ' ')
-	pos++;
-
-      t.tm_mday = strtol (pos, NULL, 10);
-      while (*pos != ' ' && *pos != '\0')
-	pos++;
-      if (*pos == '\0')
-	return (1);
-
-      while (*pos == ' ')
-	pos++;
-
-      t.tm_year = strtol (pos, NULL, 10) - 1900;
-      while (*pos != ' ' && *pos != '\0')
-	pos++;
-      if (*pos == '\0')
-	return (1);
-    }
-
-  for (i=0; i<12; i++)
-    {
-      if (strncmp (month, months[i], 3) == 0)
-        {
-          t.tm_mon = i;
-          break;
-        }
-    }
-
-  fle->datetime = mktime (&t);
+  fle->datetime = parse_time (pos, &pos);
 
   while (*pos == ' ' || *pos == ']')
     pos++;