File: ut.c

package info (click to toggle)
deco 3.9-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 592 kB
  • ctags: 884
  • sloc: ansic: 9,971; sh: 152; makefile: 141
file content (240 lines) | stat: -rw-r--r-- 4,132 bytes parent folder | download | duplicates (2)
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
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#if HAVE_UNISTD_H
#   include <unistd.h>
#endif
#if HAVE_FCNTL_H
#   include <fcntl.h>
#endif
#include "deco.h"

#define SYSTEMID "/etc/systemid"

#define DELIM ','

#define STOREINT(p,nn) {\
	register char *cp = p;\
	register n = nn;\
	*cp++ = n/10%10 + '0';\
	*cp = n%10 + '0';\
	}

static char pattern [] = "00-Mon-1900 00:00:00";

char *timestr (long tim)
{
	struct tm *t = localtime (&tim);
	register char *p = pattern;

	STOREINT (p, t->tm_mday);
	if (*p == '0')
		*p = ' ';


	strncpy (p+3, "JanFebMarAprMayJunJulAugSepOctNovDec"+t->tm_mon*3, 3);
	STOREINT (p+9, t->tm_year);
	if (t->tm_year < 100) {
		p[7] = '1';
		p[8] = '9';
	} else {
		p[7] = '2';
		p[8] = '0';
	}
	STOREINT (p+12, t->tm_hour);
	STOREINT (p+15, t->tm_min);
	STOREINT (p+18, t->tm_sec);
	return (p);
}

char *getmachine ()
{
	static char buf [64];
	register f;
	register char *p;

#if HAVE_GETHOSTNAME
	if (gethostname (buf, sizeof (buf) - 1) >= 0)
		return (buf);
#endif
	if ((f = open (SYSTEMID, 0)) >= 0) {
		read (f, buf, sizeof (buf) - 1);
		close (f);
		for (p=buf; *p; ++p)
			if (*p<' ' || *p>'~') {
				*p = 0;
				break;
			}
		return (buf);
	}
	return (0);
}

const char *extension (const char *p)
{
	const char *s;

	if (p[0]=='.' && p[1]=='.' && p[2]==0)
		return (p+2);
	s = 0;
	while (*++p)
		if (*p == '.')
			s = p;
	return (s ? s : p);
}

char *ltoac (long l)
{
	char num [15];
	static char buf [15];
	register char *p, *q;
	register n;

	sprintf (num, "%ld", l);
	for (p=num; *p; ++p);           /* find end of num */
	q = buf + sizeof (buf) - 1;     /* set q to the end of buf */
	*q-- = 0;
	for (n=0; p>num; ++n) {
		if (n && n%3 == 0)
			*q-- = DELIM;
		*q-- = *--p;
	}
	return (q+1);
}

char *basename (char *p)
{
	register char *s, *e;
	static char buf [32];

	/* convert "zzz/name" to "name" */
	for (s=p; *s; ++s)
		if (*s == '/')
			p = s+1;
	if (! *p)
		return ("");

	/* convert "name.ext" to "name" */
	for (e=s, s=p+1; *s; ++s)
		if (*s == '.')
			e = s;
	strncpy (buf, p, e-p);
	buf [e-p] = 0;
	return (buf);
}

char *strtail (char *p, int delim, int maxlen)
{
	register char *s;

	/* return tail of string */
	for (s=p; *s; ++s);
	s -= maxlen;
	if (s <= p)
		return (p);
	for (; *s; ++s)
		if (*s == delim)
			return (s+1);
	return (s - maxlen);
}

int strbcmp (char *s, char *b)
{
	/* compare string s with string b */
	/* return 1 if s == b or s == b+' '+x */

	while (*b && *b == *s)
		++b, ++s;
	if (*b || (*s && *s != ' '))
		return (0);
	return (1);
}

void outerr (char *s, ...)
{
	char buf [100];
	register char *p;
	va_list ap;

	va_start (ap, s);
	vsprintf (buf, s, ap);
	va_end (ap);
	for (p=buf; *p; ++p)
		if (*p == '\033')
			*p = '[';
	write (2, buf, (unsigned) (p - buf));
}

/*
 * int match (char *name, char *pattern)
 *
 * Match name with pattern. Returns 1 on success.
 * Pattern may contain wild symbols:
 *
 * ^       - at beginning of string - any string not matched
 * *       - any string
 * ?       - any symbol
 * [a-z]   - symbol in range
 * [^a-z]  - symbol out of range
 * [!a-z]  - symbol out of range
 */
int match (unsigned char *name, unsigned char *pat)
{
	int ok, negate_range, matched;

	if (*pat == '^') {
		matched = 0;
		++pat;
	} else
		matched = 1;
	for (;;) {
		switch (*pat++) {
		case '*':
			if (! *pat)
				return (matched);
			for (; *name; ++name)
				if (match (name, pat))
					return (matched);
			return (! matched);
		case '?':
			if (! *name++)
				return (! matched);
			break;
		case 0:
			if (*name)
				return (! matched);
			return (matched);
		default:
			if (*name++ != pat[-1])
				return (! matched);
			break;
		case '[':
			ok = 0;
			negate_range = (*pat == '^' || *pat == '!');
			if (negate_range)
				++pat;
			while (*pat++ != ']')
				if (*pat == '-') {
					if (pat[-1] <= *name && *name <= pat[1])
						ok = 1;
					pat += 2;
				} else if (pat[-1] == *name)
					ok = 1;
			if (ok == negate_range)
				return (! matched);
			++name;
			break;
		}
	}
}

char *mdup (char *s)
{
	char *p;

	p = malloc (strlen (s) + 1);
	strcpy (p, s);
	return (p);
}