commit 77eb45a9745e0e71da5fa8ce7e1b846b0672a709
parent 89ec7c4095c343a07e9561d3d71ba5b6f3202c35
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Tue, 22 Feb 2022 09:55:31 -0600
waycopy: replace copyfile with copyfd
Diffstat:
3 files changed, 9 insertions(+), 43 deletions(-)
diff --git a/util.c b/util.c
@@ -20,30 +20,6 @@ warn(const char *const error)
}
void
-copyfile(FILE *out, FILE *in)
-{
- char buf[BUFSIZ];
-
- size_t rcount, wcount;
- do {
- rcount = fread(buf, 1, BUFSIZ, in);
- wcount = fwrite(buf, 1, rcount, out);
- if (rcount < BUFSIZ) {
- if (ferror(in)) {
- // TODO: print actual error
- warn("fread failed");
- } else if (feof(in)) {
- break;
- }
- }
-
- if (wcount < rcount) {
- warn("fwrite failed");
- }
- } while (1);
-}
-
-void
copyfd(int out, int in)
{
char buf[BUFSIZ], *ptr;
diff --git a/util.h b/util.h
@@ -1,4 +1,3 @@
void die(const char *const error);
void warn(const char *const error);
-void copyfile(FILE *out, FILE *in);
void copyfd(int out, int in);
diff --git a/waycopy.c b/waycopy.c
@@ -1,4 +1,5 @@
#include <assert.h>
+#include <fcntl.h>
#include <limits.h>
#include <string.h>
#include <stdbool.h>
@@ -17,7 +18,7 @@ char mimetype[MIMETYPE_MAX_SIZE];
struct zwlr_data_control_manager_v1 *data_control_manager;
struct wl_seat *seat;
-FILE *temp;
+int temp;
bool running = 1;
@@ -44,15 +45,10 @@ static const struct wl_registry_listener registry_listener = {
void
data_source_send(void *data, struct zwlr_data_control_source_v1 *source, const char *mime_type, int32_t fd)
{
- fseek(temp, SEEK_SET, 0);
- FILE *out = fdopen(fd, "w");
- if (out == NULL) {
- warn("failed to open fd as FILE");
- return;
- }
+ lseek(temp, SEEK_SET, 0);
- copyfile(out, temp);
- fclose(out);
+ copyfd(fd, temp);
+ close(fd);
}
void
@@ -85,17 +81,12 @@ main(int argc, const char *argv[])
}
strncat(path, tempname, PATH_MAX - 1);
- int tempfd = mkstemp(path);
- if (tempfd == -1)
+ temp = mkstemp(path);
+ if (temp == -1)
die("failed to create temporary file for copy buffer");
- temp = fdopen(tempfd, "r+");
- if (temp == NULL) {
- die("failed to open temporary file as FILE");
- }
-
- copyfile(temp, stdin);
- fclose(stdin);
+ copyfd(temp, STDIN_FILENO);
+ close(STDIN_FILENO);
struct wl_display *const display = wl_display_connect(NULL);
if (display == NULL)