om

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

commit 94086ae42b24df73d49db9a5215eb969bd201033
parent 3bde9def2a35e8776571bdff75c468bb2e919578
Author: Larry Hynes <larry@larryhynes.com>
Date:   Mon,  3 Jul 2017 21:20:31 +0100

Refactor all the things, and embrace strl(cat|cpy)

- Do Things Right
- Add LICENSE
- Update README
- Tidy up

Diffstat:
LICENSE | 13+++++++++++++
Makefile | 2+-
README | 21++++++---------------
compat.h | 4++++
om.c | 32+++++++++++++++++++++++++++-----
strlcat.c | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
strlcpy.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 160 insertions(+), 21 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,13 @@ +Copyright (c) 2017 Larry Hynes <my first name at larryhynes.com> + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/Makefile b/Makefile @@ -9,7 +9,7 @@ om: $(OBJS) $(CC) $(CFLAGS) -o om $(OBJS) clean: - rm -f om.o om + rm -f *.o om install: om install -m 0755 om $(BINDIR) diff --git a/README b/README @@ -37,8 +37,8 @@ EXAMPLES mpv `om dl` -BUGS -==== +CAVEATS +======= Obviously this is a little bit brain-damaged insofar as one may not know with absolute certainty which file may be the most recently @@ -53,20 +53,11 @@ I have no idea what I'm doing. LICENSE ======= +See LICENSE. + There is not enough art here to justify rights, but I include a license to remove any ambiguity and to make it clear that there is no warranty. -Copyright (c) 2017 Larry Hynes <my first name at larryhynes.com> - -Permission to use, copy, modify, and distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +strlcat.c and strlcpy.c are copyright Todd C. Miller - see those +files for details. diff --git a/compat.h b/compat.h @@ -0,0 +1,4 @@ +#undef strlcat +size_t strlcat(char *, const char *, size_t); +#undef strlcpy +size_t strlcpy(char *, const char *, size_t); diff --git a/om.c b/om.c @@ -7,9 +7,12 @@ #include <stdlib.h> #include <string.h> +#include "strlcat.c" +#include "strlcpy.c" + char *dir; -char buf[PATH_MAX +1]; -char res[PATH_MAX]; +char buf[PATH_MAX]; +char res[NAME_MAX]; /* Test if argument can be stat-ed, and is a directory */ int @@ -35,24 +38,43 @@ char } void +serr() +{ + errx(1, "Path name too long"); +} + +void om() { DIR *dirp; struct dirent *ent; time_t mtime = 0; struct stat sb; + size_t len; dirp = opendir(dir); if (!dirp) return; while ((ent = readdir(dirp)) != NULL) { - snprintf(buf, sizeof(buf), "%s/%s", dir, ent->d_name); + len = strlcpy(buf, dir, sizeof(buf)); + if (len >= sizeof(buf)) + serr(); + strlcat(buf, "/", sizeof(buf)); + if (len >= sizeof(buf)) + serr(); + strlcat(buf, ent->d_name, sizeof(buf)); + if (len >= sizeof(buf)) + serr(); + stat(buf, &sb); + if (S_ISREG(sb.st_mode)) { stat(buf, &sb); if (sb.st_mtime > mtime) { - snprintf(res, sizeof(res), "%s", ent->d_name ); + len = strlcpy(res, ent->d_name, sizeof(res)); + if (len >= sizeof(res)) + errx(1, "Filename too long"); mtime = sb.st_mtime; } } @@ -66,7 +88,7 @@ om() int chck() { - size_t len=strlen(res); + size_t len = strlen(res); if (len == 0) exit(0); return 0; diff --git a/strlcat.c b/strlcat.c @@ -0,0 +1,57 @@ +/* $OpenBSD: strlcat.c,v 1.15 2015/03/02 21:41:08 millert Exp $ */ + +/* + * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <string.h> + +#include "compat.h" + +/* + * Appends src to string dst of size dsize (unlike strncat, dsize is the + * full size of dst, not space left). At most dsize-1 characters + * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). + * Returns strlen(src) + MIN(dsize, strlen(initial dst)). + * If retval >= dsize, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t dsize) +{ + const char *odst = dst; + const char *osrc = src; + size_t n = dsize; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end. */ + while (n-- != 0 && *dst != '\0') + dst++; + dlen = dst - odst; + n = dsize - dlen; + + if (n-- == 0) + return(dlen + strlen(src)); + while (*src != '\0') { + if (n != 0) { + *dst++ = *src; + n--; + } + src++; + } + *dst = '\0'; + + return(dlen + (src - osrc)); /* count does not include NUL */ +} diff --git a/strlcpy.c b/strlcpy.c @@ -0,0 +1,52 @@ +/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */ + +/* + * Copyright (c) 1998, 2015 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <string.h> + +#include "compat.h" + +/* + * Copy string src to buffer dst of size dsize. At most dsize-1 + * chars will be copied. Always NUL terminates (unless dsize == 0). + * Returns strlen(src); if retval >= dsize, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; /* NUL-terminate dst */ + while (*src++) + ; + } + + return(src - osrc - 1); /* count does not include NUL */ +}