File: tools.c

package info (click to toggle)
irmp3 0.4.3pre6-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 584 kB
  • ctags: 374
  • sloc: ansic: 3,955; makefile: 212; pascal: 39
file content (94 lines) | stat: -rw-r--r-- 1,779 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
/*************************************************************************
 * $Id: tools.c,v 1.6 2001/02/18 07:15:19 dpotter Exp $
 *
 * tools.c -- Some helpful functions
 *
 * Copyright (C) by Andreas Neuhaus <andy@fasta.fh-dortmund.de>
 *
 */

#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>

/*************************************************************************
 * GLOBAL VARIABLES
 */

extern int terminate;

/*************************************************************************
 * READ ONE LINE FROM FD
 */
int readline (int fd, char *buf, int count)
{
	int rc, c;

	buf[0] = 0;
	c = 0;
	do {
		rc = read(fd, buf+c, 1);
		if (rc <= 0)
			return rc-1;
		// CR/LF at beginning of line is ignored.
		if (!c && (*buf == '\r' || *buf == '\n')) {
			break;
		}
		// skip CR and/or LF at end of line
		if (buf[c] == 0 || buf[c] == '\r' || buf[c] == '\n')
			break;
		c++;
	} while (!terminate && c < count);
	buf[c] = 0;

	return c;
}


/*************************************************************************
 * SEND TEXT TO FD
 */
int sendtext (int fd, char *text, ...)
{
	char buf[1024];
	va_list ap;

	va_start(ap, text);
	vsnprintf(buf, sizeof(buf)-1, text, ap);
	buf[sizeof(buf)-1] = 0;
	va_end(ap);

	return write(fd, buf, strlen(buf));
}


/*************************************************************************
 * TRIM BLANKS
 */
char *trimleft (char *str)
{
	char *s = str;
	while (*s==' ' || *s=='\t')
		s++;
	strcpy(str, s);
	return str;
}

char *trimright (char *str)
{
	char *s = str + strlen(str) - 1;
	while (s>=str && (*s==' ' || *s=='\t'))
		*s-- = 0;
	return str;
}

char *trim (char *str)
{
	return trimleft(trimright(str));
}


/*************************************************************************
 * EOF
 */