~ubuntu-branches/ubuntu/karmic/unsort/karmic

« back to all changes in this revision

Viewing changes to filebuf.c

  • Committer: Bazaar Package Importer
  • Author(s): Guus Sliepen
  • Date: 2008-06-08 12:53:54 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080608125354-yizugn2e7fxzxuix
Tags: 1.1.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*****************************************************************************
2
2
 
3
3
        unsort - reorder files semi-randomly
4
 
        Copyright (C) 2007  Wessel Dankers <wsl@fruit.je>
 
4
        Copyright (C) 2007, 2008  Wessel Dankers <wsl@fruit.je>
5
5
 
6
6
        This program is free software: you can redistribute it and/or modify
7
7
        it under the terms of the GNU General Public License as published by
16
16
        You should have received a copy of the GNU General Public License
17
17
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
 
        $Id: filebuf.c 1234 2007-11-26 00:26:28Z wsl $
 
19
        $Id: filebuf.c 1323 2008-06-07 19:58:01Z wsl $
20
20
        $URL: http://rot.zo.oi/svn/wsl/src/unsort/filebuf.c $
21
21
 
22
22
*****************************************************************************/
35
35
#include "filebuf.h"
36
36
 
37
37
const filebuf_t filebuf_0 = {0};
38
 
filebuf_t *filebuf_first = NULL;
39
 
static filebuf_t *filebuf_last = NULL;
40
38
 
41
39
#define filebuf_off(buf, off) (((uint8_t *)(buf)) + (size_t)(off))
42
40
 
66
64
        }
67
65
}
68
66
 
69
 
 
70
67
static bool filebuf_mmap(int fd, filebuf_t *fb, size_t len) {
71
68
        void *buf;
72
69
        buf = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, (ssize_t)0);
74
71
                return true;
75
72
        fb->buf = buf;
76
73
        fb->len = len;
77
 
        fb->end = filebuf_off(buf, len);
78
74
        return false;
79
75
}
80
76
 
110
106
                        }
111
107
                        fb->buf = buf;
112
108
                        fb->len = len;
113
 
                        fb->end = filebuf_off(buf, len);
114
109
                        return;
115
110
                }
116
111
                len += r;
158
153
        close(tmp);
159
154
}
160
155
 
161
 
void filebuf_add(int fd) {
 
156
void filebuf_init(filebuf_t *fb, int fd) {
162
157
        struct stat st;
163
 
        filebuf_t fb = filebuf_0;
164
 
        filebuf_t *r;
165
 
 
166
 
        if(fstat(fd, &st) || !st.st_size || filebuf_mmap(fd, &fb, (size_t)st.st_size))
167
 
                filebuf_stream(fd, &fb);
168
 
 
169
 
        if(!fb.len)
170
 
                return;
171
 
 
172
 
        r = (filebuf_t *)xalloc(sizeof *r);
173
 
        memcpy(r, &fb, sizeof *r);
174
 
 
175
 
        if(filebuf_last)
176
 
                filebuf_last->next = r;
177
 
        else
178
 
                filebuf_first = r;
179
 
        filebuf_last = r;
 
158
 
 
159
        if(fstat(fd, &st) || !st.st_size || filebuf_mmap(fd, fb, (size_t)st.st_size))
 
160
                filebuf_stream(fd, fb);
180
161
}