summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2014-02-19 11:11:49 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2014-02-19 11:11:49 +0100
commit7f8952630abc0db42aa29cc0f2df4481789433bc (patch)
tree2ca98bd589cd6c37d860eb8d483db595a93263ce
parentf8dc9ad4c41c7ec781c180d327f11ba78801263d (diff)
Add error handling and unlink of created tmp files.
-rw-r--r--main_complete.c13
-rw-r--r--main_simple.c13
2 files changed, 18 insertions, 8 deletions
diff --git a/main_complete.c b/main_complete.c
index b86e2a1..8eb4b7e 100644
--- a/main_complete.c
+++ b/main_complete.c
@@ -30,15 +30,17 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
-
+#include <errno.h>
#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
int main(int argc, char *argv[])
{
- printf("Running...\n");
-
- int fd = open("/tmp/my.log", O_CREAT | O_RDWR, 0777);
+ const char *mylog = "/tmp/my.log";
+ int fd = open(mylog, O_CREAT | O_RDWR, 0777);
if(fd == -1) {
+ printf("Could not open '%s' for writing: %s", mylog, strerror(errno));
return 1;
}
hug_status_t status = hug_init(HUG_FLAG_USE_MUTEX |
@@ -70,7 +72,8 @@ int main(int argc, char *argv[])
hug_close();
- printf("done\n");
+ unlink(mylog);
+ unlink("/tmp/my.log2");
return 0;
}
diff --git a/main_simple.c b/main_simple.c
index 60d57ac..84753c9 100644
--- a/main_simple.c
+++ b/main_simple.c
@@ -30,13 +30,17 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
-
+#include <unistd.h>
#include <stdio.h>
+#include <string.h>
+#include <errno.h>
int main(int argc, char *argv[])
{
- int fd = open("/tmp/my.log", O_CREAT | O_RDWR, 0777);
+ const char *mylog = "/tmp/my.log";
+ int fd = open(mylog, O_CREAT | O_RDWR, 0777);
if(fd == -1) {
+ printf("Could not open '%s' for writing: %s", mylog, strerror(errno));
return 1;
}
@@ -46,7 +50,7 @@ int main(int argc, char *argv[])
HUG_FLAG_OUTPUT_TO_STDERR |
0,
HUG_OPTION_FD, fd,
- HUG_OPTION_FILENAME, "/tmp/my2.log",
+ HUG_OPTION_FILENAME, "/tmp/my.log2",
HUG_OPTION_STDOUT_NO_DATE, 0,
HUG_OPTION_END);
@@ -65,5 +69,8 @@ int main(int argc, char *argv[])
hug_close();
+ unlink(mylog);
+ unlink("/tmp/my.log2");
+
return 0;
}