1 /*
2  *   util.h - Common utility routines for xmcd, cda and libdi.
3  *
4  *   xmcd  - Motif(R) CD Audio Player/Ripper
5  *   cda   - Command-line CD Audio Player/Ripper
6  *   libdi - CD Audio Device Interface Library
7  *
8  *
9  *   Copyright (C) 1993-2004  Ti Kan
10  *   E-mail: xmcd@amb.org
11  *
12  *   This program is free software; you can redistribute it and/or modify
13  *   it under the terms of the GNU General Public License as published by
14  *   the Free Software Foundation; either version 2 of the License, or
15  *   (at your option) any later version.
16  *
17  *   This program is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with this program; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  */
27 #ifndef __UTIL_H__
28 #define __UTIL_H__
29 
30 #ifndef lint
31 static char *_util_h_ident_ = "@(#)util.h	6.71 04/03/17";
32 #endif
33 
34 /*
35  * Default platform feature configurations
36  */
37 
38 #if !defined(USE_SIGACTION) && !defined(USE_SIGSET) && !defined(USE_SIGNAL)
39 #define USE_SIGNAL      /* Make USE_SIGNAL the default is not specified */
40 #endif
41 
42 /* Avoid multiple defines */
43 #if defined(USE_SIGACTION) && defined(USE_SIGSET)
44 #undef USE_SIGSET
45 #endif
46 
47 /* Avoid multiple defines */
48 #if defined(USE_SIGPROCMASK) && defined(USE_SIGBLOCK)
49 #undef USE_SIGBLOCK
50 #endif
51 
52 #if defined(USE_SETEUID) || defined(USE_SETREUID) || defined(USE_SETRESUID)
53 #define HAS_EUID	/* Signify effective uid is supported */
54 
55 /* Avoid multiple defines */
56 #if defined(USE_SETEUID) && defined(USE_SETREUID)
57 #undef USE_SETREUID
58 #endif
59 #if defined(USE_SETEUID) && defined(USE_SETRESUID)
60 #undef USE_SETRESUID
61 #endif
62 #if defined(USE_SETREUID) && defined(USE_SETRESUID)
63 #undef USE_SETRESUID
64 #endif
65 
66 #endif
67 
68 /* Avoid multiple defines */
69 #ifdef HAS_EUID
70 
71 #if defined(USE_SETEGID) && defined(USE_SETREGID)
72 #undef USE_SETREGID
73 #endif
74 #if defined(USE_SETEGID) && defined(USE_SETRESGID)
75 #undef USE_SETRESGID
76 #endif
77 #if defined(USE_SETREGID) && defined(USE_SETRESGID)
78 #undef USE_SETRESGID
79 #endif
80 
81 #else
82 
83 #ifdef USE_SETEGID
84 #undef USE_SETEGID
85 #endif
86 #ifdef USE_SETREGID
87 #undef USE_SETREGID
88 #endif
89 #ifdef USE_SETRESGID
90 #undef USE_SETRESGID
91 #endif
92 
93 #endif
94 
95 #if !defined(USE_SELECT) && !defined(USE_POLL) && \
96     !defined(USE_NAP) && !defined(USE_USLEEP) && \
97     !defined(USE_NANOSLEEP) && !defined(USE_PTHREAD_DELAY_NP)
98 #define USE_SELECT      /* Make USE_SELECT the default if not specified */
99 #endif
100 
101 
102 /*
103  * Memory allocators
104  */
105 #define _MEM_ALLOC		malloc
106 #define _MEM_REALLOC		realloc
107 #define _MEM_CALLOC		calloc
108 #define _MEM_FREE		free
109 
110 #ifdef MEM_DEBUG
111 #define MEM_ALLOC(s,l)		util_dbg_malloc((s),(l))
112 #define MEM_REALLOC(s,p,l)	util_dbg_realloc((s),(p),(l))
113 #define MEM_CALLOC(s,n,l)	util_dbg_calloc((s),(n),(l))
114 #define MEM_FREE(p)		util_dbg_free((p))
115 #else
116 #define MEM_ALLOC(s,l)		_MEM_ALLOC((l))
117 #define MEM_REALLOC(s,p,l)	_MEM_REALLOC((p),(l))
118 #define MEM_CALLOC(s,n,l)	_MEM_CALLOC((n),(l))
119 #define MEM_FREE(p)		_MEM_FREE((p))
120 #endif
121 
122 
123 /*
124  * Directory path name convention
125  */
126 #ifdef __VMS
127 #define DIR_BEG		'['		/* Directory start */
128 #define DIR_SEP		'.'		/* Directory separator */
129 #define DIR_END		']'		/* Directory end */
130 #define CUR_DIR		"[]"		/* Current directory */
131 #else
132 #define DIR_BEG		'/'		/* Directory start */
133 #define DIR_SEP		'/'		/* Directory separator */
134 #define DIR_END		'/'		/* Directory end */
135 #define CUR_DIR		"."		/* Current directory */
136 #endif
137 
138 /*
139  * Utility macros
140  */
141 #define SQR(x)		((x) * (x))	/* Compute the square of a number */
142 
143 /*
144  * Debugging macros
145  */
146 #define DBG_GEN		0x01		/* General debugging */
147 #define DBG_DEVIO	0x02		/* Device I/O debugging */
148 #define DBG_CDI		0x04		/* CD information load debugging */
149 #define DBG_UI		0x08		/* User interface debugging */
150 #define DBG_RMT		0x10		/* Remote control debugging */
151 #define DBG_SND		0x20		/* Sound card debugging */
152 #define DBG_MOTD	0x40		/* Message of the day debugging */
153 #define DBG_ALL		0xff		/* All debug levels */
154 
155 #define DBGPRN(x)	if (app_data.debug & (x)) (void) fprintf
156 #define DBGDUMP(x)	if (app_data.debug & (x)) util_dbgdump
157 
158 #define EXE_ERR		0xdeadbeef	/* Magic retcode for util_runcmd() */
159 
160 /*
161  * Return bitmap of util_urlchk
162  */
163 #define IS_LOCAL_URL		0x01
164 #define IS_REMOTE_URL		0x02
165 #define NEED_PREPEND_HTTP	0x10
166 #define NEED_PREPEND_FTP	0x20
167 #define NEED_PREPEND_MAILTO	0x40
168 
169 /*
170  * Used for the dir parameter of util_vms_urlconv
171  */
172 #define UNIX_2_VMS	1
173 #define VMS_2_UNIX	2
174 
175 /*
176  * Used by util_chset_open, util_chset_close and util_chset_conv
177  */
178 #define CHSET_UTF8_TO_LOCALE	1	/* Convert from UTF-8 to locale */
179 #define CHSET_LOCALE_TO_UTF8	2	/* Convert from locale to UTF-8 */
180 
181 
182 typedef struct chset_conv {
183 	int		flags;
184 	void		*lconv_desc;
185 } chset_conv_t;
186 
187 
188 /* Public function prototypes */
189 extern void		util_init(void);
190 extern void		util_start(void);
191 extern void		util_onsig(int);
192 extern void		(*util_signal(int , void (*)(int)))(int);
193 extern int		util_seteuid(uid_t);
194 extern int		util_setegid(gid_t);
195 extern bool_t		util_newstr(char **, char *);
196 extern uid_t		util_get_ouid(void);
197 extern gid_t		util_get_ogid(void);
198 extern bool_t		util_set_ougid(void);
199 extern struct utsname	*util_get_uname(void);
200 extern sword32_t	util_ltobcd(sword32_t);
201 extern sword32_t	util_bcdtol(sword32_t);
202 extern bool_t		util_stob(char *);
203 extern char		*util_basename(char *);
204 extern char		*util_dirname(char *);
205 extern char		*util_loginname(void);
206 extern char		*util_homedir(uid_t);
207 extern char		*util_uhomedir(char *);
208 extern int		util_dirstat(char *, struct stat *, bool_t);
209 extern bool_t		util_mkdir(char *, mode_t);
210 extern void		util_setperm(char *, char *);
211 extern char		*util_monname(int);
212 extern bool_t		util_isexecutable(char *);
213 extern char		*util_findcmd(char *);
214 extern bool_t		util_checkcmd(char *);
215 extern int		util_runcmd(char *, void (*)(int), int);
216 extern int		util_isqrt(int);
217 extern void		util_blktomsf(sword32_t, byte_t *, byte_t *, byte_t *,
218 				      sword32_t);
219 extern void		util_msftoblk(byte_t, byte_t, byte_t, sword32_t *,
220 				      sword32_t);
221 extern sword32_t	util_xlate_blk(sword32_t);
222 extern sword32_t	util_unxlate_blk(sword32_t);
223 extern int		util_taper_vol(int);
224 extern int		util_untaper_vol(int);
225 extern int		util_scale_vol(int);
226 extern int		util_unscale_vol(int);
227 extern void		util_delayms(unsigned long);
228 extern int		util_waitchild(pid_t, time_t, void (*)(int), int,
229 				       bool_t, waitret_t *);
230 extern char		*util_strstr(char *s1, char *s2);
231 extern int		util_strcasecmp(char *, char *);
232 extern int		util_strncasecmp(char *, char *, int);
233 extern char		*util_text_reduce(char *);
234 extern char		*util_cgi_xlate(char *);
235 extern int		util_urlchk(char *, char **, int *);
236 extern char		*util_urlencode(char *);
237 extern void		util_html_fputs(char *, FILE *, bool_t, char *, int);
238 extern chset_conv_t	*util_chset_open(int);
239 extern void		util_chset_close(chset_conv_t *);
240 extern bool_t		util_chset_conv(chset_conv_t *, char *, char **,
241 					bool_t);
242 extern word16_t		util_bswap16(word16_t);
243 extern word32_t		util_bswap24(word32_t);
244 extern word32_t		util_bswap32(word32_t);
245 extern word64_t		util_bswap64(word64_t);
246 extern word16_t		util_lswap16(word16_t);
247 extern word32_t		util_lswap24(word32_t);
248 extern word32_t		util_lswap32(word32_t);
249 extern word64_t		util_lswap64(word64_t);
250 extern void		util_dbgdump(char *, byte_t *, int);
251 
252 #ifdef FAKE_64BIT
253 extern word64_t		util_assign64(sword32_t);
254 extern word32_t		util_assign32(word64_t);
255 #endif
256 
257 #ifdef __VMS
258 extern DIR		*util_opendir(char *);
259 extern void		util_closedir(DIR *);
260 extern struct dirent	*util_readdir(DIR *);
261 extern pid_t		util_waitpid(pid_t, int *, int);
262 extern int		util_link(char *, char *);
263 extern int		util_unlink(char *);
264 extern char		*util_vms_urlconv(char *, int);
265 #endif
266 
267 #ifdef MEM_DEBUG
268 void			*util_dbg_malloc(char *name, size_t size);
269 void			*util_dbg_realloc(char *name, void *ptr, size_t size);
270 void			*util_dbg_calloc(char *name, size_t nelem,
271 					 size_t elsize);
272 void			util_dbg_free(void *ptr);
273 #endif
274 
275 #endif	/* __UTIL_H__ */
276 
277 
278