File: filecopy2offset.c

package info (click to toggle)
yodl 4.05.02-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,724 kB
  • sloc: ansic: 7,803; perl: 683; cpp: 570; sh: 411; xml: 190; makefile: 163
file content (56 lines) | stat: -rw-r--r-- 1,977 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "file.ih"

void file_copy2offset(FILE *dest, FILE *src, long src_end)
{
    register char *buffer = new_memory(BLOCK_FILE, sizeof(char));
    long tocopy = src_end - ftell(src);
 
    if (tocopy <= 0)                            /* program may have eaten   */
    {                                           /* some bytes, yet          */
        int nread;
        if (src_end != EOF)                     /* not copy to EOF ?        */
        {
            free(buffer);
            return;                             /* then done                */
        }

        while (true)
        {
            nread = fread(buffer, 1, BLOCK_FILE, src);
            if (nread <= 0)
            {
                free(buffer);
                return;
            }
            fwrite(buffer, (size_t)nread, 1, dest);
        }
    }

    while                                       /* All until last BLOCK     */
    (
        tocopy > BLOCK_FILE                     /* more than 1 block waits  */
        &&                                      /* AND */
        fread(buffer, BLOCK_FILE, 1, src) == 1  /* we read 1 block */
        &&                                      /* AND */
        fwrite(buffer, BLOCK_FILE, 1, dest) == 1/* we wrote 1 block */
    )
        tocopy -= BLOCK_FILE;

    if                                          /* copy the last block      */
    (
        tocopy <= BLOCK_FILE                    /* at most 1 block waits    */
        &&                                      /* AND */
                                                /* we read the req'd chars  */
        fread(buffer, (size_t)tocopy, 1, src) == 1     
        &&                                      /* AND */
                                                /* we wrote the req'd chars */
        fwrite(buffer, (size_t)tocopy, 1, dest) == 1   
    )
    {
        free(buffer);
        return;
    }

    message_show(MSG_ALERT);
    message("file_copy2offset() r/w error");
}