1 /* $Header: /home/yav/catty/fkiss/RCS/sub.c,v 1.13 2000/09/02 03:26:22 yav Exp $
2  * fkiss subroutines
3  * written by yav <yav@bigfoot.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 char id_sub[] = "$Id: sub.c,v 1.13 2000/09/02 03:26:22 yav Exp $";
21 
22 #include <X11/Xos.h>
23 #include <X11/Xlib.h>
24 #include <stdio.h>
25 #include "config.h"
26 
27 #include "headers.h"
28 #include "fkiss.h"
29 #include "work.h"
30 #define PUBLIC_SUB_C
31 #include "extern.h"
32 
33 #ifdef HAVE_SIGNAL_H
34 #include <signal.h>
35 #endif
36 
ks_system2(str)37 int ks_system2(str)
38      char *str;
39 {
40   if (debug_mode)
41     fprintf(stderr, "system(%s)\n", str);
42   return system(str);
43 }
44 
kiss_exit(code)45 void kiss_exit(code)
46      int code;
47 {
48   char *buf;
49 
50   /* clean up extracted files */
51   if (extract_dir != NULL) {
52     buf = ks_malloc(strlen(extract_dir) + 16);
53     sprintf(buf, "rm -rf %s", extract_dir);
54     ks_system2(buf);
55     free(buf);
56   }
57   if (dsp != NULL)
58     XCloseDisplay(dsp);
59 
60   /* kill outstanding subprocesses ie: external audio players */
61   signal(SIGTERM, SIG_IGN);
62   killpg(0, SIGTERM);
63 
64   exit(code);
65 }
66 
ks_system(str)67 void ks_system(str)
68      char *str;
69 {
70   if (ks_system2(str))
71     msg("E cannot exec ``%s''\n", str);
72 }
73 
memory_error()74 void memory_error()
75 {
76   msg("E More core!\n");
77   /* NOT REACHED */
78 }
79 
ks_malloc(n)80 char *ks_malloc(n)
81      int n;
82 {
83   char *p;
84 
85   p = malloc(n);
86   if (p == NULL)
87     memory_error();
88   return p;
89 }
90 
ks_realloc(p,n)91 char *ks_realloc(p, n)
92      char *p;
93      int n;
94 {
95   p = realloc(p, n);
96   if (p == NULL)
97     memory_error();
98   return p;
99 }
100 
ks_strdup(str)101 char *ks_strdup(str)
102      char *str;
103 {
104   char *p;
105 
106 #if HAVE_STRDUP
107   p = strdup(str);
108   if (p == NULL)
109     memory_error();
110 #else
111   int len;
112 
113   len = strlen(str) + 1;	/* +1 for terminator '\0' */
114   p = ks_malloc(len);
115   bcopy(str, p, len);
116 #endif
117   return p;
118 }
119 
get_directory(path)120 char *get_directory(path)
121      char *path;
122 {
123   char *p;
124   char *buf;
125 
126   buf = ks_strdup(path);
127   p = rindex(buf, '/');
128   if (p == NULL)
129     p = buf;
130   *p = '\0';
131   return buf;
132 }
133 
get_filename(path)134 char *get_filename(path)
135      char *path;
136 {
137   char *p;
138 
139   p = rindex(path, '/');
140   return (p != NULL) ? p + 1 : path;
141 }
142 
143 /* for ASCII character set only */
strlower(str)144 void strlower(str)
145      char *str;
146 {
147   int c;
148 
149   while ((c = *str) != '\0')
150     *str++ = to_lower(c);
151 }
152 
dos_filename(src)153 char *dos_filename(src)
154      char *src;
155 {
156   char *p;
157   char *dst;
158 
159   src = get_filename(src);
160   /* cut DOS 8+3 style file name */
161   dst = ks_strdup(src);
162   p = rindex(dst, '.');
163   if (p == NULL) {
164     if (strlen(dst) > 8)
165       *(dst+8) = '\0';
166   } else {
167     if (strlen(p) > 4)
168       *(p+4) = '\0';
169     if (p - dst > 8)
170       strcpy(dst+8, p);
171   }
172   strlower(dst);
173   return dst;
174 }
175 
dos_pathname(src)176 char *dos_pathname(src)
177      char *src;
178 {
179   char *p, *p1;
180   char *dst;
181 
182   dst = ks_strdup(src);
183   p = get_filename(dst);
184   p1 = dos_filename(p);
185   strcpy(p, p1);
186   free(p1);
187   return dst;
188 }
189 
strcmp_dos_filename(dst,src)190 int strcmp_dos_filename(dst, src)
191      char *dst;
192      char *src;
193 {
194   int r;
195   char *buf1;
196   char *buf2;
197 
198   buf1 = dos_filename(dst);
199   buf2 = dos_filename(src);
200   r = strcmp(buf1, buf2);
201   free(buf1);
202   free(buf2);
203   return r;
204 }
205 
is_suffix(name,suffix)206 int is_suffix(name, suffix)
207      char *name;
208      char *suffix;
209 {
210   int i;
211   char *buf;
212 
213   i = strlen(name) - strlen(suffix);
214   if (i < 0)
215     return 0;
216   buf = ks_strdup(name+i);
217   strlower(buf);
218   i = (strcmp(buf, suffix) == 0);
219   free(buf);
220   return i;
221 }
222 
cut_crlf(p)223 void cut_crlf(p)
224      char *p;
225 {
226   int i;
227 
228   i = strlen(p);
229   if (i && *(p+i-1) == '\n') {
230     --i;
231     if (i && *(p+i-1) == '\r') /* MS-DOS CR */
232       --i;
233     *(p+i) = '\0';
234   }
235 }
236 
ks_srand(seed)237 void ks_srand(seed)
238      unsigned seed;
239 {
240   srand(seed);
241 }
242 
ks_rand()243 int ks_rand()
244 {
245   return rand();
246 }
247 
248 #define STREXTSIZE 64
lstr_init(p)249 void lstr_init(p)
250      LSTR *p;
251 {
252   p->len = STREXTSIZE;
253   p->buf = ks_malloc(p->len);
254   p->n = 0;
255   p->buf[0] = '\0';
256 }
257 
lstr_ch(p,c)258 void lstr_ch(p, c)
259      LSTR *p;
260      int c;
261 {
262   p->buf[p->n++] = c;
263   if (p->n >= p->len) {
264     p->len += STREXTSIZE;
265     p->buf = ks_realloc(p->buf, p->len);
266   }
267   p->buf[p->n] = '\0';
268 }
269 
lstr_bs(p)270 void lstr_bs(p)
271      LSTR *p;
272 {
273   if (p->n)
274     p->buf[--(p->n)] = '\0';
275 }
276 
lstr_cat(p,str)277 void lstr_cat(p, str)
278      LSTR *p;
279      char *str;			/* must be longer than or equal 1 character */
280 {
281   int c;
282 
283   while ((c = *str++) != '\0')
284     lstr_ch(p, c);
285 }
286 
287 /* End of file */
288