om

om - the most recently modified file in a directory
git clone git://git.larryhynes.net/om
Log | Files | Refs | README | LICENSE

commit f0e0424d81fd0bfedb2ba6978031e156fbc74e41
parent 94086ae42b24df73d49db9a5215eb969bd201033
Author: Larry Hynes <larry@larryhynes.com>
Date:   Tue,  4 Jul 2017 12:26:49 +0100

Better check for empty res

- Use the counter, Luke

This gets rid of the hacky check at the end for an empty 'res', by
putting a counter in om(), incrementing on regular files (S_ISREG),
and exiting if it ends up at 0.

If om is run on a directory with no regular files, it exits cleanly.

Diffstat:
om.c | 14++++----------
1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/om.c b/om.c @@ -51,6 +51,7 @@ om() time_t mtime = 0; struct stat sb; size_t len; + int c = 0; dirp = opendir(dir); if (!dirp) @@ -70,6 +71,7 @@ om() stat(buf, &sb); if (S_ISREG(sb.st_mode)) { + c++; stat(buf, &sb); if (sb.st_mtime > mtime) { len = strlcpy(res, ent->d_name, sizeof(res)); @@ -82,16 +84,9 @@ om() closedir(dirp); -} - -/* If res is empty e.g. argument is a dir of dirs, exit */ -int -chck() -{ - size_t len = strlen(res); - if (len == 0) + if (c == 0) exit(0); - return 0; + } void @@ -118,7 +113,6 @@ main(int argc, char *argv[]) om(); - chck(); prnt(dir, res); return 0;