xref: /inferno-os/utils/ld/Posix.c (revision 7ef44d652ae9e5e1f5b3465d73684e4a54de73c0)
1 #include	"l.h"
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/times.h>
5 #undef getwd
6 #include <unistd.h>	/* For sysconf() and _SC_CLK_TCK */
7 
8 /*
9  * fake malloc
10  */
11 void*
12 malloc(size_t n)
13 {
14 	void *p;
15 
16 	while(n & 7)
17 		n++;
18 	while(nhunk < n)
19 		gethunk();
20 	p = hunk;
21 	nhunk -= n;
22 	hunk += n;
23 	return p;
24 }
25 
26 void
27 free(void *p)
28 {
29 	USED(p);
30 }
31 
32 void*
33 calloc(size_t m, size_t n)
34 {
35 	void *p;
36 
37 	n *= m;
38 	p = malloc(n);
39 	memset(p, 0, n);
40 	return p;
41 }
42 
43 void*
44 realloc(void *p, size_t n)
45 {
46 	fprint(2, "realloc called\n", p, n);
47 	abort();
48 	return 0;
49 }
50 
51 void*
52 mysbrk(ulong size)
53 {
54 	return (void*)sbrk(size);
55 }
56 
57 double
58 cputime(void)
59 {
60 
61 	struct tms tmbuf;
62 	double	ret_val;
63 
64 	/*
65 	 * times() only fails if &tmbuf is invalid.
66 	 */
67 	(void)times(&tmbuf);
68 	/*
69 	 * Return the total time (in system clock ticks)
70 	 * spent in user code and system
71 	 * calls by both the calling process and its children.
72 	 */
73 	ret_val = (double)(tmbuf.tms_utime + tmbuf.tms_stime +
74 			tmbuf.tms_cutime + tmbuf.tms_cstime);
75 	/*
76 	 * Convert to seconds.
77 	 */
78 	ret_val *= sysconf(_SC_CLK_TCK);
79 	return ret_val;
80 
81 }
82 
83 int
84 fileexists(char *name)
85 {
86 	struct stat sb;
87 
88 	return stat(name, &sb) >= 0;
89 }
90