From e1cfd1c7d296cd691d2194187575f3302f66ef92 Mon Sep 17 00:00:00 2001 From: Tobias Wiese Date: Fri, 7 Aug 2020 17:40:14 +0200 Subject: reorganize directory structure. move the content of the src/ directory up into the root. There is no real need for putting those files inside an extra directory. Signed-off-by: Tobias Wiese --- .gitignore | 2 +- Makefile | 49 ++++++++++++++++++++++----- exchange.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Makefile | 39 --------------------- src/exchange.c | 105 --------------------------------------------------------- 5 files changed, 146 insertions(+), 154 deletions(-) create mode 100644 exchange.c delete mode 100644 src/Makefile delete mode 100644 src/exchange.c diff --git a/.gitignore b/.gitignore index 6783a1d..4a50cc5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.[oa] *~ -/src/exchange +/exchange diff --git a/Makefile b/Makefile index b82db39..341cd46 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,45 @@ -SHELL = /bin/sh -prefix = /usr/local +SHELL = /bin/sh +prefix = /usr/local -.PHONY: all clean -all clean: - @$(MAKE) -C src/ $@ +VERSION = $(shell git describe --dirty --always) +DATE_FMT = +%b %d %Y %H:%M:%S UTC + +ifdef SOURCE_DATE_EPOCH + BUILD_DATE ?= $(shell \ + date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null \ + || date -u -r "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null \ + || date -u "$(DATE_FMT)") +else + BUILD_DATE ?= $(shell date -u "$(DATE_FMT)") +endif + +INSTALL = install + +CFLAGS += -Wall -DVERSION="\"$(VERSION)\"" -DBUILD_DATE="\"$(BUILD_DATE)\"" +LDFLAGS += + +TARGETS := exchange +MAINS := $(addsuffix .o, $(TARGETS)) +OBJ := $(MAINS) + +.PHONY: all +all: $(TARGETS) + +.PHONY: clean +clean: + rm -f $(OBJ) $(TARGETS) .PHONY: distclean distclean: clean -.PHONY: install uninstall -install uninstall: - $(eval dd := $(shell test -z "$(DESTDIR)" || realpath -m "$(DESTDIR)")) - @$(MAKE) -C src/ $@ DESTDIR=$(dd) prefix=$(prefix) +.PHONY: install +install: $(TARGETS) + $(INSTALL) -Dt "$(DESTDIR)$(prefix)/bin" $(TARGETS) + +.PHONY: uninstall +uninstall: + cd $(DESTDIR)$(prefix)/bin && rm -f $(TARGETS) + + +$(TARGETS): % : %.o + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ diff --git a/exchange.c b/exchange.c new file mode 100644 index 0000000..c818606 --- /dev/null +++ b/exchange.c @@ -0,0 +1,105 @@ +#define _GNU_SOURCE +#include +#include +#include +#include + +#ifndef SMALL +#include +#include +#endif + +#ifndef DEFAULT_PROGRAM_NAME +# define DEFAULT_PROGRAM_NAME "exchange" +#endif + +#ifndef VERSION +# define VERSION "" +#endif + +#ifndef SMALL +static char *program_name = DEFAULT_PROGRAM_NAME; +static struct option const long_options[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'v' }, + { NULL, 0, NULL, 0 }, +}; + +void usage(FILE *fd) { + fprintf(fd, "\ +Usage: %s [OPTION]... \n\ +Exchange PATH 1 and PATH 2.\n", + program_name); + fputs("\n", fd); + fputs("\ +Available Options:\n\ + -h, --help display this help and exit\n\ + -v, --version output version information and exit\n", fd); +} + +void version() { + printf("%s %s\n", program_name, VERSION); +#ifdef BUILD_DATE + printf("Build on %s.\n", BUILD_DATE); +#endif +} +#endif // !SMALL + +int main(int argc, char *argv[]) { + char *path1, *path2; +#ifndef SMALL + if (argc > 0) + program_name = argv[0]; + + int c; + while ((c = getopt_long(argc, argv, "hvt", long_options, NULL)) + != -1) + { + switch (c) { + case 't': + break; + case 'h': // help + usage(stdout); + return EXIT_SUCCESS; + case 'v': // version + version(); + return EXIT_SUCCESS; + default: + usage(stderr); + return EXIT_FAILURE; + } + } + + if (argc - optind != 2) { + fprintf(stderr, "%s: invalid number of arguments. Expected 2.\n", program_name); + usage(stderr); + return EXIT_FAILURE; + } + +#else + // We currently don't accept any options + // when compiling with SMALL + // Define optind manually, because we don't use optparse. + const int optind = 1; +#endif // !SMALL + + if (argc - optind != 2) { +#ifndef SMALL + fprintf(stderr, "%s: invalid number of arguments. Expected 2.\n", program_name); + usage(stderr); +#endif + return EXIT_FAILURE; + } + path1 = argv[optind]; + path2 = argv[optind + 1]; + + // Do the magic + if (renameat2(AT_FDCWD, path1, AT_FDCWD, path2, RENAME_EXCHANGE) != 0) { + int errsv = errno; +#ifndef SMALL + perror(program_name); +#endif + return -errsv; + } + return EXIT_SUCCESS; +} diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index cfb66b7..0000000 --- a/src/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -VERSION = $(shell git describe --dirty --always) -DATE_FMT = +%b %d %Y %H:%M:%S UTC - -ifdef SOURCE_DATE_EPOCH - BUILD_DATE ?= $(shell \ - date -u -d "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null \ - || date -u -r "@$(SOURCE_DATE_EPOCH)" "$(DATE_FMT)" 2>/dev/null \ - || date -u "$(DATE_FMT)") -else - BUILD_DATE ?= $(shell date -u "$(DATE_FMT)") -endif - -INSTALL = install - -CFLAGS += -Wall -DVERSION="\"$(VERSION)\"" -DBUILD_DATE="\"$(BUILD_DATE)\"" -LDFLAGS += - -TARGETS := exchange -MAINS := $(addsuffix .o, $(TARGETS)) -OBJ := $(MAINS) - -.PHONY: all -all: $(TARGETS) - -.PHONY: clean -clean: - rm -f $(OBJ) $(TARGETS) - -.PHONY: install -install: $(TARGETS) - $(INSTALL) -Dt "$(DESTDIR)$(prefix)/bin" $(TARGETS) - -.PHONY: uninstall -uninstall: - cd $(DESTDIR)$(prefix)/bin && rm -f $(TARGETS) - - -$(TARGETS): % : %.o - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ diff --git a/src/exchange.c b/src/exchange.c deleted file mode 100644 index c818606..0000000 --- a/src/exchange.c +++ /dev/null @@ -1,105 +0,0 @@ -#define _GNU_SOURCE -#include -#include -#include -#include - -#ifndef SMALL -#include -#include -#endif - -#ifndef DEFAULT_PROGRAM_NAME -# define DEFAULT_PROGRAM_NAME "exchange" -#endif - -#ifndef VERSION -# define VERSION "" -#endif - -#ifndef SMALL -static char *program_name = DEFAULT_PROGRAM_NAME; -static struct option const long_options[] = { - { "help", no_argument, NULL, 'h' }, - { "version", no_argument, NULL, 'v' }, - { NULL, 0, NULL, 0 }, -}; - -void usage(FILE *fd) { - fprintf(fd, "\ -Usage: %s [OPTION]... \n\ -Exchange PATH 1 and PATH 2.\n", - program_name); - fputs("\n", fd); - fputs("\ -Available Options:\n\ - -h, --help display this help and exit\n\ - -v, --version output version information and exit\n", fd); -} - -void version() { - printf("%s %s\n", program_name, VERSION); -#ifdef BUILD_DATE - printf("Build on %s.\n", BUILD_DATE); -#endif -} -#endif // !SMALL - -int main(int argc, char *argv[]) { - char *path1, *path2; -#ifndef SMALL - if (argc > 0) - program_name = argv[0]; - - int c; - while ((c = getopt_long(argc, argv, "hvt", long_options, NULL)) - != -1) - { - switch (c) { - case 't': - break; - case 'h': // help - usage(stdout); - return EXIT_SUCCESS; - case 'v': // version - version(); - return EXIT_SUCCESS; - default: - usage(stderr); - return EXIT_FAILURE; - } - } - - if (argc - optind != 2) { - fprintf(stderr, "%s: invalid number of arguments. Expected 2.\n", program_name); - usage(stderr); - return EXIT_FAILURE; - } - -#else - // We currently don't accept any options - // when compiling with SMALL - // Define optind manually, because we don't use optparse. - const int optind = 1; -#endif // !SMALL - - if (argc - optind != 2) { -#ifndef SMALL - fprintf(stderr, "%s: invalid number of arguments. Expected 2.\n", program_name); - usage(stderr); -#endif - return EXIT_FAILURE; - } - path1 = argv[optind]; - path2 = argv[optind + 1]; - - // Do the magic - if (renameat2(AT_FDCWD, path1, AT_FDCWD, path2, RENAME_EXCHANGE) != 0) { - int errsv = errno; -#ifndef SMALL - perror(program_name); -#endif - return -errsv; - } - return EXIT_SUCCESS; -} -- cgit v1.2.3