hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/kernel/users.rv
blob: 917b9cedfae8a5a29e2c023d2235efb36785ef62 (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
# user lookups against /etc/passwd

fn parse_uid(text) -> num do
  let value = 0
  for index in 0..text:len() do
    const code = text[index]:ascii()
    if (code >= 48 and code <= 57)
      value = value * 10 + (code - 48)
  end
  return value
end

# the name for a uid or the number itself if /etc/passwd doesnt list it
global user_name = fn(uid) -> string do
  const size = fs:size("/etc/passwd")
  if (size <= 0)
    return "#{uid}"

  const lines = fs:read_bytes("/etc/passwd", 0, size):split("\n")
  for index in 0..lines:len() do
    const fields = lines[index]:split(":")
    if (fields:len() >= 2 and parse_uid(fields[0]) == uid)
      return fields[1]
  end

  return "#{uid}"
end

# the uid for a name
global user_uid = fn(name) -> num do
  const size = fs:size("/etc/passwd")
  if (size <= 0)
    return -1

  const lines = fs:read_bytes("/etc/passwd", 0, size):split("\n")
  for index in 0..lines:len() do
    const fields = lines[index]:split(":")
    if (fields:len() >= 2 and fields[1] == name)
      return parse_uid(fields[0])
  end

  return -1
end