source: trunk/athena/bin/get_message/read_to_memory.c @ 14063

Revision 14063, 1.7 KB checked in by danw, 24 years ago (diff)
get_message, formerly in athena/bin/gms
Line 
1/* Copyright 1988, 1998 by the Massachusetts Institute of Technology.
2 *
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose.  It is provided "as is"
13 * without express or implied warranty.
14 */
15
16static const char rcsid[] = "$Id: read_to_memory.c,v 1.1 1999-12-08 22:06:46 danw Exp $";
17
18#include "globalmessage.h"
19
20Code_t read_to_memory(char **ret_block, int *ret_size, int filedesc)
21{
22  char buf[BFSZ], *message_data = NULL;
23  int message_size = 0;
24  int stat;
25 
26  do {
27    /* read the block */
28    stat = read(filedesc, buf, BFSZ);
29    if(stat == -1) {
30      /* handle read failed error */
31      free(message_data);
32      return(errno);
33    }
34
35    /* allocate a memory area for copying */
36    /* the +1 are for trailing NULs */
37    message_data = realloc(message_data, message_size + stat + 1);
38    if(!message_data) {
39      return(GMS_MALLOC_ERR);
40    }
41     
42    /* copy it into the right place */
43    memcpy(&message_data[message_size], buf, stat);
44
45    message_size += stat;
46  } while(stat);
47    /* but only until we stop getting blocks. */
48
49  /* Just to make it consistent, for lazy calling routines... */
50  message_data[message_size] = '\0';
51 
52  *ret_block = message_data;
53  *ret_size = message_size;
54  return(0);
55}
56 
Note: See TracBrowser for help on using the repository browser.