File: misc.c

package info (click to toggle)
wily 0.13.41-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,560 kB
  • ctags: 3,426
  • sloc: ansic: 25,364; perl: 580; makefile: 445; sh: 415; python: 30; exp: 17
file content (113 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download | duplicates (12)
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
/* Copyright (c) 1992 AT&T - All rights reserved. */
#include	<u.h>
#include	<libc.h>
#include	<pwd.h>

#ifdef	HAVE_STDARG_H
#include	<stdarg.h>
#else
#include	<varargs.h>
#endif

#ifdef HAVE_STDARG_H
void
fprint(int fd, char *z, ...)
{
	va_list args;
	char buf[2048];			/* pick reasonable blocksize */

	va_start(args, z);
	vsprintf(buf, z, args);
	write(fd, buf, strlen(buf));
	va_end(args);
}
#else	/* !HAVE_STDARG_H */
void
fprint(va_alist)
va_dcl
{
	int fd;
	char *fmt;
	va_list args;
	char buf[2048];			/* pick reasonable blocksize */

	va_start(args);
	fd = va_arg(args, int);
	fmt = va_arg(args, char *);
	vsprintf(buf, fmt, args);
	write(fd, buf, strlen(buf));
	va_end(args);
}
#endif	/* HAVE_STDARG_H */

#ifndef HAVE_STRERROR
char *
strerror(int n)
{
	extern char *sys_errlist[];
	return sys_errlist[n];
}
#endif /* HAVE_STRERROR */

int errstr(char *buf)
{
	extern int errno;

	strncpy(buf, strerror(errno), ERRLEN);
	return 1;
}

char*
getuser(void)
{
	struct passwd *p;

	static char *user = 0;

	if (!user) {
		p = getpwuid(getuid());
		if (p && p->pw_name) {
			user = malloc(strlen(p->pw_name)+1);
			if (user)
				strcpy(user, p->pw_name);
		}
	}
	if(!user)
		user = "unknown";
	return user;
}

#ifndef HAVE_MEMMOVE
/*
 * memcpy is probably fast, but may not work with overlap
 */
void*
memmove(void *a1, const void *a2, size_t n)
{
	char *s1;
	const char *s2;

	s1 = a1;
	s2 = a2;
	if(s1 > s2)
		goto back;
	if(s1 + n <= s2)
		return memcpy(a1, a2, n);
	while(n > 0) {
		*s1++ = *s2++;
		n--;
	}
	return a1;

back:
	s2 += n;
	if(s2 <= s1)
		return memcpy(a1, a2, n);
	s1 += n;
	while(n > 0) {
		*--s1 = *--s2;
		n--;
	}
	return a1;
}
#endif /* HAVE_MEMMOVE */