The following should count directory entries immediately within a given directory, excluding "." and ".." if present (not all filesystems list those explicitly, although they still otherwise work as expected). If compiled with -DTEST, it also provides a main() function to exercise the file counting function. It compiles without warnings and appears to work as expected on at least OS X 10.11.6 (El Capitan), Ubuntu 16.04.2 LTS (Xenial Xerus), and Solaris 9. I checked man pages for various functions and include files rather than going from memory, to be reasonably sure I was doing everything the proper way, so that it would be as portable as possible. The actual function is int countfiles(const char *dirpath), and returns the number of files, or -1 in event of error (with errno reflecting the error that caused it to fail, whether opening the directory, or occasionally some problem reading the directory). There's probably a thing or two I could have done better (a static const int for -1 rather than two uses of the actual constant might have saved four bytes), but other than that, it's pretty clean, IMO.
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
#include <string.h>
#ifdef TEST
extern int countfiles(const char *);
#include <stdio.h>
int
main(int argc, char **argv)
{
int arg, count, rc = 0;
for (arg = 1; arg < argc; ++arg) {
if ((count = countfiles(argv[arg])) == -1) {
perror(argv[arg]);
rc = 1;
}
else
(void) printf("%s: %d\n", argv[arg], count);
}
return (rc);
}
#endif
int
countfiles(const char *dirpath)
{
int count = 0, errno_save;
DIR *dirp;
struct dirent *dp;
if ((dirp = opendir(dirpath)) == NULL)
return (-1);
errno = 0; /* because readdir() returns NULL on both EOF and error */
while ((dp = readdir(dirp)) !=NULL) {
if (strcmp(dp->d_name, ".") == 0 ||
strcmp(dp->d_name, "..") == 0)
continue;
++count;
}
if (errno != 0) {
errno_save = errno;
(void) closedir(dirp);
errno = errno_save;
return (-1);
}
(void) closedir(dirp);
return (count);
▸ quoted from Galen Johnson
}
On Mar 3, 2017, at 09:11, Galen Johnson <user-87f955643e3d@xymon.invalid> wrote:
Thanks for the offer, Kenneth. I have a simple script (ls $dir | wc -l) as well but I had thought that it was already a part of the Xymon base. It would be nice to have it as an option to DIR and if I can find the time, I may look into dusting off my C skills and take a user-f38fa604f572@xymon.invalid using the terms "skills" along side "C" is a stretch for me :-).
=G=
From: Kenneth S. Petersen <user-bdc293fcf8df@xymon.invalid <mailto:user-bdc293fcf8df@xymon.invalid>>
Sent: Friday, March 3, 2017 3:37 AM
To: Galen Johnson; Galen Johnson
Cc: xymon at xymon.com <mailto:xymon at xymon.com>
Subject: SV: [Xymon] file counts
Hi Galen,
I’m using the count file feature but with a external script on windows along with bbwin 0.13 or 0.12
It a two part script, one cfg file in the bbwin etc folder and one vbs in the bbwin ext folder.
Then call it from bbwin.cfg
Actually it’s the bbwin, fsmon.vbs that I have altered so it fits my needs.
Let me know if you want the script.
Med Venlig Hilsen
Kenneth S. Petersen
IT Konsulent – Stougaards IT
Wiuffsvej 3, DK-4540 Fårevejle, Denmark
Email: user-376c3952b9e2@xymon.invalid <mailto:user-376c3952b9e2@xymon.invalid> Mob.+45 2163 2325
▸ quoted from Galen Johnson
Fra: Xymon [mailto:xymon-bounces at xymon.com <mailto:xymon-bounces at xymon.com>] På vegne af Galen Johnson
Sendt: 3. marts 2017 01:30
Til: Galen Johnson <user-87f955643e3d@xymon.invalid <mailto:user-87f955643e3d@xymon.invalid>>
Cc: xymon at xymon.com <mailto:xymon at xymon.com>
Emne: Re: [Xymon] file counts
Apparently, I wasn't the only one with this need. I found a thread from 2011. I really don't want to go that route so consider this a feature request that DIR can support more than just size but file count as well.
=G=
On Thu, Mar 2, 2017 at 2:20 PM, Galen Johnson <user-87f955643e3d@xymon.invalid <mailto:user-87f955643e3d@xymon.invalid>> wrote:
Hey,
I could've sworn that Xymon had a way to count the number of files in a particular directory (natively) and you could set up alerts based on number of files. Apparently, I was wrong or I'm not seeing it in the docs. Obviously, I could get clever with file ages but that's just annoying. Am I missing something?
thanks
=G=
Xymon at xymon.com <
Xymon at xymon.com <