git » libjio » commit f949b1d

Cleanup the samples

author Alberto Bertogli
2009-03-28 04:34:11 UTC
committer Alberto Bertogli
2009-03-28 04:38:27 UTC
parent c7fbc9989650aa923468cdbbed2ce12589cfdec9

Cleanup the samples

This patch does several cleanups to the samples:

 - Replace the ugly build and clean scripts with a Makefile.
 - Several style fixes.
 - Do not open the files with O_SYNC.
 - Make them write to different files.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

samples/Makefile +22 -0
samples/build +0 -6
samples/clean +0 -2
samples/jio1.c +16 -17
samples/jio2.c +24 -28
samples/jio3.c +6 -10

diff --git a/samples/Makefile b/samples/Makefile
new file mode 100644
index 0000000..fd3d1f4
--- /dev/null
+++ b/samples/Makefile
@@ -0,0 +1,22 @@
+
+CFLAGS := -Wall -O3 -D_XOPEN_SOURCE=500 \
+	$(shell getconf LFS_CFLAGS 2>/dev/null)
+LIBS = -ljio
+
+BINS = full jio1 jio2 jio3
+
+default: all
+
+all: $(BINS)
+
+$(BINS):
+	$(CC) $(CFLAGS) $(LIBS) $@.c -o $@
+
+clean:
+	rm -f $(BINS)
+	rm -f *.bb *.bbg *.da *.gcov gmon.out
+	rm -f test1 test2 test3
+	rm -rf .test1.jio .test2.jio .test3.jio
+
+.PHONY: default all clean
+
diff --git a/samples/build b/samples/build
deleted file mode 100755
index 39b68dc..0000000
--- a/samples/build
+++ /dev/null
@@ -1,6 +0,0 @@
-
-B="gcc -Wall -O3 -ljio `getconf LFS_CFLAGS` -D_FILE_OFFSET_BITS=64"
-for i in jio1 jio2 jio3 full; do
-	echo "CC  " $i
-	$B $i.c -o $i
-done
diff --git a/samples/clean b/samples/clean
deleted file mode 100755
index b5715c8..0000000
--- a/samples/clean
+++ /dev/null
@@ -1,2 +0,0 @@
-rm -rf jio1 jio2 jio3 test1 .test1.jio
-
diff --git a/samples/jio1.c b/samples/jio1.c
index 4296ac6..9fb4062 100644
--- a/samples/jio1.c
+++ b/samples/jio1.c
@@ -1,5 +1,4 @@
 
-
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -9,43 +8,42 @@
 
 #include <libjio.h>
 
-#define str "TESTTESTTEST1234\n"
+#define STR "TESTTESTTEST1234\n"
 
-int jio(void)
+static int jio(void)
 {
 	int fd, rv;
 	struct jfs fs;
 
-	fd = jopen(&fs, "test1", O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0660, 0);
+	fd = jopen(&fs, "test1", O_RDWR | O_CREAT | O_TRUNC, 0660, 0);
 	if (fd < 0)
-		perror("OPEN");
+		perror("jopen()");
 
-	rv = jwrite(&fs, str, strlen(str));
-	if (rv != strlen(str))
-		perror("WRITE");
+	rv = jwrite(&fs, STR, strlen(STR));
+	if (rv != strlen(STR))
+		perror("jwrite()");
 
 	return 0;
-
 }
 
-int classic(void)
+static int classic(void)
 {
 	int fd, rv;
 
-	fd = open("test1", O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0660);
+	fd = open("test1", O_RDWR | O_CREAT | O_TRUNC, 0660);
 	if (fd < 0)
-		perror("OPEN");
+		perror("open()");
 
-	rv = write(fd, str, strlen(str));
-	if (rv != strlen(str))
-		perror("WRITE");
+	rv = write(fd, STR, strlen(STR));
+	if (rv != strlen(STR))
+		perror("write()");
 
 	return 0;
-
 }
 
 
-int main(int argc, char **argv) {
+int main(int argc, char **argv)
+{
 	if (argc != 2) {
 		printf("Use: jio1 [c|j]\n");
 		return 1;
@@ -60,3 +58,4 @@ int main(int argc, char **argv) {
 
 	return 0;
 }
+
diff --git a/samples/jio2.c b/samples/jio2.c
index 9d6d008..3296dab 100644
--- a/samples/jio2.c
+++ b/samples/jio2.c
@@ -1,5 +1,4 @@
 
-
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -11,62 +10,59 @@
 #include <libjio.h>
 
 
-#define str "TESTTESTTEST1234\n"
+#define STR "TESTTESTTEST1234\n"
 
-int jio(void)
+static int jio(void)
 {
 	int fd, rv;
 	struct jfs fs;
 
-	fd = jopen(&fs, "test1", O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0660, 0);
+	fd = jopen(&fs, "test2", O_RDWR | O_CREAT | O_TRUNC, 0660, 0);
 	if (fd < 0)
-		perror("OPEN");
+		perror("jopen()");
 
-	rv = jwrite(&fs, str, strlen(str));
-	if (rv != strlen(str))
-		perror("WRITE");
+	rv = jwrite(&fs, STR, strlen(STR));
+	if (rv != strlen(STR))
+		perror("jwrite()");
 
 	return 0;
-
 }
 
-int classic(void)
+static int classic(void)
 {
 	int fd, rv;
 
-	fd = open("test1", O_RDWR | O_CREAT | O_TRUNC | O_SYNC, 0660);
+	fd = open("test2", O_RDWR | O_CREAT | O_TRUNC, 0660);
 	if (fd < 0)
-		perror("OPEN");
+		perror("open()");
 
-	rv = write(fd, str, strlen(str));
-	if (rv != strlen(str))
-		perror("WRITE");
+	rv = write(fd, STR, strlen(STR));
+	if (rv != strlen(STR))
+		perror("write()");
 
 	return 0;
-
 }
 
 
-int main(int argc, char **argv) {
-	int i;
-	int N;
-	
-	if (argc != 2) {
-		printf("Use: jio1 [c|j] N\n");
+int main(int argc, char **argv)
+{
+	int i, n;
+
+	if (argc != 3) {
+		printf("Use: jio2 [c|j] N\n");
 		return 1;
 	}
-	
-	N = 0;
-	N = atoi(argv[2]);
+
+	n = atoi(argv[2]);
 
 	if (*argv[1] == 'c')
-		for (i = 0; i < N; i++)
+		for (i = 0; i < n; i++)
 			classic();
 	else if (*argv[1] == 'j')
-		for (i = 0; i < N; i++)
+		for (i = 0; i < n; i++)
 			jio();
 	else
-		printf("Use: jio1 [c|j] N\n");
+		printf("Use: jio2 [c|j] n\n");
 
 	return 0;
 }
diff --git a/samples/jio3.c b/samples/jio3.c
index b5a9f5d..d66ee8d 100644
--- a/samples/jio3.c
+++ b/samples/jio3.c
@@ -1,5 +1,4 @@
 
-
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
@@ -16,9 +15,9 @@ int main(int argc, char **argv)
 	struct jfs fs;
 	struct jtrans ts;
 
-	fd = jopen(&fs, "test1", O_RDWR | O_CREAT | O_SYNC, 0660, 0);
+	fd = jopen(&fs, "test3", O_RDWR | O_CREAT, 0660, 0);
 	if (fd < 0)
-		perror("OPEN");
+		perror("jopen()");
 
 	jtrans_init(&fs, &ts);
 
@@ -31,19 +30,16 @@ int main(int argc, char **argv)
 #define str3 "3ROLLBACKTEST3!\n"
 	jtrans_add(&ts, str3, strlen(str3), strlen(str1) + strlen(str2));
 
-
 	rv = jtrans_commit(&ts);
 	if (rv != strlen(str1) + strlen(str2) + strlen(str3))
-		perror("COMMIT");
-	printf("COMMIT OK: %d\n", rv);
-
+		perror("jtrans_commit()");
+	printf("commit ok: %d\n", rv);
 
 	rv = jtrans_rollback(&ts);
 	if (rv < 0)
-		perror("ROLLBACK");
-	printf("ROLLBACK OK: %d\n", rv);
+		perror("jtrans_rollback()");
+	printf("rollback ok: %d\n", rv);
 
 	return 0;
-
 }