duff.c
changeset 57 7739518acafb
parent 56 966ad681f25d
child 63 5a82f89d607e
--- 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;
 }