missed this..

>From 34fce3297178a5ee2e68dde28c61cb07862cb811 Mon Sep 17 00:00:00 2001
From: Tamas TEVESZ <[email protected]>
Date: Wed, 17 Mar 2010 23:59:25 +0100
Subject: [PATCH] Some more getstyle

- missed a shell invocation
- maybe copyFile should be in wutils too...?
---
 util/getstyle.c |   52 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/util/getstyle.c b/util/getstyle.c
index 452183d..271ce22 100644
--- a/util/getstyle.c
+++ b/util/getstyle.c
@@ -22,6 +22,10 @@
 
 #define PROG_VERSION "getstyle (Window Maker) 0.6"
 
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <fcntl.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -30,6 +34,7 @@
 #include <pwd.h>
 #include <limits.h>
 #include <assert.h>
+#include <libgen.h>
 #include <WINGs/WUtil.h>
 
 #ifndef PATH_MAX
@@ -171,10 +176,7 @@ char *defaultsPathForDomain(char *domain)
 
 void abortar(char *reason)
 {
-       char buffer[4000];
-
        printf("%s: %s\n", ProgName, reason);
-
        if (ThemePath) {
                printf("Removing unfinished theme pack\n");
                (void)wrmdirhier(ThemePath);
@@ -195,14 +197,48 @@ static Bool isFontOption(char *option)
        return False;
 }
 
+/*
+ * it is more or less assumed that this function will only
+ * copy reasonably-sized files
+ */
 void copyFile(char *dir, char *file)
 {
-       char buffer[4000];
+       int from_fd, to_fd;
+       size_t block, len;
+       char buf[4096];
+       struct stat st;
+       char *dst;
+
+       /* only to a directory */
+       if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode))
+               return;
+       /* only copy files */
+       if (stat(file, &st) != 0 || !S_ISREG(st.st_mode))
+               return;
 
-       sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
-       if (system(buffer) != 0) {
-               printf("%s: could not copy file %s\n", ProgName, file);
+       len = strlen(dir) + 1 /* / */ + strlen(file) + 1 /* '\0' */;
+       dst = wmalloc(len);
+       snprintf(dst, len, "%s/%s", dir, basename(file));
+       buf[len] = '\0';
+
+       if ((to_fd = open(dst, O_RDWR|O_CREAT|O_TRUNC)) == -1) {
+               wfree(dst);
+               return;
+       }
+       wfree(dst);
+       if ((from_fd = open(file, O_RDONLY)) == -1) {
+               (void)close(to_fd);
+               return;
        }
+
+       /* XXX: signal handling */
+       while ((block = read(from_fd, &buf, sizeof(buf))) > 0)
+               write(to_fd, &buf, block);
+
+       (void)fsync(to_fd);
+       (void)fchmod(to_fd, st.st_mode);
+       (void)close(to_fd);
+       (void)close(from_fd);
 }
 
 void findCopyFile(char *dir, char *file)
-- 
1.7.0


-- 
[-]

mkdir /nonexistent
From 34fce3297178a5ee2e68dde28c61cb07862cb811 Mon Sep 17 00:00:00 2001
From: Tamas TEVESZ <[email protected]>
Date: Wed, 17 Mar 2010 23:59:25 +0100
Subject: [PATCH] Some more getstyle

- missed a shell invocation
- maybe copyFile should be in wutils too...?
---
 util/getstyle.c |   52 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/util/getstyle.c b/util/getstyle.c
index 452183d..271ce22 100644
--- a/util/getstyle.c
+++ b/util/getstyle.c
@@ -22,6 +22,10 @@
 
 #define PROG_VERSION "getstyle (Window Maker) 0.6"
 
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include <fcntl.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -30,6 +34,7 @@
 #include <pwd.h>
 #include <limits.h>
 #include <assert.h>
+#include <libgen.h>
 #include <WINGs/WUtil.h>
 
 #ifndef PATH_MAX
@@ -171,10 +176,7 @@ char *defaultsPathForDomain(char *domain)
 
 void abortar(char *reason)
 {
-	char buffer[4000];
-
 	printf("%s: %s\n", ProgName, reason);
-
 	if (ThemePath) {
 		printf("Removing unfinished theme pack\n");
 		(void)wrmdirhier(ThemePath);
@@ -195,14 +197,48 @@ static Bool isFontOption(char *option)
 	return False;
 }
 
+/*
+ * it is more or less assumed that this function will only
+ * copy reasonably-sized files
+ */
 void copyFile(char *dir, char *file)
 {
-	char buffer[4000];
+	int from_fd, to_fd;
+	size_t block, len;
+	char buf[4096];
+	struct stat st;
+	char *dst;
+
+	/* only to a directory */
+	if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode))
+		return;
+	/* only copy files */
+	if (stat(file, &st) != 0 || !S_ISREG(st.st_mode))
+		return;
 
-	sprintf(buffer, "/bin/cp \"%s\" \"%s\"", file, dir);
-	if (system(buffer) != 0) {
-		printf("%s: could not copy file %s\n", ProgName, file);
+	len = strlen(dir) + 1 /* / */ + strlen(file) + 1 /* '\0' */;
+	dst = wmalloc(len);
+	snprintf(dst, len, "%s/%s", dir, basename(file));
+	buf[len] = '\0';
+
+	if ((to_fd = open(dst, O_RDWR|O_CREAT|O_TRUNC)) == -1) {
+		wfree(dst);
+		return;
+	}
+	wfree(dst);
+	if ((from_fd = open(file, O_RDONLY)) == -1) {
+		(void)close(to_fd);
+		return;
 	}
+
+	/* XXX: signal handling */
+	while ((block = read(from_fd, &buf, sizeof(buf))) > 0)
+		write(to_fd, &buf, block);
+
+	(void)fsync(to_fd);
+	(void)fchmod(to_fd, st.st_mode);
+	(void)close(to_fd);
+	(void)close(from_fd);
 }
 
 void findCopyFile(char *dir, char *file)
-- 
1.7.0

Reply via email to