File: utils.c

package info (click to toggle)
rpncalc 1.20-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 244 kB
  • ctags: 101
  • sloc: ansic: 871; sed: 72; makefile: 70
file content (109 lines) | stat: -rw-r--r-- 2,425 bytes parent folder | download | duplicates (2)
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
/************************************************************************
 * utils.c -- various utilities for rpncalc				*
 *									*
 * Refer to rpncalc.c for copyrights and license permissions.           *
 ************************************************************************/

/* $Id: utils.c,v 1.5 1998/01/03 22:57:39 david Exp $
 * $Log: utils.c,v $
 * Revision 1.5  1998/01/03 22:57:39  david
 * replaced HAVE_READLINE with HAVE_LIBREADLINE (for autoconf)
 *
 * Revision 1.4  1997/01/19 18:58:58  david
 * Changed dupstr(char *) to dupstr(const char *)
 *
 * Revision 1.4  1997/01/19 18:19:23  david
 * Changed dupstr(char *) to dupstr(const char *)
 *
 * Revision 1.2  1996/09/13 20:21:29  david
 * lclint additions
 *
 * Revision 1.1  1996/07/13 20:58:36  david
 * Added dupstr()
 * Moved getline() from rpncalc.c into utils.c
 *
 * Revision 1.0  1995/12/31 18:19:34  david
 * Initial revision
 *
 * Revision 1.0  1995/11/25 20:02:25  david
 * Initial revision
 * */

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

#include "utils.h"

#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif

#define LINEFRAGMENT 32

/*@out@*/ void *xmalloc(size_t size)	/* safe malloc */
{
  void *ptr;

  ptr=malloc(size);
  if (ptr == NULL) 
  {
    perror("Out of memory!"); 
    exit(1); 
  }

  return ptr;
}

/*@out@*/ void *xrealloc(void *ptr, size_t size)	/* save realloc */
{
  ptr=realloc(ptr, size);
  if (ptr == NULL) 
  {
    perror("Out of memory!"); 
    exit(1); 
  }

  return ptr;
}

char *dupstr(const char *s)
{
  char *r;

  r = (char *)xmalloc (strlen (s) + 1);
  strcpy (r, s);
  return (r);
}

/*@null@*/ char *getline(void)
/* Read a line of text without imposing an length limit.
   If we readline support is compiled in, use the readline function. */
{
  char *line=NULL;
#ifndef HAVE_LIBREADLINE
  int c;
  int i;
#endif

#ifdef HAVE_LIBREADLINE
  line=(char *)readline(""); 
#else
  i=0; line=(char *)xmalloc((size_t)(LINEFRAGMENT+1));
  while (((c = getc(stdin)) != EOF) && (c != '\n') && (c != '\0'))
  {
   if ((i > LINEFRAGMENT) &&  (i % LINEFRAGMENT) == 0) 
     line=(char *)xrealloc(line,(size_t)(LINEFRAGMENT+1));
   line[i]=(char)c; i++;
  }
  if ((c != EOF) && (c != '\0')) line[i]='\0';
  else
  {
    free(line); line=NULL;   /* this is readline compatible; 
				return NULL when the input line is empty */
  }
#endif

  return line;
}