-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.c
52 lines (42 loc) · 1011 Bytes
/
library.c
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
// SPDX-License-Identifier: MIT
// Copyright 2024 s3rj1k.
#define _GNU_SOURCE
#include <dlfcn.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static char *(*original_fgets)(char *buf, int n, FILE *stream) = NULL;
char *fgets(char *buf, int n, FILE *stream)
{
if (!original_fgets)
{
original_fgets = (char *(*)(char *, int, FILE *))dlsym(RTLD_NEXT, "fgets");
}
if (stream != stdin)
{
return original_fgets(buf, n, stream);
}
const char *command = getenv("AUTH_CMD");
if (command == NULL)
{
return original_fgets(buf, n, stream);
}
FILE *proc = popen(command, "r");
if (proc == NULL)
{
return original_fgets(buf, n, stream);
}
char *result = original_fgets(buf, n, proc);
int status = pclose(proc);
if (status == -1)
{
return NULL;
}
return result;
}
char *gets(char *buf)
{
return fgets(buf, INT_MAX, stdin) ? buf : NULL;
}