summaryrefslogtreecommitdiff
path: root/lib/mlibc/sysdeps/ironclad/generic/utmpx.cpp
blob: ec74e22092042c25f3c4206967fc8ca276566917 (plain)
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
#include <bits/ensure.h>
#include <stddef.h>
#include <errno.h>
#include <utmpx.h>
#include <stdio.h>
#include <time.h>
#include <paths.h>
#include <unistd.h>
#include <fcntl.h>

int utmpx_file = -1;

void updwtmpx(const char *, const struct utmpx *) {
	// Empty as musl does
}

void endutxent(void) {
	if (utmpx_file >= 0) {
		close(utmpx_file);
	}
}

void setutxent(void) {
	if (utmpx_file < 0) {
		utmpx_file = open(UTMPX_FILE, O_RDWR | O_CREAT, 0755);
	} else {
		lseek(utmpx_file, 0, SEEK_SET);
	}
}

struct utmpx returned;

struct utmpx *getutxent(void) {
	if (utmpx_file < 0) {
		setutxent();
		if (utmpx_file < 0) {
			return NULL;
		}
	}

	if (read(utmpx_file, &returned, sizeof(struct utmpx)) != sizeof(struct utmpx)) {
		return NULL;
	}

	return &returned;
}

struct utmpx *pututxline(const struct utmpx *added) {
	if (utmpx_file < 0) {
		setutxent();
		if (utmpx_file < 0) {
			return NULL;
		}
	}

	lseek(utmpx_file, 0, SEEK_END);
	if (write(utmpx_file, added, sizeof(struct utmpx)) != sizeof(struct utmpx)) {
		return NULL;
	}

	return (struct utmpx *)added;
}

int utmpxname(const char *path) {
	if (utmpx_file > 0) {
		close(utmpx_file);
	}

	utmpx_file = open(path, O_RDWR | O_CREAT, 0755);
	if (utmpx_file > 0) {
		lseek(utmpx_file, 0, SEEK_END);
		return 1;
	} else {
		return 0;
	}
}