rev |
line source |
yuuji@0
|
1 /* ========================================================================
|
yuuji@0
|
2 * Copyright 1988-2007 University of Washington
|
yuuji@0
|
3 *
|
yuuji@0
|
4 * Licensed under the Apache License, Version 2.0 (the "License");
|
yuuji@0
|
5 * you may not use this file except in compliance with the License.
|
yuuji@0
|
6 * You may obtain a copy of the License at
|
yuuji@0
|
7 *
|
yuuji@0
|
8 * http://www.apache.org/licenses/LICENSE-2.0
|
yuuji@0
|
9 *
|
yuuji@0
|
10 *
|
yuuji@0
|
11 * ========================================================================
|
yuuji@0
|
12 */
|
yuuji@0
|
13
|
yuuji@0
|
14 /*
|
yuuji@0
|
15 * Program: File descriptor string routines
|
yuuji@0
|
16 *
|
yuuji@0
|
17 * Author: Mark Crispin
|
yuuji@0
|
18 * Networks and Distributed Computing
|
yuuji@0
|
19 * Computing & Communications
|
yuuji@0
|
20 * University of Washington
|
yuuji@0
|
21 * Administration Building, AG-44
|
yuuji@0
|
22 * Seattle, WA 98195
|
yuuji@0
|
23 * Internet: MRC@CAC.Washington.EDU
|
yuuji@0
|
24 *
|
yuuji@0
|
25 * Date: 15 April 1997
|
yuuji@0
|
26 * Last Edited: 4 April 2007
|
yuuji@0
|
27 */
|
yuuji@0
|
28
|
yuuji@0
|
29 #include "mail.h"
|
yuuji@0
|
30 #include "osdep.h"
|
yuuji@0
|
31 #include "misc.h"
|
yuuji@0
|
32 #include "fdstring.h"
|
yuuji@0
|
33
|
yuuji@0
|
34 /* String driver for fd stringstructs */
|
yuuji@0
|
35
|
yuuji@0
|
36 static void fd_string_init (STRING *s,void *data,unsigned long size);
|
yuuji@0
|
37 static char fd_string_next (STRING *s);
|
yuuji@0
|
38 static void fd_string_setpos (STRING *s,unsigned long i);
|
yuuji@0
|
39
|
yuuji@0
|
40 STRINGDRIVER fd_string = {
|
yuuji@0
|
41 fd_string_init, /* initialize string structure */
|
yuuji@0
|
42 fd_string_next, /* get next byte in string structure */
|
yuuji@0
|
43 fd_string_setpos /* set position in string structure */
|
yuuji@0
|
44 };
|
yuuji@0
|
45
|
yuuji@0
|
46
|
yuuji@0
|
47 /* Initialize string structure for fd stringstruct
|
yuuji@0
|
48 * Accepts: string structure
|
yuuji@0
|
49 * pointer to string
|
yuuji@0
|
50 * size of string
|
yuuji@0
|
51 */
|
yuuji@0
|
52
|
yuuji@0
|
53 static void fd_string_init (STRING *s,void *data,unsigned long size)
|
yuuji@0
|
54 {
|
yuuji@0
|
55 FDDATA *d = (FDDATA *) data;
|
yuuji@0
|
56 /* note fd */
|
yuuji@0
|
57 s->data = (void *) (unsigned long) d->fd;
|
yuuji@0
|
58 s->data1 = d->pos; /* note file offset */
|
yuuji@0
|
59 s->size = size; /* note size */
|
yuuji@0
|
60 s->curpos = s->chunk = d->chunk;
|
yuuji@0
|
61 s->chunksize = (unsigned long) d->chunksize;
|
yuuji@0
|
62 s->offset = 0; /* initial position */
|
yuuji@0
|
63 /* and size of data */
|
yuuji@0
|
64 s->cursize = min (s->chunksize,size);
|
yuuji@0
|
65 /* move to that position in the file */
|
yuuji@0
|
66 lseek (d->fd,d->pos,L_SET);
|
yuuji@0
|
67 read (d->fd,s->chunk,(size_t) s->cursize);
|
yuuji@0
|
68 }
|
yuuji@0
|
69
|
yuuji@0
|
70 /* Get next character from fd stringstruct
|
yuuji@0
|
71 * Accepts: string structure
|
yuuji@0
|
72 * Returns: character, string structure chunk refreshed
|
yuuji@0
|
73 */
|
yuuji@0
|
74
|
yuuji@0
|
75 static char fd_string_next (STRING *s)
|
yuuji@0
|
76 {
|
yuuji@0
|
77 char c = *s->curpos++; /* get next byte */
|
yuuji@0
|
78 SETPOS (s,GETPOS (s)); /* move to next chunk */
|
yuuji@0
|
79 return c; /* return the byte */
|
yuuji@0
|
80 }
|
yuuji@0
|
81
|
yuuji@0
|
82
|
yuuji@0
|
83 /* Set string pointer position for fd stringstruct
|
yuuji@0
|
84 * Accepts: string structure
|
yuuji@0
|
85 * new position
|
yuuji@0
|
86 */
|
yuuji@0
|
87
|
yuuji@0
|
88 static void fd_string_setpos (STRING *s,unsigned long i)
|
yuuji@0
|
89 {
|
yuuji@0
|
90 if (i > s->size) i = s->size; /* don't permit setting beyond EOF */
|
yuuji@0
|
91 s->offset = i; /* set new offset */
|
yuuji@0
|
92 s->curpos = s->chunk; /* reset position */
|
yuuji@0
|
93 /* set size of data */
|
yuuji@0
|
94 if (s->cursize = min (s->chunksize,SIZE (s))) {
|
yuuji@0
|
95 /* move to that position in the file */
|
yuuji@0
|
96 lseek ((long) s->data,s->data1 + s->offset,L_SET);
|
yuuji@0
|
97 read ((long) s->data,s->curpos,(size_t) s->cursize);
|
yuuji@0
|
98 }
|
yuuji@0
|
99 }
|