1 /* Parsing FTP `ls' output.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation,
6 This file is part of GNU Wget.
8 GNU Wget is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GNU Wget is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Wget. If not, see <http://www.gnu.org/licenses/>.
21 Additional permission under GNU GPL version 3 section 7
23 If you modify this program, or any covered work, by linking or
24 combining it with the OpenSSL project's OpenSSL library (or a
25 modified version of that library), containing parts covered by the
26 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
27 grants you additional permission to convey the resulting work.
28 Corresponding Source for a non-source form of such a combination
29 shall include the source code for the parts of OpenSSL used as well
30 as that of the covered work. */
43 #include "convert.h" /* for html_quote_string prototype */
44 #include "retr.h" /* for output_stream */
46 /* Converts symbolic permissions to number-style ones, e.g. string
47 rwxr-xr-x to 755. For now, it knows nothing of
48 setuid/setgid/sticky. ACLs are ignored. */
50 symperms (const char *s)
56 for (i = 0; i < 3; i++, s += 3)
59 perms += (((s[0] == 'r') << 2) + ((s[1] == 'w') << 1) +
60 (s[2] == 'x' || s[2] == 's'));
66 /* Cleans a line of text so that it can be consistently parsed. Destroys
67 <CR> and <LF> in case that thay occur at the end of the line and
68 replaces all <TAB> character with <SPACE>. Returns the length of the
71 clean_line(char *line)
73 int len = strlen (line);
75 if (line[len - 1] == '\n')
78 if (line[len - 1] == '\r')
80 for ( ; *line ; line++ ) if (*line == '\t') *line = ' ';
84 /* Convert the Un*x-ish style directory listing stored in FILE to a
85 linked list of fileinfo (system-independent) entries. The contents
86 of FILE are considered to be produced by the standard Unix `ls -la'
87 output (whatever that might be). BSD (no group) and SYSV (with
88 group) listings are handled.
90 The time stamps are stored in a separate variable, time_t
91 compatible (I hope). The timezones are ignored. */
92 static struct fileinfo *
93 ftp_parse_unix_ls (const char *file, int ignore_perms)
96 static const char *months[] = {
97 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
98 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
100 int next, len, i, error, ignore;
101 int year, month, day; /* for time analysis */
102 int hour, min, sec, ptype;
103 struct tm timestruct, *tnow;
106 char *line, *tok, *ptok; /* tokenizer */
107 struct fileinfo *dir, *l, cur; /* list creation */
109 fp = fopen (file, "rb");
112 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
117 /* Line loop to end of file: */
118 while ((line = read_whole_line (fp)) != NULL)
120 len = clean_line (line);
121 /* Skip if total... */
122 if (!strncasecmp (line, "total", 5))
127 /* Get the first token (permissions). */
128 tok = strtok (line, " ");
138 /* Decide whether we deal with a file or a directory. */
142 cur.type = FT_PLAINFILE;
143 DEBUGP (("PLAINFILE; "));
146 cur.type = FT_DIRECTORY;
147 DEBUGP (("DIRECTORY; "));
150 cur.type = FT_SYMLINK;
151 DEBUGP (("SYMLINK; "));
154 cur.type = FT_UNKNOWN;
155 DEBUGP (("UNKNOWN; "));
170 /*cur.perms = 1023;*/ /* #### What is this? --hniksic */
173 DEBUGP (("implicit perms %0o; ", cur.perms));
177 cur.perms = symperms (tok + 1);
178 DEBUGP (("perms %0o; ", cur.perms));
181 error = ignore = 0; /* Erroneous and ignoring entries are
182 treated equally for now. */
183 year = hour = min = sec = 0; /* Silence the compiler. */
187 /* While there are tokens on the line, parse them. Next is the
188 number of tokens left until the filename.
190 Use the month-name token as the "anchor" (the place where the
191 position wrt the file name is "known"). When a month name is
192 encountered, `next' is set to 5. Also, the preceding
193 characters are parsed to get the file size.
195 This tactic is quite dubious when it comes to
196 internationalization issues (non-English month names), but it
200 (tok = strtok (NULL, " ")) != NULL)
203 if (next < 0) /* a month name was not encountered */
205 for (i = 0; i < 12; i++)
206 if (!strcmp (tok, months[i]))
208 /* If we got a month, it means the token before it is the
209 size, and the filename is three tokens away. */
214 /* Parse the previous token with str_to_wgint. */
217 /* Something has gone wrong during parsing. */
222 size = str_to_wgint (ptok, NULL, 10);
223 if (size == WGINT_MAX && errno == ERANGE)
224 /* Out of range -- ignore the size. #### Should
225 we refuse to start the download. */
229 DEBUGP (("size: %s; ", number_to_static_string(cur.size)));
233 DEBUGP (("month: %s; ", months[month]));
236 else if (next == 4) /* days */
238 if (tok[1]) /* two-digit... */
239 day = 10 * (*tok - '0') + tok[1] - '0';
240 else /* ...or one-digit */
242 DEBUGP (("day: %d; ", day));
246 /* This ought to be either the time, or the year. Let's
249 If we have a number x, it's a year. If we have x:y,
250 it's hours and minutes. If we have x:y:z, z are
253 min = hour = sec = 0;
254 /* We must deal with digits. */
255 if (c_isdigit (*tok))
257 /* Suppose it's year. */
258 for (; c_isdigit (*tok); tok++)
259 year = (*tok - '0') + 10 * year;
262 /* This means these were hours! */
267 /* Get the minutes... */
268 for (; c_isdigit (*tok); tok++)
269 min = (*tok - '0') + 10 * min;
272 /* ...and the seconds. */
274 for (; c_isdigit (*tok); tok++)
275 sec = (*tok - '0') + 10 * sec;
280 DEBUGP (("year: %d (no tm); ", year));
282 DEBUGP (("time: %02d:%02d:%02d (no yr); ", hour, min, sec));
284 else if (next == 2) /* The file name */
289 /* Since the file name may contain a SPC, it is possible
290 for strtok to handle it wrong. */
291 fnlen = strlen (tok);
292 if (fnlen < len - (tok - line))
294 /* So we have a SPC in the file name. Restore the
297 /* If the file is a symbolic link, it should have a
299 if (cur.type == FT_SYMLINK)
301 p = strstr (tok, " -> ");
307 cur.linkto = xstrdup (p + 4);
308 DEBUGP (("link to: %s\n", cur.linkto));
309 /* And separate it from the file name. */
313 /* If we have the filename, add it to the list of files or
315 /* "." and ".." are an exception! */
316 if (!strcmp (tok, ".") || !strcmp (tok, ".."))
318 DEBUGP (("\nIgnoring `.' and `..'; "));
322 /* Some FTP sites choose to have ls -F as their default
323 LIST output, which marks the symlinks with a trailing
324 `@', directory names with a trailing `/' and
325 executables with a trailing `*'. This is no problem
326 unless encountering a symbolic link ending with `@',
327 or an executable ending with `*' on a server without
328 default -F output. I believe these cases are very
330 fnlen = strlen (tok); /* re-calculate `fnlen' */
331 cur.name = xmalloc (fnlen + 1);
332 memcpy (cur.name, tok, fnlen + 1);
335 if (cur.type == FT_DIRECTORY && cur.name[fnlen - 1] == '/')
337 cur.name[fnlen - 1] = '\0';
338 DEBUGP (("trailing `/' on dir.\n"));
340 else if (cur.type == FT_SYMLINK && cur.name[fnlen - 1] == '@')
342 cur.name[fnlen - 1] = '\0';
343 DEBUGP (("trailing `@' on link.\n"));
345 else if (cur.type == FT_PLAINFILE
346 && (cur.perms & 0111)
347 && cur.name[fnlen - 1] == '*')
349 cur.name[fnlen - 1] = '\0';
350 DEBUGP (("trailing `*' on exec.\n"));
361 if (!cur.name || (cur.type == FT_SYMLINK && !cur.linkto))
364 DEBUGP (("%s\n", cur.name ? cur.name : ""));
368 DEBUGP (("Skipping.\n"));
369 xfree_null (cur.name);
370 xfree_null (cur.linkto);
377 l = dir = xnew (struct fileinfo);
378 memcpy (l, &cur, sizeof (cur));
379 l->prev = l->next = NULL;
384 l->next = xnew (struct fileinfo);
386 memcpy (l, &cur, sizeof (cur));
389 /* Get the current time. */
390 timenow = time (NULL);
391 tnow = localtime (&timenow);
392 /* Build the time-stamp (the idea by zaga@fly.cc.fer.hr). */
393 timestruct.tm_sec = sec;
394 timestruct.tm_min = min;
395 timestruct.tm_hour = hour;
396 timestruct.tm_mday = day;
397 timestruct.tm_mon = month;
400 /* Some listings will not specify the year if it is "obvious"
401 that the file was from the previous year. E.g. if today
402 is 97-01-12, and you see a file of Dec 15th, its year is
403 1996, not 1997. Thanks to Vladimir Volovich for
405 if (month > tnow->tm_mon)
406 timestruct.tm_year = tnow->tm_year - 1;
408 timestruct.tm_year = tnow->tm_year;
411 timestruct.tm_year = year;
412 if (timestruct.tm_year >= 1900)
413 timestruct.tm_year -= 1900;
414 timestruct.tm_wday = 0;
415 timestruct.tm_yday = 0;
416 timestruct.tm_isdst = -1;
417 l->tstamp = mktime (×truct); /* store the time-stamp */
427 static struct fileinfo *
428 ftp_parse_winnt_ls (const char *file)
432 int year, month, day; /* for time analysis */
434 struct tm timestruct;
436 char *line, *tok; /* tokenizer */
438 struct fileinfo *dir, *l, cur; /* list creation */
440 fp = fopen (file, "rb");
443 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
448 /* Line loop to end of file: */
449 while ((line = read_whole_line (fp)) != NULL)
451 len = clean_line (line);
453 /* Name begins at 39 column of the listing if date presented in `mm-dd-yy'
454 format or at 41 column if date presented in `mm-dd-yyyy' format. Thus,
455 we cannot extract name before we parse date. Using this information we
456 also can recognize filenames that begin with a series of space
457 characters (but who really wants to use such filenames anyway?). */
458 if (len < 40) goto continue_loop;
459 filename = line + 39;
461 /* First column: mm-dd-yy or mm-dd-yyyy. Should atoi() on the month fail,
462 january will be assumed. */
463 tok = strtok(line, "-");
464 if (tok == NULL) goto continue_loop;
465 month = atoi(tok) - 1;
466 if (month < 0) month = 0;
467 tok = strtok(NULL, "-");
468 if (tok == NULL) goto continue_loop;
470 tok = strtok(NULL, " ");
471 if (tok == NULL) goto continue_loop;
473 /* Assuming the epoch starting at 1.1.1970 */
478 else if (year >= 1900)
483 /* Now it is possible to determine the position of the first symbol in
485 cur.name = xstrdup(filename);
486 DEBUGP (("Name: '%s'\n", cur.name));
489 /* Second column: hh:mm[AP]M, listing does not contain value for
491 tok = strtok(NULL, ":");
492 if (tok == NULL) goto continue_loop;
494 tok = strtok(NULL, "M");
495 if (tok == NULL) goto continue_loop;
497 /* Adjust hour from AM/PM. Just for the record, the sequence goes
498 11:00AM, 12:00PM, 01:00PM ... 11:00PM, 12:00AM, 01:00AM . */
500 if (hour == 12) hour = 0;
501 if (*tok == 'P') hour += 12;
503 DEBUGP (("YYYY/MM/DD HH:MM - %d/%02d/%02d %02d:%02d\n",
504 year+1900, month, day, hour, min));
506 /* Build the time-stamp (copy & paste from above) */
507 timestruct.tm_sec = 0;
508 timestruct.tm_min = min;
509 timestruct.tm_hour = hour;
510 timestruct.tm_mday = day;
511 timestruct.tm_mon = month;
512 timestruct.tm_year = year;
513 timestruct.tm_wday = 0;
514 timestruct.tm_yday = 0;
515 timestruct.tm_isdst = -1;
516 cur.tstamp = mktime (×truct); /* store the time-stamp */
517 cur.ptype = TT_HOUR_MIN;
519 DEBUGP (("Timestamp: %ld\n", cur.tstamp));
521 /* Third column: Either file length, or <DIR>. We also set the
522 permissions (guessed as 0644 for plain files and 0755 for
523 directories as the listing does not give us a clue) and filetype
525 tok = strtok(NULL, " ");
526 if (tok == NULL) goto continue_loop;
527 while ((tok != NULL) && (*tok == '\0')) tok = strtok(NULL, " ");
528 if (tok == NULL) goto continue_loop;
531 cur.type = FT_DIRECTORY;
534 DEBUGP (("Directory\n"));
539 cur.type = FT_PLAINFILE;
541 size = str_to_wgint (tok, NULL, 10);
542 if (size == WGINT_MAX && errno == ERANGE)
543 cur.size = 0; /* overflow */
547 DEBUGP (("File, size %s bytes\n", number_to_static_string (cur.size)));
552 /* And put everything into the linked list */
555 l = dir = xnew (struct fileinfo);
556 memcpy (l, &cur, sizeof (cur));
557 l->prev = l->next = NULL;
562 l->next = xnew (struct fileinfo);
564 memcpy (l, &cur, sizeof (cur));
578 /* Convert the VMS-style directory listing stored in "file" to a
579 linked list of fileinfo (system-independent) entries. The contents
580 of FILE are considered to be produced by the standard VMS
581 "DIRECTORY [/SIZE [= ALL]] /DATE [/OWNER] [/PROTECTION]" command,
582 more or less. (Different VMS FTP servers may have different headers,
583 and may not supply the same data, but all should be subsets of this.)
585 VMS normally provides local (server) time and date information.
586 Define the logical name or environment variable
587 "WGET_TIMEZONE_DIFFERENTIAL" (seconds) to adjust the receiving local
588 times if different from the remote local times.
591 Added code to eliminate "^" escape characters from ODS5 extended file
592 names. The TCPIP FTP server (V5.4) seems to prefer requests which do
593 not use the escaped names which it provides.
596 #define VMS_DEFAULT_PROT_FILE 0644
597 #define VMS_DEFAULT_PROT_DIR 0755
602 Delete ODS5 extended file name escape characters ("^") in the
604 Note that the current scheme does not handle all EFN cases, but it
605 could be made more complicated.
608 static void eat_carets( char *str)
609 /* char *str; Source pointer. */
611 char *strd; /* Destination pointer. */
616 /* Skip ahead to the first "^", if any. */
617 while ((*str != '\0') && (*str != '^'))
620 /* If no caret was found, quit early. */
623 /* Shift characters leftward as carets are found. */
630 /* Found a caret. Skip it, and check the next character. */
632 prop = char_prop[ uchr];
635 /* Hex digit. Get char code from this and next hex digit. */
638 hdgt = uchr- '0'; /* '0' - '9' -> 0 - 9. */
642 hdgt = ((uchr- 'A')& 7)+ 10; /* [Aa] - [Ff] -> 10 - 15. */
644 hdgt <<= 4; /* X16. */
645 uchr = *(++str); /* Next char must be hex digit. */
648 uchr = hdgt+ uchr- '0';
652 uchr = hdgt+ ((uchr- 'A')& 15)+ 10;
655 else if (uchr == '_')
657 /* Convert escaped "_" to " ". */
660 else if (uchr == '/')
662 /* Convert escaped "/" (invalid Zip) to "?" (invalid VMS). */
663 /* Note that this is a left-over from Info-ZIP code, and is
664 probably of little value here, except perhaps to avoid
665 directory confusion which an unconverted slash might cause.
669 /* Else, not a hex digit. Must be a simple escaped character
670 (or Unicode, which is not yet handled here).
673 /* Else, not a caret. Use as-is. */
676 /* Advance destination and source pointers. */
680 /* Terminate the destination string. */
686 static struct fileinfo *
687 ftp_parse_vms_ls (const char *file)
693 struct tm *timestruct;
696 char *line, *tok; /* tokenizer */
697 struct fileinfo *dir, *l, cur; /* list creation */
699 fp = fopen (file, "r");
702 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
707 /* Skip blank lines, Directory heading, and more blank lines. */
709 j = 0; /* Expecting initial blank line(s). */
712 line = read_whole_line (fp);
719 i = clean_line (line);
722 xfree (line); /* Free useless line storage. */
723 continue; /* Blank line. Keep looking. */
727 if ((j == 0) && (line[ i- 1] == ']'))
729 /* Found Directory heading line. Next non-blank line
734 else if (!strncmp (line, "Total of ", 9))
736 /* Found "Total of ..." footing line. No valid data
737 will follow (empty directory).
739 xfree (line); /* Free useless line storage. */
740 line = NULL; /* Arrange for early exit. */
745 break; /* Must be significant data. */
748 xfree (line); /* Free useless line storage. */
752 /* Read remainder of file until the next blank line or EOF. */
758 /* The first token is the file name. After a long name, other
759 data may be on the following line. A valid directory name ends
760 in ".DIR;1" (any case), although some VMS FTP servers may omit
761 the version number (";1").
764 tok = strtok(line, " ");
765 if (tok == NULL) tok = line;
766 DEBUGP (("file name: '%s'\n", tok));
768 /* Stripping the version number on a VMS system would be wrong.
769 It may be foolish on a non-VMS system, too, but that's someone
770 else's problem. (Define PRESERVE_VMS_VERSIONS for proper
771 operation on other operating systems.)
774 ODS5 extended file names may contain escaped semi-colons, so
775 the version number is identified as right-side decimal digits
776 led by a non-escaped semi-colon. It may be absent.
779 #if (!defined( __VMS) && !defined( PRESERVE_VMS_VERSIONS))
780 for (p = tok + strlen (tok); (--p > tok) && c_isdigit( *p); );
781 if ((*p == ';') && (*(p- 1) != '^'))
785 #endif /* (!defined( __VMS) && !defined( PRESERVE_VMS_VERSIONS)) */
788 Eliminate "^" escape characters from ODS5 extended file name.
789 (A caret is invalid in an ODS2 name, so this is always safe.)
792 DEBUGP (("file name-^: '%s'\n", tok));
794 /* Differentiate between a directory and any other file. A VMS
795 listing may not include file protections (permissions). Set a
796 default permissions value (according to the file type), which
797 may be overwritten later. Store directory names without the
798 ".DIR;1" file type and version number, as the plain name is
799 what will work in a CWD command.
802 if (!strncasecmp((tok + (len - 4)), ".DIR", 4))
804 *(tok+ (len - 4)) = '\0'; /* Discard ".DIR". */
805 cur.type = FT_DIRECTORY;
806 cur.perms = VMS_DEFAULT_PROT_DIR;
807 DEBUGP (("Directory (nv)\n"));
809 else if (!strncasecmp ((tok + (len - 6)), ".DIR;1", 6))
811 *(tok+ (len - 6)) = '\0'; /* Discard ".DIR;1". */
812 cur.type = FT_DIRECTORY;
813 cur.perms = VMS_DEFAULT_PROT_DIR;
814 DEBUGP (("Directory (v)\n"));
818 cur.type = FT_PLAINFILE;
819 cur.perms = VMS_DEFAULT_PROT_FILE;
822 cur.name = xstrdup (tok);
823 DEBUGP (("Name: '%s'\n", cur.name));
825 /* Null the date and time string. */
828 /* VMS lacks symbolic links. */
831 /* VMS reports file sizes in (512-byte) disk blocks, not bytes,
832 hence useless for an integrity check based on byte-count.
837 /* Get token 2, if any. A long name may force all other data onto
838 a second line. If needed, read the second line.
841 tok = strtok (NULL, " ");
844 DEBUGP (("Getting additional line.\n"));
846 line = read_whole_line (fp);
849 DEBUGP (("EOF. Leaving listing parser.\n"));
853 /* Second line must begin with " ". Otherwise, it's a first
854 line (and we may be confused).
858 /* Blank line. End of significant file listing. */
859 DEBUGP (("Blank line. Leaving listing parser.\n"));
860 xfree (line); /* Free useless line storage. */
863 else if (line[ 0] != ' ')
865 DEBUGP (("Non-blank in column 1. Must be a new file name?\n"));
870 tok = strtok (line, " ");
873 /* Unexpected non-empty but apparently blank line. */
874 DEBUGP (("Null token. Leaving listing parser.\n"));
875 xfree (line); /* Free useless line storage. */
881 /* Analyze tokens. (Order is not significant, except date must
884 Size: ddd or ddd/ddd (where "ddd" is a decimal number)
886 Time: HH:MM or HH:MM:SS or HH:MM:SS.CC
887 Owner: [user] or [user,group]
888 Protection: (ppp,ppp,ppp,ppp) (where "ppp" is "RWED" or some
889 subset thereof, for System, Owner, Group, World.
891 If permission is lacking, info may be replaced by the string:
892 "No privilege for attempted operation".
896 DEBUGP (("Token: >%s<: ", tok));
898 if ((strlen (tok) < 12) && (strchr( tok, '-') != NULL))
901 DEBUGP (("Date.\n"));
902 strcpy( date_str, tok);
903 strcat( date_str, " ");
905 else if ((strlen (tok) < 12) && (strchr( tok, ':') != NULL))
911 (sizeof( date_str)- strlen (date_str) - 1));
912 DEBUGP (("Date time: >%s<\n", date_str));
914 else if (strchr ( tok, '[') != NULL)
916 /* Owner. (Ignore.) */
917 DEBUGP (("Owner.\n"));
919 else if (strchr (tok, '(') != NULL)
921 /* Protections (permissions). */
924 for (i = 0; i < strlen( tok); i++)
958 DEBUGP (("Prot. perms = %0o.\n", cur.perms));
962 /* Nondescript. Probably size(s), probably in blocks.
963 Could be "No privilege ..." message. (Ignore.)
965 DEBUGP (("Ignored (size?).\n"));
968 tok = strtok (NULL, " ");
971 /* Tokens exhausted. Interpret the data, and fill in the
974 /* Fill tm timestruct according to date-time string. Fractional
975 seconds are ignored. Default to current time, if conversion
978 timenow = time( NULL);
979 timestruct = localtime( &timenow );
980 strptime( date_str, "%d-%b-%Y %H:%M:%S", timestruct);
982 /* Convert struct tm local time to time_t local time. */
983 timenow = mktime (timestruct);
984 /* Offset local time according to environment variable (seconds). */
985 if ((tok = getenv ( "WGET_TIMEZONE_DIFFERENTIAL")) != NULL)
988 DEBUGP (("Time differential = %d.\n", dt));
998 cur.tstamp = timenow; /* Store the time-stamp. */
999 DEBUGP (("Timestamp: %ld\n", cur.tstamp));
1000 cur.ptype = TT_HOUR_MIN;
1002 /* Add the data for this item to the linked list, */
1005 l = dir = (struct fileinfo *)xmalloc (sizeof (struct fileinfo));
1006 memcpy (l, &cur, sizeof (cur));
1007 l->prev = l->next = NULL;
1012 l->next = (struct fileinfo *)xmalloc (sizeof (struct fileinfo));
1014 memcpy (l, &cur, sizeof (cur));
1018 /* Free old line storage. Read a new line. */
1020 line = read_whole_line (fp);
1023 i = clean_line (line);
1026 /* Blank line. End of significant file listing. */
1027 xfree (line); /* Free useless line storage. */
1038 /* This function switches between the correct parsing routine depending on
1039 the SYSTEM_TYPE. The system type should be based on the result of the
1040 "SYST" response of the FTP server. According to this repsonse we will
1041 use on of the three different listing parsers that cover the most of FTP
1042 servers used nowadays. */
1045 ftp_parse_ls (const char *file, const enum stype system_type)
1047 switch (system_type)
1050 return ftp_parse_unix_ls (file, 0);
1053 /* Detect whether the listing is simulating the UNIX format */
1056 fp = fopen (file, "rb");
1059 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
1064 /* If the first character of the file is '0'-'9', it's WINNT
1066 if (c >= '0' && c <='9')
1067 return ftp_parse_winnt_ls (file);
1069 return ftp_parse_unix_ls (file, 1);
1072 return ftp_parse_vms_ls (file);
1074 return ftp_parse_unix_ls (file, 1);
1076 logprintf (LOG_NOTQUIET, _("\
1077 Unsupported listing type, trying Unix listing parser.\n"));
1078 return ftp_parse_unix_ls (file, 0);
1082 /* Stuff for creating FTP index. */
1084 /* The function creates an HTML index containing references to given
1085 directories and files on the appropriate host. The references are
1088 ftp_index (const char *file, struct url *u, struct fileinfo *f)
1092 char *htcldir; /* HTML-clean dir name */
1093 char *htclfile; /* HTML-clean file name */
1094 char *urlclfile; /* URL-clean file name */
1098 fp = fopen (file, "wb");
1101 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
1109 char *tmpu, *tmpp; /* temporary, clean user and passwd */
1111 tmpu = url_escape (u->user);
1112 tmpp = u->passwd ? url_escape (u->passwd) : NULL;
1114 upwd = concat_strings (tmpu, ":", tmpp, "@", (char *) 0);
1116 upwd = concat_strings (tmpu, "@", (char *) 0);
1121 upwd = xstrdup ("");
1123 htcldir = html_quote_string (u->dir);
1125 fprintf (fp, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n");
1126 fprintf (fp, "<html>\n<head>\n<title>");
1127 fprintf (fp, _("Index of /%s on %s:%d"), htcldir, u->host, u->port);
1128 fprintf (fp, "</title>\n</head>\n<body>\n<h1>");
1129 fprintf (fp, _("Index of /%s on %s:%d"), htcldir, u->host, u->port);
1130 fprintf (fp, "</h1>\n<hr>\n<pre>\n");
1135 if (f->tstamp != -1)
1137 /* #### Should we translate the months? Or, even better, use
1139 static const char *months[] = {
1140 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
1141 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1143 time_t tstamp = f->tstamp;
1144 struct tm *ptm = localtime (&tstamp);
1146 fprintf (fp, "%d %s %02d ", ptm->tm_year + 1900, months[ptm->tm_mon],
1148 if (f->ptype == TT_HOUR_MIN)
1149 fprintf (fp, "%02d:%02d ", ptm->tm_hour, ptm->tm_min);
1154 fprintf (fp, _("time unknown "));
1158 fprintf (fp, _("File "));
1161 fprintf (fp, _("Directory "));
1164 fprintf (fp, _("Link "));
1167 fprintf (fp, _("Not sure "));
1170 htclfile = html_quote_string (f->name);
1171 urlclfile = url_escape_unsafe_and_reserved (f->name);
1172 fprintf (fp, "<a href=\"ftp://%s%s:%d", upwd, u->host, u->port);
1175 /* XXX: Should probably URL-escape dir components here, rather
1176 * than just HTML-escape, for consistency with the next bit where
1177 * we use urlclfile for the file component. Anyway, this is safer
1178 * than what we had... */
1179 fprintf (fp, "%s", htcldir);
1182 fprintf (fp, "%s", urlclfile);
1183 if (f->type == FT_DIRECTORY)
1185 fprintf (fp, "\">%s", htclfile);
1186 if (f->type == FT_DIRECTORY)
1188 fprintf (fp, "</a> ");
1189 if (f->type == FT_PLAINFILE)
1190 fprintf (fp, _(" (%s bytes)"), number_to_static_string (f->size));
1191 else if (f->type == FT_SYMLINK)
1192 fprintf (fp, "-> %s", f->linkto ? f->linkto : "(nil)");
1198 fprintf (fp, "</pre>\n</body>\n</html>\n");