1 
2 /*
3  *  Diverse Bristol audio routines.
4  *  Copyright (c) by Nick Copeland <nickycopeland@hotmail.com> 1996,2012
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 3 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <strings.h>
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 
31 void *
brightonmalloc(size_t size)32 brightonmalloc(size_t size)
33 {
34 	void *mem;
35 
36 	/*
37 	 * Man, this is an ugly workaround. Somewhere in the app I am writing
38 	 * past the end of a memory block, leading to heap corruption and it is
39 	 * naturally causing segfaults in malloc(). This works around the issue
40 	 * until I can track it down.
41 	 *
42 	 * We could use calloc here and let -O3 remove the whole shim.
43 	 */
44 	if ((mem = malloc(size)) == NULL)
45 	{
46 		printf("NULL malloc, exiting\n");
47 		exit(-10);
48 	}
49 	bzero(mem, size);
50 
51 	return(mem);
52 }
53 
54 void
brightonfree(void * mem)55 brightonfree(void *mem)
56 {
57 	if (mem)
58 		free(mem);
59 }
60 
61 FILE *
brightonfopen(char * filename,char * permissions)62 brightonfopen(char *filename, char *permissions)
63 {
64 	return(fopen(filename, permissions));
65 }
66 
67 char *
brightonfgets(char * line,int size,FILE * fd)68 brightonfgets(char *line, int size, FILE *fd)
69 {
70 	return(fgets(line, size, fd));
71 }
72 
73 int
brightonfclose(FILE * fd)74 brightonfclose(FILE *fd)
75 {
76 	/*
77 	 * This may or may not cause a fault at some point.
78 	 */
79 	return(fclose(fd));
80 }
81 
82 #ifdef nothing
83 /*
84  * This was really just to debug the heap corruption.
85  */
86 FILE *
brightonfopen(char * filename,char * permissions)87 brightonfopen(char *filename, char *permissions)
88 {
89 	/*
90 	 * We only use fopen for reading work, it was easy but gave problems
91 	 */
92 	return((FILE *) open(filename, O_RDONLY));
93 }
94 
95 char *
brightonfgets(char * line,int size,FILE * FD)96 brightonfgets(char *line, int size, FILE *FD)
97 {
98 	int fd = (int) FD, count = 0;
99 
100 	/*
101 	 * This is not going to be the fastest code, we can work on buffer
102 	 * optimisation later.
103 	 */
104 	while (count < size) {
105 		if (read(fd, &line[count], 1) < 0)
106 		{
107 			if (count == 0)
108 				return(NULL);
109 			line[count] = '\0';
110 			return(line);
111 		}
112 
113 		if ((line[count] == '\n')
114 			|| (line[count] == '\r'))
115 		{
116 			line[count] = '\0';
117 			return(line);
118 		}
119 
120 		count++;
121 	}
122 
123 	return(line);
124 }
125 
126 int
brightonfclose(FILE * fd)127 brightonfclose(FILE *fd)
128 {
129 	/*
130 	 * This may or may not cause a fault at some point.
131 	 */
132 	return(close((int) fd));
133 }
134 #endif
135