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;
}
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");
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;
DEBUGP (("Skipping.\n"));
xfree_null (cur.name);
xfree_null (cur.linkto);
- xfree (line);
continue;
}
timestruct.tm_isdst = -1;
l->tstamp = mktime (×truct); /* store the time-stamp */
l->ptype = ptype;
-
- xfree (line);
}
+ xfree (line);
fclose (fp);
return dir;
}
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");
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?). */
- if (len < 40) goto continue_loop;
- tok = line + 39;
- cur.name = xstrdup(tok);
- DEBUGP (("Name: '%s'\n", cur.name));
-
- /* First column: mm-dd-yy. Should atoi() on the month fail, january
- will be assumed. */
+ 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;
+ filename = line + 39;
+
+ /* 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) goto continue_loop;
+ if (tok == NULL) continue;
month = atoi(tok) - 1;
if (month < 0) month = 0;
tok = strtok(NULL, "-");
- if (tok == NULL) goto continue_loop;
+ if (tok == NULL) continue;
day = atoi(tok);
tok = strtok(NULL, " ");
- if (tok == NULL) goto continue_loop;
+ 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 */
tok = strtok(NULL, ":");
- if (tok == NULL) goto continue_loop;
+ if (tok == NULL) continue;
hour = atoi(tok);
tok = strtok(NULL, "M");
- if (tok == NULL) goto continue_loop;
+ if (tok == NULL) continue;
min = atoi(tok);
/* Adjust hour from AM/PM. Just for the record, the sequence goes
11:00AM, 12:00PM, 01:00PM ... 11:00PM, 12:00AM, 01:00AM . */
directories as the listing does not give us a clue) and filetype
here. */
tok = strtok(NULL, " ");
- if (tok == NULL) goto continue_loop;
+ if (tok == NULL) continue;
while ((tok != NULL) && (*tok == '\0')) tok = strtok(NULL, " ");
- if (tok == NULL) goto continue_loop;
+ if (tok == NULL) continue;
if (*tok == '<')
{
cur.type = FT_DIRECTORY;
memcpy (l, &cur, sizeof (cur));
l->next = NULL;
}
-
-continue_loop:
- xfree (line);
}
+ xfree (line);
fclose(fp);
return dir;
}
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");
/* 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;
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;
/* 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;
{
/* Unexpected non-empty but apparently blank line. */
DEBUGP (("Null token. Leaving listing parser.\n"));
- xfree (line); /* Free useless line storage. */
break;
}
}
(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"));
/* Protections (permissions). */
perms = 0;
j = 0;
- for (i = 0; i < strlen( tok); i++)
+ for (i = 0; i < strlen(tok); i++)
{
switch (tok[ i])
{
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;
}