Changed passive_mode flag for a more elaborate field.
[uftps.git] / file_stats.c
blob59683274ee193e82df6029f7506759af396ecbfc
1 /*
2 * User FTP Server, Share folders over FTP without being root.
3 * Copyright (C) 2008 Isaac Jurado
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
8 * version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "uftps.h"
21 #include <sys/stat.h>
22 #include <time.h>
23 #include <stdio.h>
27 * File status query.
29 * Here we implement some commands (SIZE, MDTM) that share the need of calling
30 * stat() over a given file, no matter the fields looked up. It's handy to
31 * merge all of them here so safety checks are not replicated.
33 void file_stats (int type)
35 int l = 0, e;
36 struct stat s;
37 struct tm t;
39 if (SS.arg == NULL)
41 warning("Stat type %d without argument", type);
42 reply_c("501 Argument required.\r\n");
43 return;
45 expand_arg();
47 e = lstat(SS.arg, &s);
48 if (e == -1 || !S_ISREG(s.st_mode) || !S_ISDIR(s.st_mode))
50 if (e == -1)
51 error("Stating file %s", SS.arg);
52 else
53 warning("Path %s is not file nor directory", SS.arg);
54 reply_c("550 Could not stat file.\r\n");
55 return;
58 switch (type)
60 case 0: /* MDTM */
61 gmtime_r(&s.st_mtime, &t);
62 l = snprintf(SS.aux, LINE_SIZE,
63 "213 %4d%02d%02d%02d%02d%02d\r\n", t.tm_year + 1900,
64 t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
65 break;
67 case 1: /* SIZE */
68 l = snprintf(SS.aux, LINE_SIZE, "213 %lld\r\n",
69 (long long) s.st_size);
72 reply(SS.aux, l);