File: sub.c

package info (click to toggle)
fkiss 0.33a.patch-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 804 kB
  • ctags: 1,074
  • sloc: ansic: 9,141; sh: 2,924; makefile: 98; sed: 14
file content (287 lines) | stat: -rw-r--r-- 4,770 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* $Header: /home/yav/catty/fkiss/RCS/sub.c,v 1.13 2000/09/02 03:26:22 yav Exp $
 * fkiss subroutines
 * written by yav <yav@bigfoot.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

char id_sub[] = "$Id: sub.c,v 1.13 2000/09/02 03:26:22 yav Exp $";

#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include "config.h"

#include "headers.h"
#include "fkiss.h"
#include "work.h"
#define PUBLIC_SUB_C
#include "extern.h"

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

int ks_system2(str)
     char *str;
{
  if (debug_mode)
    fprintf(stderr, "system(%s)\n", str);
  return system(str);
}

void kiss_exit(code)
     int code;
{
  char *buf;
  
  /* clean up extracted files */
  if (extract_dir != NULL) {
    buf = ks_malloc(strlen(extract_dir) + 16);
    sprintf(buf, "rm -rf %s", extract_dir);
    ks_system2(buf);
    free(buf);
  }
  if (dsp != NULL)
    XCloseDisplay(dsp);
    
  /* kill outstanding subprocesses ie: external audio players */
  signal(SIGTERM, SIG_IGN);
  killpg(0, SIGTERM);
  
  exit(code);
}

void ks_system(str)
     char *str;
{
  if (ks_system2(str))
    msg("E cannot exec ``%s''\n", str);
}

void memory_error()
{
  msg("E More core!\n");
  /* NOT REACHED */
}

char *ks_malloc(n)
     int n;
{
  char *p;
  
  p = malloc(n);
  if (p == NULL)
    memory_error();
  return p;
}

char *ks_realloc(p, n)
     char *p;
     int n;
{
  p = realloc(p, n);
  if (p == NULL)
    memory_error();
  return p;
}

char *ks_strdup(str)
     char *str;
{
  char *p;
  
#if HAVE_STRDUP
  p = strdup(str);
  if (p == NULL)
    memory_error();
#else
  int len;
  
  len = strlen(str) + 1;	/* +1 for terminator '\0' */
  p = ks_malloc(len);
  bcopy(str, p, len);
#endif
  return p;
}

char *get_directory(path)
     char *path;
{
  char *p;
  char *buf;
  
  buf = ks_strdup(path);
  p = rindex(buf, '/');
  if (p == NULL)
    p = buf;
  *p = '\0';
  return buf;
}

char *get_filename(path)
     char *path;
{
  char *p;
  
  p = rindex(path, '/');
  return (p != NULL) ? p + 1 : path;
}

/* for ASCII character set only */
void strlower(str)
     char *str;
{
  int c;
  
  while ((c = *str) != '\0')
    *str++ = to_lower(c);
}

char *dos_filename(src)
     char *src;
{
  char *p;
  char *dst;

  src = get_filename(src);
  /* cut DOS 8+3 style file name */
  dst = ks_strdup(src);
  p = rindex(dst, '.');
  if (p == NULL) {
    if (strlen(dst) > 8)
      *(dst+8) = '\0';
  } else {
    if (strlen(p) > 4)
      *(p+4) = '\0';
    if (p - dst > 8)
      strcpy(dst+8, p);
  }
  strlower(dst);
  return dst;
}

char *dos_pathname(src)
     char *src;
{
  char *p, *p1;
  char *dst;

  dst = ks_strdup(src);
  p = get_filename(dst);
  p1 = dos_filename(p);
  strcpy(p, p1);
  free(p1);
  return dst;
}

int strcmp_dos_filename(dst, src)
     char *dst;
     char *src;
{
  int r;
  char *buf1;
  char *buf2;
  
  buf1 = dos_filename(dst);
  buf2 = dos_filename(src);
  r = strcmp(buf1, buf2);
  free(buf1);
  free(buf2);
  return r;
}

int is_suffix(name, suffix)
     char *name;
     char *suffix;
{
  int i;
  char *buf;
  
  i = strlen(name) - strlen(suffix);
  if (i < 0)
    return 0;
  buf = ks_strdup(name+i);
  strlower(buf);
  i = (strcmp(buf, suffix) == 0);
  free(buf);
  return i;
}

void cut_crlf(p)
     char *p;
{
  int i;
  
  i = strlen(p);
  if (i && *(p+i-1) == '\n') {
    --i;
    if (i && *(p+i-1) == '\r') /* MS-DOS CR */
      --i;
    *(p+i) = '\0';
  }
}

void ks_srand(seed)
     unsigned seed;
{
  srand(seed);
}

int ks_rand()
{
  return rand();
}

#define STREXTSIZE 64
void lstr_init(p)
     LSTR *p;
{
  p->len = STREXTSIZE;
  p->buf = ks_malloc(p->len);
  p->n = 0;
  p->buf[0] = '\0';
}

void lstr_ch(p, c)
     LSTR *p;
     int c;
{
  p->buf[p->n++] = c;
  if (p->n >= p->len) {
    p->len += STREXTSIZE;
    p->buf = ks_realloc(p->buf, p->len);
  }
  p->buf[p->n] = '\0';
}

void lstr_bs(p)
     LSTR *p;
{
  if (p->n)
    p->buf[--(p->n)] = '\0';
}

void lstr_cat(p, str)
     LSTR *p;
     char *str;			/* must be longer than or equal 1 character */
{
  int c;
  
  while ((c = *str++) != '\0')
    lstr_ch(p, c);
}

/* End of file */