1 /* mailstat.c -- stat a mailbox file, handling maildir-type mail directories */
3 /* Copyright (C) 2001 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
26 #include <bashtypes.h>
27 #include <posixstat.h>
32 # include <sys/param.h>
38 * Stat a file. If it's a maildir, check all messages
39 * in the maildir and present the grand total as a file.
40 * The fields in the 'struct stat' are from the mail directory.
41 * The following fields are emulated:
43 * st_nlink always 1, unless st_blocks is not present, in which case it's
44 * the total number of messages
45 * st_size total number of bytes in all files
46 * st_blocks total number of messages, if present in struct stat
47 * st_atime access time of newest file in maildir
48 * st_mtime modify time of newest file in maildir
49 * st_mode S_IFDIR changed to S_IFREG
51 * This is good enough for most mail-checking applications.
59 static struct stat st_new_last, st_ret_last;
60 struct stat st_ret, st_tmp;
63 char dir[PATH_MAX * 2], file[PATH_MAX * 2];
69 /* First see if it's a directory. */
70 if ((i = stat(path, st)) != 0 || S_ISDIR(st->st_mode) == 0)
73 if (strlen(path) > sizeof(dir) - 5)
86 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
91 st_ret.st_mode &= ~S_IFDIR;
92 st_ret.st_mode |= S_IFREG;
94 /* See if cur/ is present */
95 sprintf(dir, "%s/cur", path);
96 if (stat(dir, &st_tmp) || S_ISDIR(st_tmp.st_mode) == 0)
98 st_ret.st_atime = st_tmp.st_atime;
100 /* See if tmp/ is present */
101 sprintf(dir, "%s/tmp", path);
102 if (stat(dir, &st_tmp) || S_ISDIR(st_tmp.st_mode) == 0)
104 st_ret.st_mtime = st_tmp.st_mtime;
107 sprintf(dir, "%s/new", path);
108 if (stat(dir, &st_tmp) || S_ISDIR(st_tmp.st_mode) == 0)
110 st_ret.st_mtime = st_tmp.st_mtime;
112 /* Optimization - if new/ didn't change, nothing else did. */
113 if (st_tmp.st_dev == st_new_last.st_dev &&
114 st_tmp.st_ino == st_new_last.st_ino &&
115 st_tmp.st_atime == st_new_last.st_atime &&
116 st_tmp.st_mtime == st_new_last.st_mtime)
121 st_new_last = st_tmp;
123 /* Loop over new/ and cur/ */
124 for (i = 0; i < 2; i++)
126 sprintf(dir, "%s/%s", path, i ? "cur" : "new");
127 sprintf(file, "%s/", dir);
129 if ((dd = opendir(dir)) == NULL)
131 while ((fn = readdir(dd)) != NULL)
133 if (fn->d_name[0] == '.' || strlen(fn->d_name) + l >= sizeof(file))
135 strcpy(file + l, fn->d_name);
136 if (stat(file, &st_tmp) != 0)
138 st_ret.st_size += st_tmp.st_size;
139 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
144 if (st_tmp.st_atime != st_tmp.st_mtime && st_tmp.st_atime > atime)
145 atime = st_tmp.st_atime;
146 if (st_tmp.st_mtime > mtime)
147 mtime = st_tmp.st_mtime;
152 /* if (atime) */ /* Set atime even if cur/ is empty */
153 st_ret.st_atime = atime;
155 st_ret.st_mtime = mtime;
157 *st = st_ret_last = st_ret;