26 static long totai(
long year,
long month,
long mday)
29 if (month >= 2) month -= 2;
30 else { month += 10; --year; }
31 result = (mday - 1) * 10 + 5 + 306 * month;
33 if (result == 365) { year -= 3; result = 1460; }
34 else result += 365 * (year % 4);
36 result += 1461 * (year % 25);
38 if (result == 36524) { year -= 3; result = 146096; }
39 else { result += 36524 * (year % 4); }
41 result += 146097 * (year - 5);
43 return result * 86400;
59 base = -(
totai(t->tm_year + 1900,t->tm_mon,t->tm_mday) + t->tm_hour * 3600 + t->tm_min * 60 + t->tm_sec);
71 now = time((time_t *) 0) -
base;
75 if ((
now % 86400) < 0) --day;
77 year = 5 + day / 146097;
79 if (day < 0) { day += 146097; --year; }
81 if (day == 146096) { year += 3; day = 36524; }
82 else { year += day / 36524; day %= 36524; }
87 if (day == 1460) { year += 3; day = 365; }
88 else { year += day / 365; day %= 365; }
90 if ((day + 5) / 306 >= 10) ++year;
108 t =
totai(year,month,mday);
109 if (
now - t < 350 * 86400)
115 static int check(
char *buf,
char *monthname)
117 if ((buf[0] != monthname[0]) && (buf[0] != monthname[0] - 32))
return 0;
118 if ((buf[1] != monthname[1]) && (buf[1] != monthname[1] - 32))
return 0;
119 if ((buf[2] != monthname[2]) && (buf[2] != monthname[2] - 32))
return 0;
124 "jan",
"feb",
"mar",
"apr",
"may",
"jun",
"jul",
"aug",
"sep",
"oct",
"nov",
"dec"
131 for (i = 0;i < 12;++i)
140 u = u * 10 + (*buf++ -
'0');
177 for (j = 1;j < len;++j) {
179 fp->
name = buf + j + 1;
202 fp->
id = buf + i + 1;
203 fp->
idlen = j - i - 1;
240 for (j = 1;j < len;++j)
241 if ((buf[j] ==
' ') && (buf[j - 1] !=
' ')) {
248 if ((j - i == 6) && (buf[i] ==
'f'))
270 if ((j - i == 4) && (buf[i + 1] ==
':')) {
272 minute =
getlong(buf + i + 2,2);
276 }
else if ((j - i == 5) && (buf[i + 2] ==
':')) {
278 minute =
getlong(buf + i + 3,2);
283 else if (j - i >= 4) {
291 fp->
name = buf + j + 1;
299 while ((i < len) && (buf[i] ==
' ')) ++i;
309 for (i = 0;i + 3 < fp->
namelen;++i)
310 if (fp->
name[i] ==
' ')
311 if (fp->
name[i + 1] ==
'-')
312 if (fp->
name[i + 2] ==
'>')
313 if (fp->
name[i + 3] ==
' ') {
319 if ((buf[1] ==
' ') || (buf[1] ==
'['))
321 if (fp->
name[0] ==
' ')
322 if (fp->
name[1] ==
' ')
323 if (fp->
name[2] ==
' ') {
336 for (i = 0;i < len;++i)
343 if (buf[i - 4] ==
'.')
344 if (buf[i - 3] ==
'D')
345 if (buf[i - 2] ==
'I')
346 if (buf[i - 1] ==
'R') {
352 while (buf[i] !=
' ')
if (++i == len)
return 0;
353 while (buf[i] ==
' ')
if (++i == len)
return 0;
354 while (buf[i] !=
' ')
if (++i == len)
return 0;
355 while (buf[i] ==
' ')
if (++i == len)
return 0;
357 while (buf[j] !=
'-')
if (++j == len)
return 0;
359 while (buf[j] ==
'-')
if (++j == len)
return 0;
361 while (buf[j] !=
'-')
if (++j == len)
return 0;
363 if (month < 0)
return 0;
364 while (buf[j] ==
'-')
if (++j == len)
return 0;
366 while (buf[j] !=
' ')
if (++j == len)
return 0;
368 while (buf[j] ==
' ')
if (++j == len)
return 0;
370 while (buf[j] !=
':')
if (++j == len)
return 0;
372 while (buf[j] ==
':')
if (++j == len)
return 0;
374 while ((buf[j] !=
':') && (buf[j] !=
' '))
if (++j == len)
return 0;
375 minute =
getlong(buf + i,j - i);
379 fp->
mtime =
base +
totai(year,month,mday) + hour * 3600 + minute * 60;
388 if ((*buf >=
'0') && (*buf <=
'9')) {
391 while (buf[j] !=
'-')
if (++j == len)
return 0;
392 month =
getlong(buf + i,j - i) - 1;
393 while (buf[j] ==
'-')
if (++j == len)
return 0;
395 while (buf[j] !=
'-')
if (++j == len)
return 0;
397 while (buf[j] ==
'-')
if (++j == len)
return 0;
399 while (buf[j] !=
' ')
if (++j == len)
return 0;
401 if (year < 50) year += 2000;
402 if (year < 1000) year += 1900;
403 while (buf[j] ==
' ')
if (++j == len)
return 0;
405 while (buf[j] !=
':')
if (++j == len)
return 0;
407 while (buf[j] ==
':')
if (++j == len)
return 0;
409 while ((buf[j] !=
'A') && (buf[j] !=
'P'))
if (++j == len)
return 0;
410 minute =
getlong(buf + i,j - i);
411 if (hour == 12) hour = 0;
412 if (buf[j] ==
'A')
if (++j == len)
return 0;
413 if (buf[j] ==
'P') { hour += 12;
if (++j == len)
return 0; }
414 if (buf[j] ==
'M')
if (++j == len)
return 0;
416 while (buf[j] ==
' ')
if (++j == len)
return 0;
419 while (buf[j] !=
' ')
if (++j == len)
return 0;
423 while (buf[j] !=
' ')
if (++j == len)
return 0;
428 while (buf[j] ==
' ')
if (++j == len)
return 0;
435 fp->
mtime =
base +
totai(year,month,mday) + hour * 3600 + minute * 60;
#define FTPPARSE_MTIME_REMOTEDAY
static int check(char *buf, char *monthname)
static long totai(long year, long month, long mday)
#define FTPPARSE_MTIME_UNKNOWN
#define FTPPARSE_SIZE_UNKNOWN
static long getlong(char *buf, int len)
static int flagneedcurrentyear
#define FTPPARSE_MTIME_LOCAL
static long guesstai(long month, long mday)
static int getmonth(char *buf, int len)
#define FTPPARSE_SIZE_BINARY
int ftpparse(struct ftpparse *fp, char *buf, int len)
static void initbase(void)
#define FTPPARSE_ID_UNKNOWN
#define FTPPARSE_MTIME_REMOTEMINUTE
static void initnow(void)