summaryrefslogtreecommitdiff
path: root/direct-append.c
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2008-07-11 00:04:58 +0200
committerTobias Klauser <tklauser@xenon.tklauser.home>2008-07-11 00:04:58 +0200
commit59d34ee47b14c728ebd6399fb89c409383a1aa2e (patch)
tree356a31ecaacf3f1845e8ebda7c1765ede88ad284 /direct-append.c
Initial import
Diffstat (limited to 'direct-append.c')
-rw-r--r--direct-append.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/direct-append.c b/direct-append.c
new file mode 100644
index 0000000..bf18c72
--- /dev/null
+++ b/direct-append.c
@@ -0,0 +1,71 @@
+/*
+ * Append some random characters to a file
+ *
+ * Just a small tool to debug inotify
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define DEFAULT_N_CHARS 1024
+
+static char get_random_printable_char(void)
+{
+ int fd;
+ char ret;
+
+ fd = open("/dev/urandom", O_RDONLY);
+ if (fd < 0) {
+ perror("open");
+ exit(EXIT_FAILURE);
+ }
+
+ do {
+ read(fd, &ret, sizeof(ret));
+ } while (ret < 0x20 || ret > 0x7e);
+
+ close(fd);
+
+ return ret;
+}
+
+int main(int argc, char *argv[])
+{
+ int fd, n_chars, i;
+ char *str;
+
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s <file> <n_chars>\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ /* Open the file, create it in case it doesn't exist */
+ if ((fd = open(argv[1], O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
+ perror("open");
+ exit(EXIT_FAILURE);
+ }
+
+ if (lseek(fd, 0, SEEK_END) < 0) {
+ perror("lseek");
+ exit(EXIT_FAILURE);
+ }
+
+ n_chars = strtoul(argv[2], NULL, 0);
+ if (n_chars <= 0)
+ n_chars = DEFAULT_N_CHARS;
+
+ str = malloc(n_chars * sizeof(char));
+
+ for (i = 0; i < n_chars; i++)
+ str[i] = get_random_printable_char();
+
+ write(fd, str, n_chars);
+ free(str);
+ close(fd);
+
+ exit(EXIT_SUCCESS);
+}