1 | /* This file is part of the Project Athena Global Message System. |
---|
2 | * Created by: Mark W. Eichin <eichin@athena.mit.edu> |
---|
3 | * $Source: /afs/dev.mit.edu/source/repository/athena/bin/gms/read_to_memory.c,v $ |
---|
4 | * $Author: ghudson $ |
---|
5 | * |
---|
6 | * Copyright (c) 1988 by the Massachusetts Institute of Technology. |
---|
7 | * For copying and distribution information, see the file |
---|
8 | * "mit-copyright.h". |
---|
9 | */ |
---|
10 | #include <mit-copyright.h> |
---|
11 | #ifndef lint |
---|
12 | static char rcsid_read_to_memory_c[] = "$Header: /afs/dev.mit.edu/source/repository/athena/bin/gms/read_to_memory.c,v 1.2 1996-09-19 22:39:20 ghudson Exp $"; |
---|
13 | #endif lint |
---|
14 | |
---|
15 | #include "globalmessage.h" |
---|
16 | |
---|
17 | Code_t read_to_memory(ret_block, ret_size, filedesc) |
---|
18 | char **ret_block; |
---|
19 | int *ret_size, filedesc; |
---|
20 | { |
---|
21 | char buf[BFSZ], *message_data; |
---|
22 | int message_size = 0; |
---|
23 | int stat; |
---|
24 | |
---|
25 | do { |
---|
26 | /* read the block */ |
---|
27 | stat = read(filedesc, buf, BFSZ); |
---|
28 | if(stat == -1) { |
---|
29 | /* handle read failed error */ |
---|
30 | if(message_size) { |
---|
31 | free(message_data); |
---|
32 | } |
---|
33 | return(errno); |
---|
34 | } |
---|
35 | |
---|
36 | /* allocate a memory area for copying */ |
---|
37 | /* the +1 are for trailing NUL's */ |
---|
38 | if(message_size) { |
---|
39 | message_data=realloc(message_data, message_size + stat +1); |
---|
40 | } else { |
---|
41 | message_data = malloc(message_size + stat +1); |
---|
42 | } |
---|
43 | if(!message_data) { |
---|
44 | return(GMS_MALLOC_ERR); |
---|
45 | } |
---|
46 | |
---|
47 | /* copy it into the right place */ |
---|
48 | memcpy(&message_data[message_size], buf, stat); |
---|
49 | |
---|
50 | message_size += stat; |
---|
51 | } while(stat); |
---|
52 | /* but only until we stop getting blocks. */ |
---|
53 | |
---|
54 | /* Just to make it consistent, for lazy calling routines... */ |
---|
55 | message_data[message_size] = '\0'; |
---|
56 | |
---|
57 | *ret_block = message_data; |
---|
58 | *ret_size = message_size; |
---|
59 | return(0); |
---|
60 | } |
---|
61 | |
---|