[PATCH v3 evemu] Only overwrite NULL names on evemu_read()

Peter Hutterer peter.hutterer at who-t.net
Tue Nov 19 16:41:54 PST 2013


If a caller has provided a device name to evemu_new(), parse the name but
don't copy it in to the device struct.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
Changes to v2:
- replace some dev->name access with evemu_get/set_name

 src/evemu.c              | 13 +++++++++++--
 test/test-evemu-create.c | 16 ++++++++++++----
 tools/evemu-device.c     | 11 +++++++----
 3 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/src/evemu.c b/src/evemu.c
index 838225c..d984718 100644
--- a/src/evemu.c
+++ b/src/evemu.c
@@ -421,8 +421,10 @@ static int parse_name(struct evemu_device *dev, const char *line, struct version
 	int matched;
 	char *devname = NULL;
 
-	if ((matched = sscanf(line, "N: %m[^\n]\n", &devname)) > 0)
-		evemu_set_name(dev, devname);
+	if ((matched = sscanf(line, "N: %m[^\n]\n", &devname)) > 0) {
+		if (strlen(evemu_get_name(dev)) == 0)
+			evemu_set_name(dev, devname);
+	}
 
 	if (devname != NULL)
 		free(devname);
@@ -558,9 +560,16 @@ int evemu_read(struct evemu_device *dev, FILE *fp)
 	struct version file_version; /* file format version */
 	size_t size = 0;
 	char *line = NULL;
+	char *name = NULL;
 
+	if (strlen(evemu_get_name(dev)) > 0)
+		name = strdup(evemu_get_name(dev));
 	memset(dev, 0, sizeof(*dev));
 	dev->version = EVEMU_VERSION;
+	if (name) {
+		evemu_set_name(dev, name);
+		free(name);
+	}
 
 	/* first line _may_ be version */
 	if (!first_line(fp, &line, &size)) {
diff --git a/test/test-evemu-create.c b/test/test-evemu-create.c
index 4437420..3df3194 100644
--- a/test/test-evemu-create.c
+++ b/test/test-evemu-create.c
@@ -14,6 +14,7 @@
 #define UNUSED __attribute__((unused))
 
 #define NAME "evemu test device"
+#define CUSTOM_NAME "custom evemu test device"
 
 static const char *name		= "N: " NAME "";
 static const char *ident	= "I: 0003 0004 0005 0006";
@@ -40,7 +41,8 @@ enum flags {
 	EOLCOMMENT	 = (1 << 6), /* end-of-line comment */
 	EVENT		 = (1 << 7), /* event line */
 	EMPTYLINE	 = (1 << 8),
-	ALLFLAGS	 = (EMPTYLINE << 1) - 1
+	WITHNAME	 = (1 << 9), /* use evemu_new(custom name) */
+	ALLFLAGS	 = (WITHNAME << 1) - 1
 };
 
 static int max[EV_CNT] = {
@@ -69,6 +71,7 @@ void check_evemu_read(int fd, const char *file, enum flags flags)
 {
 	FILE *fp;
 	struct evemu_device *dev;
+	const char *device_name;
 
 	ftruncate(fd, 0);
 	lseek(fd, 0, SEEK_SET);
@@ -146,11 +149,16 @@ void check_evemu_read(int fd, const char *file, enum flags flags)
 	fp = fopen(file, "r");
 	assert(fp);
 
-	dev = evemu_new("test device");
+	if (flags & WITHNAME) {
+		dev = evemu_new(CUSTOM_NAME);
+		device_name = CUSTOM_NAME;
+	} else {
+		dev = evemu_new(NULL);
+		device_name = NAME;
+	}
 	assert(dev);
-
 	assert(evemu_read(dev, fp) >= 0);
-	assert(strcmp(NAME, evemu_get_name(dev)) == 0);
+	assert(strcmp(device_name, evemu_get_name(dev)) == 0);
 	assert(evemu_get_id_bustype(dev) == 0x0003);
 	assert(evemu_get_id_vendor(dev) == 0x0004);
 	assert(evemu_get_id_product(dev) == 0x0005);
diff --git a/tools/evemu-device.c b/tools/evemu-device.c
index 20e0f44..5ddd166 100644
--- a/tools/evemu-device.c
+++ b/tools/evemu-device.c
@@ -201,19 +201,22 @@ static void hold_device(const struct evemu_device *dev)
 static int evemu_device(FILE *fp)
 {
 	struct evemu_device *dev;
-	char name[64];
 	int ret = -ENOMEM;
 	int fd;
 
-	sprintf(name, "evemu-%d", getpid());
-
-	dev = evemu_new(name);
+	dev = evemu_new(NULL);
 	if (!dev)
 		goto out;
 	ret = evemu_read(dev, fp);
 	if (ret <= 0)
 		goto out;
 
+	if (strlen(evemu_get_name(dev)) == 0) {
+		char name[64];
+		sprintf(name, "evemu-%d", getpid());
+		evemu_set_name(dev, name);
+	}
+
 	ret = fd = open(UINPUT_NODE, O_WRONLY);
 	if (ret < 0)
 		goto out;
-- 
1.8.3.1



More information about the Input-tools mailing list