File: tools.c

package info (click to toggle)
xcdroast 0.98%2B0alpha9-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,084 kB
  • ctags: 1,001
  • sloc: ansic: 21,608; makefile: 119; sh: 116
file content (710 lines) | stat: -rw-r--r-- 13,210 bytes parent folder | download
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
	tools.c
	27.3.99 tn
*/

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <errno.h>
#include <ctype.h>
#include <pwd.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk_imlib.h>
#include "xcdrdata.h"
#include "xcdroast.h"
#include "main.h"


/* return 1 if path is a directory and 0 if not or invalid path */

gint is_directory(gchar *path) {
struct stat buf;

	if (stat(path,&buf) != 0) {
		return 0;
	}

	if (S_ISDIR(buf.st_mode) == 1) {
		return 1;
	} else {
		return 0;
	}
}


/* return 1 if path is a file or directory and 0 if not or invalid path */

gint is_file(gchar *path) {
struct stat buf;

	if (stat(path,&buf) != 0) {
		return 0;
	}
	return 1;
}


/* return the base directory of a given path, or NULL when invalid */
/* warning..overwrites original str */

gchar *get_basedir(gchar *dir) {
gchar *p;

	/* nothing to do if this is already a directory */
	if (is_directory(dir)) {
		return dir;
	}

	/* look for last slash */
	p = rindex(dir,'/');
	if (p) {
		*p = '\0';
		if (strcmp(dir,"") == 0) {
			strcpy(dir,"/");
		}
		return dir;
	}
	return NULL;
}


/* return 1 when path contains a subdir, else 0 */
/* skip . and ..-directories */

gint is_subdirs(gchar *path) {
struct dirent *ent;
DIR *dir;         
gchar tmp[MAXLINE];

	dir = opendir(path);
	
	/* invalid directory */
	if (dir == NULL) 
		return 0;

	while ( (ent = readdir(dir)) ) {
		strcpy(tmp,path);
		/* add slash when not there */
		if (tmp[strlen(tmp)-1] != '/')  
			strcat(tmp,"/");
		strcat(tmp,ent->d_name);

		if (strcmp(ent->d_name,".") == 0 || 
		    strcmp(ent->d_name,"..") == 0) 
			continue;

		if (is_directory(tmp)) {
			closedir(dir);
			return 1;
		}
	}
	closedir(dir);
	return 0;
}


/* return 1 when path contains files, else 0 */
/* skip . and ..-directories */

gint is_subfiles(gchar *path) {
struct dirent *ent;
DIR *dir;         
gchar tmp[MAXLINE];

	dir = opendir(path);
	
	/* invalid directory */
	if (dir == NULL) 
		return 0;

	while ( (ent = readdir(dir)) ) {
		strcpy(tmp,path);
		/* add slash when not there */
		if (tmp[strlen(tmp)-1] != '/')  
			strcat(tmp,"/");
		strcat(tmp,ent->d_name);

		if (strcmp(ent->d_name,".") == 0 || 
		    strcmp(ent->d_name,"..") == 0) 
			continue;

		closedir(dir);
		return 1;
	}
	closedir(dir);
	return 0;
}


/* strip a string of leading and trailing whitespace */

gchar *strip_string(gchar *str) {
gint i,j;
gint c1;

        if ( str == NULL) return (NULL);

        /* count how many leading chars to be whitespace */
        for(i=0; i<strlen(str); i++) {
                if (str[i] != ' ' && str[i] != '\t' && str[i] != '\r') 
                        break;
        }

        /* count how many trailing chars to be whitespace */
        for(j=strlen(str)-1; j >= 0; j--) {
                if (str[j] != ' ' && str[j] != '\t' && str[j] != '\n' && str[j] != '\r')
                        break;
        }

	/* string contains only whitespace? */
	if (j<i) {
		str[0] = '\0';
		return(str);
	}

        /* now move the chars to the front */
        for(c1=i; c1 <= j; c1++) {
                str[c1-i] = str[c1]; 
        }
        str[j+1-i] = '\0';      

        return(str);
}


/* parse escape-chars in string -> e.g. translate \n to newline */

gchar *escape_parse(gchar *str) {
gchar tmp[MAXLINE];
gchar c;
gint i,j;

        if ( str == NULL) return (NULL);

	j = 0;
        for(i=0; i<strlen(str); i++) {
		c = str[i];
		if (c == '\\') {
			i++;
			switch(str[i]) {

			case 'n':
				c = '\n';
				break;
			
			case 't':
				c = '\t';
				break;
	
			case 'b':
				c = '\b';
				break;

			default:
				c = str[i];
			}
		}	

		tmp[j]=c;
		j++;
        }

	tmp[j] = '\0';

	strcpy(str,tmp);
        return(str);
}


/* convert escape-chars in string -> e.g. newline to \n */

gchar *convert_escape(gchar *str) {
gchar tmp[MAXLINE*2];
gchar c,d;
gint i,j;

	if (str == NULL) return (NULL);

	j = 0;
	for (i = 0; i < strlen(str); i++) {
		c = str[i];
		d = '\0';

		switch (c) {
		case '\n':
			d = 'n';
			break;
		case '\t':
			d = 't';
			break;
		case '\b':
			d = 'b';
			break;
		case '\\':
			d = '\\';
			break;
		case '\"':
			d = '\"';
			break;
		}
		if (d != '\0') {
			/* generate escaped char */
			tmp[j] = '\\'; j++;
			tmp[j] = d; j++;
		} else {
			tmp[j] = c; j++;
		}
	}

	tmp[j] = '\0';

	/* check if new string is not getting to long */
	if (strlen(tmp) < MAXLINE) {
		strcpy(str,tmp);
	} else {
		/* over MAXLINE chars - should never happen */
		/* cut off string */
		strncpy(str,tmp,MAXLINE-1);
		str[MAXLINE-1] = '\0';
	}
	return(str);
}


/* check if there are illegal chars in our string and replace them with _ */
/* return 0 if all ok, and 1 if any chars changed/removed */

gint remove_illegal_chars(gchar *str) {
gint i,j;

	if (str == NULL) return 0;

	j = 0;
	for (i = 0; i < strlen(str); i++) {
		switch (str[i]) {
		case '/':
		case '\\':
		case '\b':
		case '\t':
			str[i] = '_';
			j = 1;
			break;
		}
	}

	return(j);
}


/* get a string of the form "'xxx' from 'xxx'" and extract the strings xxx
   to artist and title. remove any escape characters.
   Return 0 if all ok, 1 on problems */ 
 
gint decode_title_artist(gchar *str, gchar *title, gchar *artist) {
gchar tmp[MAXLINE];
gchar tmp2[MAXLINE];
gchar c,oldc;
gint i;

	if (str == NULL) {
		return 1;
	}

	oldc = '\0';

	/* check first char */
	if (str[0] != '\'') {
		/* not right - abort */
		return 1;
	}
	strcpy(tmp,str+1);

	/* look for next unescaped quote */
	for (i = 0; i < strlen(tmp); i++) {
		c = tmp[i];

		/* if current char ' and previous not \ we are done */
		if ((c == '\'') && (oldc != '\\')) {
			break;
		}
		oldc = c;
	}

	if (i == strlen(tmp)) {
		/* no quote found at all */
		return 1;
	}

	/* done extracting the title */
	strncpy(title,tmp,i);
	title[i] = '\0';
	/* now parse all other escaped chars */
	escape_parse(title);

	/* now the artist-field */
	strcpy(tmp2, tmp+i);
	if (strncmp(tmp2,"' from '",8) != 0) {
		/* look for middle string */
		/* not fitting - abort */ 
		return 1;
	}
	strcpy(tmp,tmp2+8);

	/* look for next unescaped quote */
	for (i = 0; i < strlen(tmp); i++) {
		c = tmp[i];

		/* if current char ' and previous not \ we are done */
		if ((c == '\'') && (oldc != '\\')) {
			break;
		}
		oldc = c;
	}

	if (i == strlen(tmp)) {
		/* no quote found at all */
		return 1;
	}

	/* done extracting the artist */
	strncpy(artist,tmp,i);
	artist[i] = '\0';
	/* now parse all other escaped chars */
	escape_parse(artist);

	return 0;
}


/*
 * Read a line from a descriptor.  Read the line one byte at a time,
 * looking for the newline. Works fine in nonblocking mode..here
 * we return when no more data can be read. 
 * We overwrite the newline with a null.
 * We return the number of characters up to, but not including,
 * the null (the same as strlen(3)).
 */
 
gint read_line(gint fd, gchar *ptr, gint maxlen) {
gint n, rc;
gchar c;
gchar *str;

	str = ptr;
 
        for (n = 1; n < maxlen; n++) {
                if ( (rc = read(fd, &c, 1)) == 1) {
                        *ptr++ = c;
                        if (c == '\n') {
                                break;
                        }

                } else if (rc == 0) {
			/* EOF */
                        if (n == 1)
                                return(0);      /* EOF, no data read */
                        else
                                break;          /* EOF, some data was read */
                } else if (rc == -2) {
			/* timeout while reading string */
			return(-2);
		} else {
			/* nonblocking mode an nothing to read? */
			if (rc == -1 && errno == EAGAIN) {
				if (n == 1) 
					return(-1);
				else
					break;
			}	
                        return(-1);     /* error */
		}
        }

	/* terminate the string */
	*ptr = 0;
 
        /* strip of some trailing chars - yes..we need both levels */
        ptr--;
        if ((*ptr == '\n') || (*ptr == '\r') ) {
                *ptr = 0;
        }
        ptr--;
        if ((*ptr == '\n') || (*ptr == '\r') ) {
                *ptr = 0;
        }
 
	if (strlen(str) == 0) {
		/* if we read an empty string, but are NOT on EOF return 1 */
		return 1;
	} else {
        	return(strlen(str));
	}
}


/* extract quotes-delimted string after first colon */
/* e.g.: 03: "bla" -> bla */

gint extract_quoted(gchar *str) {
gchar *p, *p2;
gchar tmp[MAXLINE];
gchar tmp2[MAXLINE];

	strcpy(tmp,str);

	/* get string after first colon */
	p = strtok(tmp,":");
	if (p == NULL)
		return 1;
	p = strtok(NULL,"");
	if (p == NULL) 
		return 1;

	strcpy(tmp,p);
	strip_string(tmp);

	/* now strip quotes */
	p = tmp;
	if (*p == '\"') {
		p2 = p+1;
	} else {
		p2 = p;
	}
	if (p[strlen(p)-1] == '\"') {
		p[strlen(p)-1] = '\0';
	}
	strcpy(tmp2,p2);
	escape_parse(tmp2);

	strcpy(str,tmp2);

	return 0;
}


/* read one char from a file descriptor with timeout */
/* return 1 if char read ok, -1 on read error, -2 on timeout */

gint get_char(gint fd, char *c) {
gint j;
struct timeval t;
fd_set set;

	FD_ZERO(&set);
	FD_SET(fd,&set);
	t.tv_sec = NETIOTIMEOUT;
	t.tv_usec = 0;

	j = select(fd+1, &set, NULL, NULL, &t);
	if (j > 0) {
		return(read(fd, c, 1));
	} else {	
		/* timeout triggered */
		return -2;
	}
}


/*
 * Read a line from a descriptor.  Read the line one byte at a time,
 * looking for the newline.  
 * We overwrite the newline with a null.
 * We return the number of characters up to, but not including,
 * the null (the same as strlen(3)).
 */
 
gint read_line2(gint fd, gchar *ptr, gint maxlen, gint timeout) {
gint n, rc;
gchar c;
 
        for (n = 1; n < maxlen; n++) {
		if (timeout) {
			/* use timeout */
			rc = get_char(fd,&c);
		} else {
			rc = read(fd, &c, 1);
		}	
		if ( rc == 1) {
                        *ptr++ = c;
                        if (c == '\n') {
                                break;
                        }
                } else if (rc == 0) {
                        if (n == 1)
                                return(0);      /* EOF, no data read */
                        else
                                break;          /* EOF, some data was read */
                } else if (rc == -2) {
			/* timeout while reading string */
			return(-2);
		} else {
                        return(-1);     /* error */
		}
        }
 
        /* strip of some trailing chars - yes..we need all three levels*/
        if ((*ptr == '\n') || (*ptr == '\r') ) {
                *ptr = 0;
        }
        ptr--;
        if ((*ptr == '\n') || (*ptr == '\r') ) {
                *ptr = 0;
        }
        ptr--;
        if ((*ptr == '\n') || (*ptr == '\r') ) {
                *ptr = 0;
        }
 
        return(n);
}

 
/*
 * Write "n" bytes to a descriptor.
 * Use in place of write() when fd is a stream socket.
 */
 
gint writen(gint fd, gchar *ptr, gint nbytes, gint newline) {
gint nleft, nwritten;
 
        nleft = nbytes;
        while (nleft > 0) {
                nwritten = write(fd, ptr, nleft);
                if (nwritten <= 0)
                        return(nwritten);               /* error */
 
                nleft -= nwritten;
                ptr   += nwritten;
        }
 
        /* add a newline when requested */
        if (newline) {
                write(fd,"\n",1);
        }
 
        return(nbytes - nleft);
}


/* replace ~/ in an entry field by the home-directory */

gchar *check_tilde(gchar *str) {
gchar tmp[MAXLINE];

	if (str == NULL) 
		return str;

	strip_string(str);

	/* to short, do nothing */
	if (strlen(str) < 2) 
		return str;

	/* ~/ found? */
	if (str[0] == '~' && str[1] == '/') {
		tmp[0] = '\0';
		if (g_get_home_dir()) {
			strcpy(tmp, g_get_home_dir());
		}
		strcat(tmp,"/");
		strcat(tmp,str+2);

		strcpy(str,tmp);
	}

	return str;
}


/* parse config line and return id and value */
/* return 0 if ok, 1 on error */

gint parse_config_line(gchar *iline, gchar *id, gchar *value) {
gchar *p,*p2;
gchar line[1024];
gchar tmp[1024];

	strcpy(line,iline); 
	strcpy(id,"");
        p = strtok(line,"=");
        if (p != NULL) {
		/* got id */
        	strcpy(id,p);
                strip_string(id);
        } else {
		return 1;
	}

	strcpy(tmp,"");
        p = strtok(NULL,"");
        if (p != NULL) {
		/* string after = */
        	strcpy(tmp,p);
                strip_string(tmp);
        } else { 
		return 1;
	}

        /* now strip quotes from string */
        p = tmp;
        if (*p == '\"') {
                p2 = p+1;
        } else {
                p2 = p;
        }
        if (p[strlen(p)-1] == '\"') {
                p[strlen(p)-1] = '\0';
        }
        strcpy(value,p2);

        /* now reconvert escape-chars */
        escape_parse(value);

	/* all ok */
	return 0;
}


/* check if we are root */

gint isroot() {

	if (getuid() == 0) {
		return 1;
	} else {
		return 0;
	}
}


/* check if user exists */

gint check_pw_user(gchar *name) {
struct passwd *ent;

	ent = getpwnam(name);
	if (ent) return 1;
	return 0;
}


/* return the owner (uid) of a given file */

gint get_file_owner(gchar *path) {
struct stat buf;

	if (stat(path,&buf) != 0) {
		return -1;
	}

	return (buf.st_uid);
}