Short Description
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 56 966ad681f25d
parent 55 2a20d0184041
child 57 7739518acafb
Short Description * duff.c Duffs Device, a fast copy algorithm * crypt.c the Salt was to short. * files.c massive directory creation * fork.c a small fork demo committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
crypt.c
duff.c
files.c
fork.c
--- a/Makefile
+++ b/Makefile
@@ -49,7 +49,10 @@
 	getpwnam_error \
 	xmlparser \
 	vector \
-	base10
+	base10 \
+	files \
+	fork \
+	duff
 
 .SUFFIXES: .c .cc .asm
 
@@ -265,6 +268,18 @@
 	@echo Linking $< ...
 	@$(CC) -lm -o $@ $<
 
+files: files.o
+	@echo Linking $< ...
+	@$(CC) -o $@ $<
+
+fork: fork.o
+	@echo Linking $< ...
+	@$(CC) -o $@ $<
+
+duff: duff.o
+	@echo Linking $< ...
+	@$(CC) -o $@ $<
+
 .PHONY: clean uninstall
 
 clean:
--- a/crypt.c
+++ b/crypt.c
@@ -12,7 +12,7 @@
 
 int main (int argc, char **argv)
 {
-    char salt[20] = { 0 };
+    char salt[12] = { 0 };
 
     if (argc != 2) {
         printf ("Usage: %s <plaintext>\n", argv[0]);
@@ -21,7 +21,9 @@
 
     srand (time (NULL));
 
-    sprintf (salt, "$1$%c%c", GETRANDOM (26), GETRANDOM (26));
+    sprintf (salt, "$1$%c%c%c%c%c%c%c%c",
+             GETRANDOM (26), GETRANDOM (26),
+             GETRANDOM (26), GETRANDOM (26), GETRANDOM (26), GETRANDOM (26), GETRANDOM (26), GETRANDOM (26));
 
     printf ("%s\n", crypt (argv[1], salt));
     return EXIT_SUCCESS;
new file mode 100644
--- /dev/null
+++ b/duff.c
@@ -0,0 +1,62 @@
+/**
+ * Duff's Device
+ * fast copy algorithm
+ * performs 8 times faster on long strings.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *duffcopy (char *to, char *from, int num)
+{
+    int n, pos = 0;
+
+    n = (num + 7) / 8;
+
+    switch (num % 8) {
+    case 0:
+        do {
+            to[pos] = from[pos];
+            pos++;
+    case 7:
+            to[pos] = from[pos];
+            pos++;
+    case 6:
+            to[pos] = from[pos];
+            pos++;
+    case 5:
+            to[pos] = from[pos];
+            pos++;
+    case 4:
+            to[pos] = from[pos];
+            pos++;
+    case 3:
+            to[pos] = from[pos];
+            pos++;
+    case 2:
+            to[pos] = from[pos];
+            pos++;
+    case 1:
+            to[pos] = from[pos];
+            pos++;
+        } while (--n > 0);
+    }
+
+    return to;
+}
+
+int main (int argc, char **argv)
+{
+    char to[80], *from;
+
+    if (argc != 2) {
+        printf ("Usage: %s <string>\n", argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    from = argv[1];
+
+    printf ("DUFF: %s\n", duffcopy (to, from, strlen (from)));
+    return EXIT_SUCCESS;
+}
new file mode 100644
--- /dev/null
+++ b/files.c
@@ -0,0 +1,64 @@
+/**
+ * test/demos/files.c
+ * Copyright (C) Markus Broeker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <time.h>
+
+#define  MAXDIRS 1000
+#define MAXFILES 1000
+
+#define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0))
+
+char *GetRandomNumberString ()
+{
+    static char buffer[12];
+
+    sprintf (buffer, "%d", GETRANDOM (65535));
+
+    return buffer;
+}
+
+int main (int argc, char **argv)
+{
+    int i, j;
+    int fd;
+    char *s;
+
+    if (argc != 2) {
+        printf ("Usage: %s <basedir>\n", argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    if (chdir (argv[1]) == -1) {
+        printf ("Initial Chdir to %s failed, aborting...\n", argv[1]);
+        return EXIT_FAILURE;
+    }
+
+    srand (time (NULL));
+
+    for (i = 0; i < MAXDIRS; i++) {
+        s = GetRandomNumberString ();
+        mkdir (s, 0777);
+        if (chdir (s) == -1) {
+            printf ("Chdir to %s failed, aborting\n", s);
+            return EXIT_FAILURE;
+        }
+        for (j = 0; j < MAXFILES; j++) {
+            s = GetRandomNumberString ();
+            if ((fd = open (s, O_RDWR | O_CREAT, 0666)) < 3) {
+                perror ("CREATE");
+                return EXIT_FAILURE;
+            }
+            close (fd);
+        }
+        chdir ("..");
+    }
+    return EXIT_SUCCESS;
+}
new file mode 100644
--- /dev/null
+++ b/fork.c
@@ -0,0 +1,46 @@
+/**
+ * test/demos/fork.c
+ * Copyright (C) Markus Broeker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <errno.h>
+
+int fork_proc (char **argv, char **argp)
+{
+    int pid;
+    char *cmd = argv[0];
+
+    pid = fork ();
+    switch (pid) {
+    case 0:
+        printf ("Starting new Process\n");
+        execve (cmd, argv, argp);
+    case -1:
+        perror ("FORK");
+        return errno;
+    default:
+        printf ("Pid started as %d\n", pid);
+        wait (&pid);
+        printf ("Exit-Status = %d\n", pid);
+        return pid;
+    }
+}
+
+int main (int argc, char **argv)
+{
+    char *args[] = {
+        "/bin/ls",
+        "-al",
+        NULL
+    };
+
+    if (fork_proc (args, NULL) == 0) {
+        printf ("It just works :P\n");
+    }
+
+    return EXIT_SUCCESS;
+}