File: t_ncio.c

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (376 lines) | stat: -rw-r--r-- 7,462 bytes parent folder | download | duplicates (9)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 *   Copyright 2018, University Corporation for Atmospheric Research
 *   See top level COPYRIGHT file for copying and redistribution conditions.
 */
/* $Id: t_ncio.c,v 1.10 2010/05/26 11:11:26 ed Exp $ */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "ncio.h"
#ifndef NC_NOERR
#define NC_NOERR 0
#endif


static void
usage(const char *av0)
{
	(void)fprintf(stderr,
		"Usage: %s [options] fname\t\nOptions:\n", av0);
	(void)fprintf(stderr,
		"\t-v		Verbose\n") ;
	(void)fprintf(stderr,
		"\t-w		Open Read/Write, default is read only\n") ;
	(void)fprintf(stderr,
		"\t-c		Create, clobber existing\n") ;
	(void)fprintf(stderr,
		"\t-n		Create, error if it already exists\n") ;
	(void)fprintf(stderr,
		"\t-L		Use locking if available\n") ;
	(void)fprintf(stderr,
		"\t-S		Share updates (turn off caching)\n") ;
	(void)fprintf(stderr,
		"\t-U		Delete (unlink) on close\n") ;
	(void)fprintf(stderr,
		"\t-o igeto	Initial get offset\n") ;
	(void)fprintf(stderr,
		"\t-i igetsz	Initial get size\n") ;
	(void)fprintf(stderr,
		"\t-I initialsz	Initial file size for create\n") ;
	(void)fprintf(stderr,
		"\t-s sizehint	Buffer size\n") ;
	exit(EXIT_FAILURE);
}


static long
argscale(const char *arg, const char *tag)
{
	long value = 0;
		/* last character */
	const char *cp = arg + strlen(arg) -1;
	value = atol(arg);
	if(isalpha(*cp))
	{
		switch(*cp) {
		case 'k':
		case 'K':
			value *= 1024;
			break;
		case 'm':
		case 'M':
			value *= (1024*1024);
			break;
		default:
			value = 0; /* trigger error below */
			break;
		}
	}
	if(value == 0)
	{
		fprintf(stderr,
			 "Illegal %s \"%s\", ignored\n", tag, arg);
	}
	return value;
}


static void
modify_ex(off_t offset, size_t extent, void *vp)
{
	unsigned char *obuf = vp;
	const unsigned char *const end = &obuf[extent];
	unsigned char *cp = obuf;

	if(cp >= end) return;
	*cp++ = (unsigned char)( offset               >> 24);
	if(cp >= end) return;
	*cp++ = (unsigned char)((offset & 0x00ff0000) >> 16);
	if(cp >= end) return;
	*cp++ = (unsigned char)((offset & 0x0000ff00) >>  8);
	if(cp >= end) return;
	*cp++ = (unsigned char)( offset & 0x000000ff);
	if(cp >= end) return;
	*cp++ = (unsigned char)( extent               >> 24);
	if(cp >= end) return;
	*cp++ = (unsigned char)((extent & 0x00ff0000) >> 16);
	if(cp >= end) return;
	*cp++ = (unsigned char)((extent & 0x0000ff00) >>  8);
	if(cp >= end) return;
	*cp++   = (unsigned char)( extent & 0x000000ff);

	while(cp < end)
	{
		*cp++ = (unsigned char) (cp - obuf);
	}
}


typedef struct riu {
	struct riu *next;
	struct riu *prev;
	off_t offset;
	size_t extent;
	void *vp;
} riu;

static void
free_riu(riu *riup)
{
	if(riup == NULL)
		return;
	free(riup);
}

static riu *
new_riu(off_t offset, size_t extent, void *vp)
{
	riu *riup = (riu *)malloc(sizeof(riu));
	if(riup == NULL)
	{
		fprintf(stderr,
			"new_riu: malloc failed\n");
		exit(EXIT_FAILURE);
	}
	riup->next = NULL;
	riup->prev = NULL;
	riup->offset = offset;
	riup->extent = extent;
	riup->vp = vp;
	return riup;
}

static riu *stack = NULL;

static void
riu_push(off_t offset, size_t extent, void *vp)
{
	riu *riup = new_riu(offset, extent, vp);
	/* assert(riup != NULL); */
	riup->next = stack;
	if(stack != NULL)
		stack->prev = riup;
	stack = riup;
}

static int
riu_pop(off_t offset, int modify)
{
	riu *riup = stack;
	while(riup != NULL)
	{
		if(riup->offset == offset)
		{
			if(modify)
			{
				modify_ex(riup->offset, riup->extent, riup->vp);
			}
			if(riup->next != NULL)
			{
				riup->next->prev = riup->prev;
			}
			if(riup == stack)
			{
				stack = riup->next;
			}
			else
			{
				assert(riup->prev != NULL);
				riup->prev->next = riup->next;
			}
			free_riu(riup);
			return 1;
		}
		riup = riup->next;
	}
	/* else, not found */
	return 0;
}

main(int ac, char *av[])
{
	char *path = "";
	ncio *nciop;
	char linebuf[128];
	off_t offset;
	size_t extent;
	void *vp;
	int status = NC_NOERR;
	int verbose = 0;
	int flags = 0;
	int create = 0;
	off_t igeto = 0;
	size_t igetsz = 0;
	size_t initialsz = 0;
	int doUnlink = 0;
	size_t sizehint = NC_SIZEHINT_DEFAULT;

	{
	extern int optind;
	extern int opterr;
	extern char *optarg;
	int ch;

	opterr = 1;

	while ((ch = getopt(ac, av, "vwcnLSUo:i:I:s:")) != EOF)
		switch (ch) {
		case 'v':
			verbose = 1;
			break;
		case 'w':
			flags |= NC_WRITE;
			break;
		case 'c':
			create = 1;
			break;
		case 'n':
			create = 1;
			flags |= NC_NOCLOBBER;
			break;
		case 'L':
			flags |= NC_LOCK;
			break;
		case 'S':
			flags |= NC_SHARE;
			break;
		case 'U':
			doUnlink = 1;
			break;
		case 'o':
			igeto = argscale(optarg, "igeto");
			break;
		case 'i':
			igetsz = argscale(optarg, "igetsz");
			break;
		case 'I':
			initialsz = argscale(optarg, "initialsz");
			break;
		case 's':
			sizehint = argscale(optarg, "sizehint");
			break;
		case '?':
			usage(av[0]);
			break;
		}

	/* last arg, the file name, is required */
	if(ac - optind <= 0)
		usage(av[0]) ;
	path = av[optind];


	}

	if(!create)
	{
		status = ncio_open(path, flags,
				igeto, igetsz, &sizehint,
				&nciop, &vp);
		if(status != NC_NOERR)
		{
			fprintf(stderr, "ncio_open: %s: %s\n",
				path, strerror(status));
			return(EXIT_FAILURE);
		}
	} else {
		status = ncio_create(path, flags, initialsz,
			igeto, igetsz, &sizehint,
			&nciop, &vp);
		if(status != NC_NOERR)
		{
			fprintf(stderr, "ncio_create: %s: %s\n",
				path, strerror(status));
			return(EXIT_FAILURE);
		}
	}

	while(fgets(linebuf, sizeof(linebuf), stdin) != NULL)
	{
		offset = 0;
		extent = 0;

		if(*linebuf == '#')
			continue; /* comment */
		if(sscanf(linebuf, "rel 0x%lx", &offset) == 1
			|| sscanf(linebuf, "rel %ld", &offset) == 1)
		{
			if(verbose)
				printf("- rel  %8ld\n", offset);
			if(!riu_pop(offset, 0))
				continue;
			status = nciop->rel(nciop, offset, 0);
			if(status)
			{
				fprintf(stderr, "- rel  error: %s\n",
					strerror(status));
				continue;
			}
		}
		else if(sscanf(linebuf, "relm 0x%lx", &offset) == 1
			|| sscanf(linebuf, "relm %ld", &offset) == 1)
		{
			if(verbose)
				printf("- relm %8ld\n", offset);
			if(!riu_pop(offset, 1))
				continue;
			status = nciop->rel(nciop, offset, RGN_MODIFIED);
			if(status)
			{
				fprintf(stderr, "- relm %8ld error: %s\n",
					offset, strerror(status));
				continue;
			}
		}
		else if(sscanf(linebuf, "get 0x%lx %ld", &offset, &extent) == 2
			|| sscanf(linebuf, "get %ld %ld", &offset, &extent) == 2)
		{
			if(verbose)
				printf("- get  %10ld %8ld\n", offset, extent);
			status = nciop->get(nciop, offset, extent, 0, &vp);
			if(status)
			{
				fprintf(stderr, "- get  error: %s\n",
					strerror(status));
				continue;
			}
			riu_push(offset, extent, vp);
		}
		else if(sscanf(linebuf, "getw 0x%lx %ld", &offset, &extent) == 2
			|| sscanf(linebuf, "getw %ld %ld", &offset, &extent) == 2)
		{
			if(verbose)
				printf("- getw %10ld %8ld\n", offset, extent);
			status = nciop->get(nciop, offset, extent, RGN_WRITE, &vp);
			if(status)
			{
				fprintf(stderr, "- getw  error: %s\n",
					strerror(status));
				continue;
			}
			riu_push(offset, extent, vp);
		}
		else if(strchr(linebuf, 'q') != NULL)
			break;
		else
			printf("???\n");
	}

	status = ncio_close(nciop, doUnlink);
	if(status != NC_NOERR)
	{
		fprintf(stderr, "ncio_close(%s): %s: %s\n",
			doUnlink ? "doUnlink" : "",
			path, strerror(status));
		return(EXIT_FAILURE);
	}

	return(EXIT_SUCCESS);
}