blob: 4af665d125f60b93d28ea1aa005099af7b65e04f (
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
|
#include "lslib.h"
#include <paths.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
FILE* get_file_s(const char* path, const char* type) {
struct stat s;
FILE* file;
if (streql("-", path) && type[0] == 'r') {
clearerr(stdin);
fflush(stdin);
return stdin;
}
if (lstat(path, &s) < 0) {
if (type[0] != 'r') goto read;
error_s("failed to read %s", path);
return NULL;
}
if (S_ISDIR(s.st_mode)) {
error_s("%s is a directory", path);
return NULL;
}
read:
file = fopen(path, type);
if (file == NULL) {
error_s("failed to %s file %s", type[0] == 'r' ? "read" : "write", path);
}
return file;
}
FILE* get_file(const char* path, const char* type) {
FILE* file = get_file_s(path, type);
if (file == NULL) exit(EXIT_FAILURE);
return file;
}
int get_tty (void) {
int fd = open(_PATH_TTY, O_RDONLY | O_NOCTTY | O_SYNC);
if (fd < 0) error("failed to get tty");
return fd;
}
FILE* get_tty_stream(char* type) {
int fd = get_tty();
FILE* file = fdopen(fd, type);
if (file == NULL) {
error("failed to open tty stream");
}
return file;
}
|