~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina4/src/sys/gen/io.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <tina/sys.h>
  4 #include <tina/sysfuncs.h>
  5 
  6 #define TS_MAGIC            0x80008000
  7 #define TS_MAGIC_NEED_SWAP  0x00800080
  8 
  9 #undef TTCL_DEBUG
 10 
 11 static int magic = TS_MAGIC;
 12 
 13 
 14 void fwrite_ts(FILE * fp, List * lg, int c)
 15 {
 16         List *l;
 17         int rval;
 18 
 19         fwrite(&magic, sizeof(int), 1, fp);
 20 
 21         fwrite(&c, sizeof(int), 1, fp);
 22 
 23         for (l = lg; l; l = l->next)
 24                 rval = fwrite(&(l->type), sizeof(int), 1, fp);
 25 
 26         for (l = lg; l; l = l->next)
 27                 if (l->type > 0)
 28                         rval = fwrite(l->to, l->type, 1, fp);
 29 }
 30 
 31 
 32 void swrite_ts(int fd, List * lg, int c)
 33 {
 34         List *l;
 35         int rval;
 36 
 37         write(fd, &magic, sizeof(int));
 38         write(fd, &c, sizeof(int));
 39 
 40         for (l = lg; l; l = l->next)
 41         {
 42                 rval = write(fd, &(l->type), sizeof(int));
 43 #ifdef TTCL_DEBUG
 44                 printf("sent %d bytes\n", rval);
 45 #endif
 46         }
 47 
 48         for (l = lg; l; l = l->next)
 49                 if (l->type > 0)
 50                 {
 51                         rval = write(fd, l->to, l->type);
 52 #ifdef TTCL_DEBUG
 53                         printf("sent %d bytes\n", rval);
 54 #endif
 55                 }
 56 }
 57 
 58 
 59 List *fread_ts(FILE * fp, int *c)
 60 {
 61         List *l = NULL;
 62         int *s, count, magic, rval;
 63         void **d;
 64 
 65         rval = fread(&magic, sizeof(int), 1, fp);
 66         set_swapping_ts((magic == TS_MAGIC_NEED_SWAP));
 67         rval = fread(c, sizeof(int), 1, fp);
 68 
 69         word_swap((char *) c);
 70         s = (int *) ralloc(sizeof(int) * (*c));
 71         d = (void **) ralloc(sizeof(void *) * (*c));
 72 
 73         for (count = 0; count < *c; count++)
 74         {
 75                 rval = fread(s + count, sizeof(int), 1, fp);
 76 
 77                 word_swap((char *) (s + count));
 78                 d[count] = s[count] > 0 ? (void *) ralloc(s[count]) : NULL;
 79         }
 80         for (count = *c - 1; count >= 0; count--)
 81                 if (!d[count])
 82                         d[count] = d[count - s[count]];
 83         for (count = 0; count < *c; count++)
 84         {
 85                 l = ref_addtostart(l, d[count], s[count]);
 86                 if (s[count] > 0)
 87                 {
 88                         rval = fread(l->to, l->type, 1, fp);
 89                         if (rval != 1)
 90                                 fprintf(stderr, "fread_ts: Error reading data item %i\n", count);
 91                 }
 92         }
 93         rfree(s);
 94         rfree(d);
 95         return l;
 96 }
 97 
 98 
 99 static int              sock_read(int fd, int *data, size_t size)
100 {
101         int     red = 0, count = 0; 
102         size_t  total = 0;
103         
104         while((total < size) && count < 50)
105         {
106                 red = read(fd, data, (size_t)(size-total));     
107                 total += red;
108                 data += red;
109         }
110 
111         return(red);
112 }
113                                 
114 
115 
116 List *sread_ts(int fd, int *c)
117 {
118         List *l = NULL;
119         int *s, count, magic, rval;
120         void **d;
121 
122         rval = sock_read(fd, &magic, sizeof(int));
123 #ifdef TTCL_DEBUG
124         printf("got %d bytes\n", rval);
125 #endif
126         set_swapping_ts((magic == TS_MAGIC_NEED_SWAP));
127         rval = sock_read(fd, c, sizeof(int));
128 #ifdef TTCL_DEBUG
129         printf("got %d bytes\n", rval);
130 #endif
131 
132         word_swap((char *) c);
133         s = (int *) ralloc(sizeof(int) * (*c));
134         d = (void **) ralloc(sizeof(void *) * (*c));
135 
136         for (count = 0; count < *c; count++)
137         {
138                 rval = sock_read(fd, s + count, sizeof(int));
139 #ifdef TTCL_DEBUG
140                 printf("got %d bytes\n", rval);
141 #endif
142 
143                 word_swap((char *) (s + count));
144                 d[count] = s[count] > 0 ? (void *) ralloc(s[count]) : NULL;
145         }
146         for (count = *c - 1; count >= 0; count--)
147                 if (!d[count])
148                         d[count] = d[count - s[count]];
149         for (count = 0; count < *c; count++)
150         {
151                 l = ref_addtostart(l, d[count], s[count]);
152                 if (s[count] > 0)
153                 {
154                         rval = sock_read(fd, l->to, l->type);
155 #ifdef TTCL_DEBUG
156                         printf("got %d bytes\n", rval);
157 #endif
158                 }
159         }
160         rfree(s);
161         rfree(d);
162         return l;
163 }
164 
165 
166 void buffwrite_ts(char *mem, List * lg, int c)
167 {
168         List *l;
169         Varptr m;
170         void *memcpy();
171 
172         m.char_v = mem;
173         *(m.int_v++) = magic;                                   /*
174                                                                                                                                  * put magic no into data 
175                                                                                                                                  */
176         *(m.int_v++) = c;
177         for (l = lg; l; l = l->next)
178                 *(m.int_v++) = l->type;
179         for (l = lg; l; l = l->next)
180                 if (l->type > 0)
181                         memcpy(m.char_v, l->to, l->type), m.char_v += l->type;
182 }
183 List *buffread_ts(char *mem, int *c)
184 {
185         List *l = NULL;
186         Varptr m;
187         int *s, count, magic;
188         void **d;
189 
190         m.char_v = mem;
191 
192         magic = *(m.int_v++);                                   /*
193                                                                                                                                  * get magic no out 
194                                                                                                                                  */
195         set_swapping_ts((magic == TS_MAGIC_NEED_SWAP)); /*
196                                                                                                                                                                                                          * determine if swapping 
197                                                                                                                                                                                                          */
198         word_swap(m.char_v);
199         *c = *m.int_v++;
200         s = (int *) ralloc(sizeof(int) * (*c));
201         d = (void **) ralloc(sizeof(void *) * (*c));
202 
203         for (count = 0; count < *c; count++)    /*
204                                                                                                                                                                  * ralloc all the data 
205                                                                                                                                                                  */
206                 /*
207                  * blocks 
208                  */
209         {
210                 word_swap(m.char_v);
211                 s[count] = *m.int_v++;
212                 d[count] = s[count] > 0 ? ralloc(s[count]) : NULL;
213         }
214         for (count = *c - 1; count >= 0; count--)       /*
215                                                                                                                                                                                  * setup replicated 
216                                                                                                                                                                                  */
217                 /*
218                  * pointers 
219                  */
220                 if (!d[count])
221                         d[count] = d[count - s[count]];
222         for (count = 0; count < *c; count++)    /*
223                                                                                                                                                                  * copy data into blocks 
224                                                                                                                                                                  */
225         {
226                 l = ref_addtostart(l, d[count], s[count]);
227                 if (s[count] > 0)
228                         memcpy(l->to, m.char_v, l->type), m.char_v += l->type;
229         }
230         rfree(s);
231         rfree(d);
232         return l;
233 }
234 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.