File: iovec.c

package info (click to toggle)
unsort 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 532 kB
  • ctags: 134
  • sloc: ansic: 1,401; sh: 1,042; makefile: 5
file content (139 lines) | stat: -rw-r--r-- 2,885 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*****************************************************************************

	unsort - reorder files semi-randomly
	Copyright (C) 2007, 2008  Wessel Dankers <wsl@fruit.je>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

*****************************************************************************/

#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/uio.h>

#include "error.h"
#include "filebuf.h"
#include "iovec.h"

struct iovec_const {
	const void *iov_base;
	size_t iov_len;
};

#ifndef IOV_MAX
#define IOV_MAX _XOPEN_IOV_MAX
#endif

static uint32_t writev_max = IOV_MAX;

void iovec_write(int fd, struct iovec *iov, uint32_t count) {
	ssize_t r;
	size_t len;

	if(!count)
		return;

	while(count) {
		r = writev(fd, iov, (int)(count > writev_max ? writev_max : count));
		if(r < 0)
			exit_perror(ERROR_SYSTEM, "Can't write to fd %d", fd);
		if(!r)
			exit_error(ERROR_SYSTEM, "Can't write to fd %d", fd);
		while(r) {
			len = iov->iov_len;
			if((size_t)r < len) {
				iov->iov_base = (char *)iov->iov_base + r;
				iov->iov_len -= (size_t)r;
				r = 0;
			} else {
				r -= (ssize_t)len;
				iov++;
				count--;
			}
		}
	}
}

bool iovec_parse(filebuf_t *fb, int sep, struct iovec *iov, uint32_t *count) {
	const char *buf, *end, *line;
	uint32_t u = 0;
	struct iovec_const *ioc;

	buf = fb->buf;
	end = buf + fb->len;

	while(buf < end) {
		line = (const char *)memchr(buf, sep, (size_t)(end - buf));
		if(!line)
			break;
		line++;
		/* buf..line is nu een regel */
		if(iov) {
			ioc = (struct iovec_const *)iov + u;
			ioc->iov_base = buf;
			ioc->iov_len = (size_t)(line - buf);
		}
		u++;
		buf = line;
	}

	if(count)
		*count = u;

	return buf < end;
}

void iovec_shuffle(struct iovec *iov, uint32_t *tlb, uint32_t count) {
	uint32_t start;
	uint32_t m, n;
	struct iovec v, w;

	for(start = 0; start < count; start++) {
		m = tlb[start];
		if(m == start)
			continue;
		v = iov[start];

		for(;;) {
			w = iov[m];
			iov[m] = v;

			n = tlb[m];
			tlb[m] = m;

			if(n == start) {
				iov[start] = w;
				break;
			}

			v = iov[n];
			iov[n] = w;

			m = tlb[n];
			tlb[n] = n;

			if(m == start) {
				iov[start] = v;
				break;
			}
		}
	}
}