[go: up one dir, main page]

File: compat.c

package info (click to toggle)
tirc 1.2-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,316 kB
  • ctags: 1,906
  • sloc: ansic: 19,264; sh: 1,594; makefile: 209; awk: 14
file content (160 lines) | stat: -rw-r--r-- 2,379 bytes parent folder | download | duplicates (4)
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
/*-
 * tirc -- client for the Internet Relay Chat
 *
 *	Copyright (c) 1996, 1997, 1998
 *		Matthias Buelow.  All rights reserved.
 *
 *	See the file ``COPYRIGHT'' for the usage and distribution
 *	license and warranty disclaimer.
 */

#ifndef lint
static char rcsid[] = "$Id: compat.c,v 1.17 1998/02/02 03:41:29 mkb Exp $";
#endif

#include "tirc.h"

#ifdef	HAVE_STRING_H
#include <string.h>
#elif defined(HAVE_MEMORY_H)
#include <memory.h>
#endif
#ifdef	HAVE_MALLOC_H
#include <malloc.h>
#endif

#include "compat.h"

#ifndef	__STDC__
#define	MAXFUNC	16	/* maximum number of functions to be registered */
void	(*funcs[MAXFUNC]) __P((void));
int	funcreg = 0;
#endif

/* Save a copy of a string */
char *
Strdup(str)
	const char *str;
{
#ifdef	HAVE_STRDUP
	return strdup(str);
#else
	char *s;
	int l;

	l = strlen(str);
	if ((s = malloc(l+1)) != NULL)
		memmove(s, str, l+1);
	return s;
#endif	/* HAVE_STRDUP */
}

/*
 * Register a function to be called at Exit().
 * This is a simple implementation that can only hold MAXFUNC functions.
 */
int
Atexit(func)
	void (*func) __P((void));
{
#ifdef	__STDC__
	return atexit(func);
#else
	if (funcreg < MAXFUNC) {
		funcs[funcreg++] = func;
		return 0;
	}
	errno = ENOMEM;
	return -1;
#endif
}

void
Exit(status)
	int status;
{
#ifdef	__STDC__
	exit(status);
#else
	int i;

	for (i = 0; i < funcreg; i++)
		(*funcs[i])();
	exit(status);
#endif
}

#ifndef	HAVE_BASENAME
char *
basename(s)
	char *s;
{
	static char def[] = ".";
	char *t;

	if (s == NULL || !strlen(s))
		return def;
	t = s + strlen(s);
	while (t > s && *t != '/')
		t--;
	if (*t == '/' && *(t+1))
		t++;
	return t;
}
#endif

/*
 * Returns system memory page size or -1 on error.
 */
int
Getpagesize()
{
#ifdef	HAVE_GETPAGESIZE
	return getpagesize();
#elif defined(HAVE_SYSCONF)
	return sysconf(_SC_PAGESIZE);
#elif defined(NBPG)
	return NBPG;
#else
	/* whoops, how shall we get the pagesize? */
	return -1;
#endif
}

/*
 * Returns the appropriate error message for errno.
 */
char *
Strerror(en)
	int en;
{
#ifdef	HAVE_STRERROR
	return strerror(en);
#else
	return sys_errlist[en];
#endif
}

#ifndef	HAVE_LABS
/*
 * Computes the absolute value of a long integer.
 */
long
labs(i)
	long i;
{
	return i < 0 ? -i : i;
}
#endif	/* !HAVE_LABS */

#ifndef	HAVE_ABS
/*
 * Same as labs for normal integers.
 */
int
abs(i)
	int i;
{
	return i < 0 ? -i : i;
}
#endif	/* !HAVE_ABS */