summaryrefslogtreecommitdiff
path: root/userland/minibox/utilities/lock.c
blob: 027038d585123933d8ffc199de4193b8f232faed (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
#include "include.h"
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int lock_main(int argc, char **argv) {
  if (argc < 2) {
    fprintf("Usage: %s <password>", argv[0] ? argv[0] : "lock");
    return 1;
  }
  char *password = argv[1];
  size_t buffer_length = strlen(password) + 1;
  char *buffer = malloc(buffer_length);

  int should_exit = 0;
  size_t i = 0;
  for (;;) {
    char c;
    if (0 == read(STDIN_FILENO, &c, 1)) {
      break;
    }
    if ('\n' == c) {
      break;
    }
    if (i > buffer_length) {
      should_exit = 1;
      continue;
    }
    buffer[i] = c;
    i++;
  }
  buffer[i] = '\0';

  if (should_exit) {
    return 1;
  }

  if (0 != strcmp(password, buffer)) {
    return 1;
  }
  return 0;
}