File: open.c

package info (click to toggle)
tinyssh 20250501-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (45 lines) | stat: -rw-r--r-- 1,021 bytes parent folder | download
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
/*
taken from nacl-20110221, from curvecp/open_cwd.c curvecp/open_pipe.c
curvecp/open_read.c  curvecp/open_write.c
- reformated using clang-format
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "blocking.h"
#include "open.h"

int open_cwd(void) { return open_read("."); }

int open_pipe(int *fd) {
    int i;
    if (pipe(fd) == -1) return -1;
    for (i = 0; i < 2; ++i) {
        fcntl(fd[i], F_SETFD, 1);
        blocking_disable(fd[i]);
    }
    return 0;
}

int open_read(const char *fn) {
#ifdef O_CLOEXEC
    return open(fn, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
#else
    int fd = open(fn, O_RDONLY | O_NONBLOCK);
    if (fd == -1) return -1;
    fcntl(fd, F_SETFD, 1);
    return fd;
#endif
}

int open_write(const char *fn) {
#ifdef O_CLOEXEC
    return open(fn, O_CREAT | O_WRONLY | O_NONBLOCK | O_CLOEXEC, 0644);
#else
    int fd = open(fn, O_CREAT | O_WRONLY | O_NONBLOCK, 0644);
    if (fd == -1) return -1;
    fcntl(fd, F_SETFD, 1);
    return fd;
#endif
}