diff options
Diffstat (limited to 'utils/touchy')
-rw-r--r-- | utils/touchy/Makefile | 20 | ||||
-rw-r--r-- | utils/touchy/touchy.c | 63 |
2 files changed, 83 insertions, 0 deletions
diff --git a/utils/touchy/Makefile b/utils/touchy/Makefile new file mode 100644 index 0000000000..d2430df162 --- /dev/null +++ b/utils/touchy/Makefile @@ -0,0 +1,20 @@ +# +# Substitute for 'touch' on win32 platforms (without an Unix toolset installed). +# +TOP=../.. +include $(TOP)/mk/boilerplate.mk + +C_SRCS=touchy.c +C_PROG=touchy +SRC_CC_OPTS += -O + +# +# Install touchy in lib/.* +# +INSTALL_LIBEXECS += $(C_PROG) + +include $(TOP)/mk/target.mk + +# Get it over with! +boot :: all + diff --git a/utils/touchy/touchy.c b/utils/touchy/touchy.c new file mode 100644 index 0000000000..90fb31e93e --- /dev/null +++ b/utils/touchy/touchy.c @@ -0,0 +1,63 @@ +/* + * Simple _utime() wrapper for setting the mod. time on files + * to the current system time. + * + */ +#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32) +#error "Win32-only, the platform you're using is supposed to have 'touch' already." +#else +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> +#include <errno.h> + +int +main(int argc, char** argv) +{ + int rc; + int i=0; + int fd; + int wBitSet = 0; + struct _stat sb; + + if (argc == 1) { + fprintf(stderr, "Usage: %s <files>\n", argv[0]); + return 1; + } + + + while (i++ < (argc-1)) { + if ( (_access(argv[i], 00) < 0) && (errno == ENOENT || errno == EACCES) ) { + /* File doesn't exist, try creating it. */ + if ( (fd = _open(argv[i], _O_CREAT | _O_EXCL | _O_TRUNC, _S_IREAD | _S_IWRITE)) < 0 ) { + fprintf(stderr, "Unable to create %s, skipping.\n", argv[i]); + } else { + _close(fd); + } + } + if ( (_access(argv[i], 02)) < 0 ) { + /* No write permission, try setting it first. */ + if (_stat(argv[i], &sb) < 0) { + fprintf(stderr, "Unable to change mod. time for %s (%d)\n", argv[i], errno); + continue; + } + if (_chmod(argv[i], (sb.st_mode & _S_IREAD) | _S_IWRITE) < 0) { + fprintf(stderr, "Unable to change mod. time for %s (%d)\n", argv[i], errno); + continue; + } + wBitSet = 1; + } + if ( (rc = _utime(argv[i],NULL)) < 0) { + fprintf(stderr, "Unable to change mod. time for %s (%d)\n", argv[i], errno); + } + if (wBitSet) { + /* Turn the file back into a read-only file */ + _chmod(argv[i], (sb.st_mode & _S_IREAD)); + wBitSet = 0; + } + } + + return 0; +} +#endif |