1 /* @(#)fjmem.c	1.14 15/05/10 Copyright 1998-2015 J. Schilling */
2 #include <schily/mconfig.h>
3 #ifndef lint
4 static	UConst char sccsid[] =
5 	"@(#)fjmem.c	1.14 15/05/10 Copyright 1998-2015 J. Schilling";
6 #endif
7 /*
8  *	Memory handling with error checking
9  *
10  *	Copyright (c) 1998-2015 J. Schilling
11  */
12 /*
13  * The contents of this file are subject to the terms of the
14  * Common Development and Distribution License, Version 1.0 only
15  * (the "License").  You may not use this file except in compliance
16  * with the License.
17  *
18  * See the file CDDL.Schily.txt in this distribution for details.
19  * A copy of the CDDL is also available via the Internet at
20  * http://www.opensource.org/licenses/cddl1.txt
21  *
22  * When distributing Covered Code, include this CDDL HEADER in each
23  * file and include the License file CDDL.Schily.txt from this distribution.
24  */
25 
26 #include <schily/stdio.h>
27 #include <schily/stdlib.h>
28 #include <schily/unistd.h>
29 #include <schily/string.h>
30 #include <schily/standard.h>
31 #include <schily/jmpdefs.h>
32 #include <schily/schily.h>
33 #include <schily/nlsdefs.h>
34 
35 EXPORT	int	__fjmexval	__PR((int exval));
36 EXPORT	void	*__fjmalloc	__PR((FILE *f, size_t size, char *msg, sigjmps_t *jmp));
37 EXPORT	void	*__fjrealloc	__PR((FILE *f, void *ptr, size_t size, char *msg, sigjmps_t *jmp));
38 EXPORT	char	*__fjsavestr	__PR((FILE *f, const char *s, sigjmps_t *jmp));
39 
40 LOCAL	int	fjmexval;
41 
42 EXPORT	int
__fjmexval(exval)43 __fjmexval(exval)
44 	int	exval;
45 {
46 	int	ret = fjmexval;
47 
48 	fjmexval = exval;
49 
50 	return (ret);
51 }
52 
53 EXPORT void *
__fjmalloc(f,size,msg,jmp)54 __fjmalloc(f, size, msg, jmp)
55 	FILE		*f;
56 	size_t		size;
57 	char		*msg;
58 	sigjmps_t	*jmp;
59 {
60 	void	*ret;
61 
62 	ret = malloc(size);
63 	if (ret == NULL) {
64 		int	err = geterrno();
65 
66 		if (f != NULL) {
67 			ferrmsg(f,
68 				gettext("Cannot allocate memory for %s.\n"),
69 									msg);
70 		}
71 		if (fjmexval)
72 			err = fjmexval;
73 		if (jmp == JM_EXIT)
74 			comexit(err);
75 		if (jmp != NULL)
76 			siglongjmp(jmp->jb, 1);
77 	}
78 	return (ret);
79 }
80 
81 EXPORT void *
__fjrealloc(f,ptr,size,msg,jmp)82 __fjrealloc(f, ptr, size, msg, jmp)
83 	FILE		*f;
84 	void		*ptr;
85 	size_t		size;
86 	char		*msg;
87 	sigjmps_t	*jmp;
88 {
89 	void	*ret;
90 
91 	if (ptr == NULL)
92 		ret = malloc(size);
93 	else
94 		ret = realloc(ptr, size);
95 	if (ret == NULL) {
96 		int	err = geterrno();
97 
98 		if (f != NULL) {
99 			ferrmsg(f,
100 				gettext("Cannot realloc memory for %s.\n"),
101 									msg);
102 		}
103 		if (fjmexval)
104 			err = fjmexval;
105 		if (jmp == JM_EXIT)
106 			comexit(err);
107 		if (jmp != NULL)
108 			siglongjmp(jmp->jb, 1);
109 	}
110 	return (ret);
111 }
112 
113 EXPORT char *
__fjsavestr(f,s,jmp)114 __fjsavestr(f, s, jmp)
115 	FILE		*f;
116 	const char	*s;
117 	sigjmps_t		*jmp;
118 {
119 	char	*ret = __fjmalloc(f, strlen(s)+1, "saved string", jmp);
120 
121 	if (ret != NULL)
122 		strcpy(ret, s);
123 	return (ret);
124 }
125