This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
minor tweaks to release_managers_guide.pod
[perl5.git] / x2p / util.c
... / ...
CommitLineData
1/* util.c
2 *
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
4 * 2000, 2001, 2005 by Larry Wall and others
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 */
9
10#include "EXTERN.h"
11#include "a2p.h"
12#include "INTERN.h"
13#include "util.h"
14
15#include <stdarg.h>
16#define FLUSH
17
18static const char nomem[] = "Out of memory!\n";
19
20/* paranoid version of malloc */
21
22
23Malloc_t
24safemalloc(MEM_SIZE size)
25{
26 Malloc_t ptr;
27
28 /* malloc(0) is NASTY on some systems */
29 ptr = malloc(size ? size : 1);
30#ifdef DEBUGGING
31 if (debug & 128)
32 fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr,
33 an++,(long)size);
34#endif
35 if (ptr != NULL)
36 return ptr;
37 else {
38 fputs(nomem,stdout) FLUSH;
39 exit(1);
40 }
41 /*NOTREACHED*/
42 return 0;
43}
44
45/* paranoid version of realloc */
46
47Malloc_t
48saferealloc(Malloc_t where, MEM_SIZE size)
49{
50 Malloc_t ptr;
51
52 /* realloc(0) is NASTY on some systems */
53 ptr = realloc(where, size ? size : 1);
54#ifdef DEBUGGING
55 if (debug & 128) {
56 fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++);
57 fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size);
58 }
59#endif
60 if (ptr != NULL)
61 return ptr;
62 else {
63 fputs(nomem,stdout) FLUSH;
64 exit(1);
65 }
66 /*NOTREACHED*/
67 return 0;
68}
69
70/* safe version of free */
71
72Free_t
73safefree(Malloc_t where)
74{
75#ifdef DEBUGGING
76 if (debug & 128)
77 fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++);
78#endif
79 free(where);
80}
81
82/* copy a string up to some (non-backslashed) delimiter, if any */
83
84char *
85cpytill(register char *to, register char *from, register int delim)
86{
87 for (; *from; from++,to++) {
88 if (*from == '\\') {
89 if (from[1] == delim)
90 from++;
91 else if (from[1] == '\\')
92 *to++ = *from++;
93 }
94 else if (*from == delim)
95 break;
96 *to = *from;
97 }
98 *to = '\0';
99 return from;
100}
101
102
103char *
104cpy2(register char *to, register char *from, register int delim)
105{
106 for (; *from; from++,to++) {
107 if (*from == '\\')
108 *to++ = *from++;
109 else if (*from == '$')
110 *to++ = '\\';
111 else if (*from == delim)
112 break;
113 *to = *from;
114 }
115 *to = '\0';
116 return from;
117}
118
119/* return ptr to little string in big string, NULL if not found */
120
121char *
122instr(char *big, const char *little)
123{
124 register char *t, *x;
125 register const char *s;
126
127 for (t = big; *t; t++) {
128 for (x=t,s=little; *s; x++,s++) {
129 if (!*x)
130 return NULL;
131 if (*s != *x)
132 break;
133 }
134 if (!*s)
135 return t;
136 }
137 return NULL;
138}
139
140/* copy a string to a safe spot */
141
142char *
143savestr(const char *str)
144{
145 register char * const newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1));
146
147 (void)strcpy(newaddr,str);
148 return newaddr;
149}
150
151/* grow a static string to at least a certain length */
152
153void
154growstr(char **strptr, int *curlen, int newlen)
155{
156 if (newlen > *curlen) { /* need more room? */
157 if (*curlen)
158 *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen);
159 else
160 *strptr = (char *) safemalloc((MEM_SIZE)newlen);
161 *curlen = newlen;
162 }
163}
164
165void
166fatal(const char *pat,...)
167{
168#if defined(HAS_VPRINTF)
169 va_list args;
170
171 va_start(args, pat);
172 vfprintf(stderr,pat,args);
173 va_end(args);
174#else
175 fprintf(stderr,pat,a1,a2,a3,a4);
176#endif
177 exit(1);
178}
179
180#if defined(DARWIN)
181__private_extern__ /* warn() conflicts with libc */
182#endif
183void
184warn(const char *pat,...)
185{
186#if defined(HAS_VPRINTF)
187 va_list args;
188
189 va_start(args, pat);
190 vfprintf(stderr,pat,args);
191 va_end(args);
192#else
193 fprintf(stderr,pat,a1,a2,a3,a4);
194#endif
195}
196