duff.c:
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:13 +0200
changeset 57 7739518acafb
parent 56 966ad681f25d
child 58 9609c54a54d3
duff.c: * Large String support added * parameter num renamed to len committer: Markus Bröker <mbroeker@largo.homelinux.org>
duff.c
--- a/duff.c
+++ b/duff.c
@@ -8,13 +8,13 @@
 #include <stdlib.h>
 #include <string.h>
 
-char *duffcopy (char *to, char *from, int num)
+char *duffcopy (char *to, char *from, int len)
 {
     int n, pos = 0;
 
-    n = (num + 7) / 8;
+    n = (len + 7) / 8;
 
-    switch (num % 8) {
+    switch (len % 8) {
     case 0:
         do {
             to[pos] = from[pos];
@@ -48,7 +48,8 @@
 
 int main (int argc, char **argv)
 {
-    char to[80], *from;
+    char *to, *from;
+    int len;
 
     if (argc != 2) {
         printf ("Usage: %s <string>\n", argv[0]);
@@ -56,7 +57,18 @@
     }
 
     from = argv[1];
+    len = strlen (from) + 1;
 
-    printf ("DUFF: %s\n", duffcopy (to, from, strlen (from)));
+    if ((to = malloc (len)) == NULL) {
+        perror ("MALLOC");
+        return EXIT_FAILURE;
+    }
+
+    printf ("DUFF: %s\n", duffcopy (to, from, len));
+
+    if (to != NULL) {
+        free (to);
+    }
+
     return EXIT_SUCCESS;
 }