diff options
author | 2006-02-20 08:38:18 +0000 | |
---|---|---|
committer | 2006-02-20 08:38:18 +0000 | |
commit | 1d3b72fd31ae08801caf7b6801cfa0527500da50 (patch) | |
tree | b78a9daaebdf817586e6d14c6d49ee8d2a7dfef6 | |
parent | refactor processq() loop. From Rai Lay; ok jaredy@ (diff) | |
download | wireguard-openbsd-1d3b72fd31ae08801caf7b6801cfa0527500da50.tar.xz wireguard-openbsd-1d3b72fd31ae08801caf7b6801cfa0527500da50.zip |
Move two functions to separate file; from Ray Lai; ok jaredy@
-rw-r--r-- | usr.bin/sdiff/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/sdiff/common.c | 71 | ||||
-rw-r--r-- | usr.bin/sdiff/common.h | 9 | ||||
-rw-r--r-- | usr.bin/sdiff/edit.c | 63 |
4 files changed, 84 insertions, 63 deletions
diff --git a/usr.bin/sdiff/Makefile b/usr.bin/sdiff/Makefile index b438ea06e44..e626f3667d1 100644 --- a/usr.bin/sdiff/Makefile +++ b/usr.bin/sdiff/Makefile @@ -1,7 +1,7 @@ -# $OpenBSD: Makefile,v 1.3 2006/02/15 06:58:06 otto Exp $ +# $OpenBSD: Makefile,v 1.4 2006/02/20 08:38:18 otto Exp $ PROG=sdiff -SRCS=edit.c sdiff.c +SRCS=common.c edit.c sdiff.c COPTS+=-Wall -W LDADD+= -lutil diff --git a/usr.bin/sdiff/common.c b/usr.bin/sdiff/common.c new file mode 100644 index 00000000000..5bb380291fa --- /dev/null +++ b/usr.bin/sdiff/common.c @@ -0,0 +1,71 @@ +/* $OpenBSD: common.c,v 1.1 2006/02/20 08:38:18 otto Exp $ */ + +/* + * Written by Raymond Lai <ray@cyth.net>. + * Public domain. + */ + +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "common.h" + +void +cleanup(const char *filename) +{ + if (unlink(filename)) + err(2, "could not delete: %s", filename); + exit(2); +} + +/* + * Creates and returns the name of a temporary file. Takes a string + * (or NULL) is written to the temporary file. The returned string + * needs to be freed. + */ +char * +xmktemp(const char *s) +{ + FILE *file; + int fd; + const char *tmpdir; + char *filename; + + /* If TMPDIR is set, use it; otherwise use /tmp. */ + if (!(tmpdir = getenv("TMPDIR"))) + tmpdir = "/tmp"; + if (asprintf(&filename, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1) + err(2, "xmktemp"); + + /* Create temp file. */ + if ((fd = mkstemp(filename)) == -1) + err(2, "could not create temporary file"); + + /* If we don't write anything to the file, just close. */ + if (s == NULL) { + close(fd); + + return (filename); + } + + /* Open temp file for writing. */ + if ((file = fdopen(fd, "w")) == NULL) { + warn("could not open %s", filename); + cleanup(filename); + /* NOTREACHED */ + } + + /* Write to file. */ + if (fputs(s, file)) { + warn("could not write to %s", filename); + cleanup(filename); + /* NOTREACHED */ + } + + /* Close temp file. */ + fclose(file); + + return (filename); +} diff --git a/usr.bin/sdiff/common.h b/usr.bin/sdiff/common.h new file mode 100644 index 00000000000..c75aef2700b --- /dev/null +++ b/usr.bin/sdiff/common.h @@ -0,0 +1,9 @@ +/* $OpenBSD: common.h,v 1.1 2006/02/20 08:38:18 otto Exp $ */ + +/* + * Written by Raymond Lai <ray@cyth.net>. + * Public domain. + */ + +__dead void cleanup(const char *); +char *xmktemp(const char *); diff --git a/usr.bin/sdiff/edit.c b/usr.bin/sdiff/edit.c index d876b25ca03..d282e7fcfbf 100644 --- a/usr.bin/sdiff/edit.c +++ b/usr.bin/sdiff/edit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: edit.c,v 1.11 2006/02/15 06:58:06 otto Exp $ */ +/* $OpenBSD: edit.c,v 1.12 2006/02/20 08:38:18 otto Exp $ */ /* * Written by Raymond Lai <ray@cyth.net>. @@ -14,19 +14,10 @@ #include <stdlib.h> #include <unistd.h> +#include "common.h" #include "extern.h" -__dead static void cleanup(const char *); static void edit(const char *); -static char *xmktemp(const char *); - -static void -cleanup(const char *filename) -{ - if (unlink(filename)) - err(2, "could not delete: %s", filename); - exit(2); -} /* * Takes the name of a file and opens it with an editor. @@ -75,56 +66,6 @@ edit(const char *filename) } /* - * Creates and returns the name of a temporary file. Takes a string - * (or NULL) is written to the temporary file. The returned string - * needs to be freed. - */ -static char * -xmktemp(const char *s) -{ - FILE *file; - int fd; - const char *tmpdir; - char *filename; - - /* If TMPDIR is set, use it; otherwise use /tmp. */ - if (!(tmpdir = getenv("TMPDIR"))) - tmpdir = "/tmp"; - if (asprintf(&filename, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1) - err(2, "xmktemp"); - - /* Create temp file. */ - if ((fd = mkstemp(filename)) == -1) - err(2, "could not create temporary file"); - - /* If we don't write anything to the file, just close. */ - if (s == NULL) { - close(fd); - - return (filename); - } - - /* Open temp file for writing. */ - if ((file = fdopen(fd, "w")) == NULL) { - warn("could not open %s", filename); - cleanup(filename); - /* NOTREACHED */ - } - - /* Write to file. */ - if (fputs(s, file)) { - warn("could not write to %s", filename); - cleanup(filename); - /* NOTREACHED */ - } - - /* Close temp file. */ - fclose(file); - - return (filename); -} - -/* * Parse edit command. Returns 0 on success, -1 on error. */ int |