duff.c
changeset 57 7739518acafb
parent 56 966ad681f25d
child 63 5a82f89d607e
equal deleted inserted replaced
56:966ad681f25d 57:7739518acafb
     6 
     6 
     7 #include <stdio.h>
     7 #include <stdio.h>
     8 #include <stdlib.h>
     8 #include <stdlib.h>
     9 #include <string.h>
     9 #include <string.h>
    10 
    10 
    11 char *duffcopy (char *to, char *from, int num)
    11 char *duffcopy (char *to, char *from, int len)
    12 {
    12 {
    13     int n, pos = 0;
    13     int n, pos = 0;
    14 
    14 
    15     n = (num + 7) / 8;
    15     n = (len + 7) / 8;
    16 
    16 
    17     switch (num % 8) {
    17     switch (len % 8) {
    18     case 0:
    18     case 0:
    19         do {
    19         do {
    20             to[pos] = from[pos];
    20             to[pos] = from[pos];
    21             pos++;
    21             pos++;
    22     case 7:
    22     case 7:
    46     return to;
    46     return to;
    47 }
    47 }
    48 
    48 
    49 int main (int argc, char **argv)
    49 int main (int argc, char **argv)
    50 {
    50 {
    51     char to[80], *from;
    51     char *to, *from;
       
    52     int len;
    52 
    53 
    53     if (argc != 2) {
    54     if (argc != 2) {
    54         printf ("Usage: %s <string>\n", argv[0]);
    55         printf ("Usage: %s <string>\n", argv[0]);
    55         return EXIT_FAILURE;
    56         return EXIT_FAILURE;
    56     }
    57     }
    57 
    58 
    58     from = argv[1];
    59     from = argv[1];
       
    60     len = strlen (from) + 1;
    59 
    61 
    60     printf ("DUFF: %s\n", duffcopy (to, from, strlen (from)));
    62     if ((to = malloc (len)) == NULL) {
       
    63         perror ("MALLOC");
       
    64         return EXIT_FAILURE;
       
    65     }
       
    66 
       
    67     printf ("DUFF: %s\n", duffcopy (to, from, len));
       
    68 
       
    69     if (to != NULL) {
       
    70         free (to);
       
    71     }
       
    72 
    61     return EXIT_SUCCESS;
    73     return EXIT_SUCCESS;
    62 }
    74 }