File: fd.c

package info (click to toggle)
bar 1.11.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 928 kB
  • sloc: ansic: 4,381; sh: 3,334; makefile: 264
file content (77 lines) | stat: -rw-r--r-- 1,179 bytes parent folder | download | duplicates (3)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "config.h"

#include "headers.h"
#include "types.h"
#include "error.h"
#include "fd.h"

int set_fl(int fd, int flags)
{
	int val;

	if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
		print_error(stderr, "fcntl() failed");
		return(1);
	}
	val |= flags;
	if (fcntl(fd, F_SETFL, val) < 0) {
		print_error(stderr, "fcntl() failed");
		return(1);
	}
	return(0);
}

int clr_fl(int fd, int flags)
{
	int val;

	if ((val = fcntl(fd, F_GETFL, 0)) < 0) {
		print_error(stderr, "fcntl() failed");
		return(1);
	}
	val &= ~flags;
	if (fcntl(fd, F_SETFL, val) < 0) {
		print_error(stderr, "fcntl() failed");
		return(1);
	}
	return(0);
}

int fdBegin(int fd)
{
	return(set_fl(fd, O_NONBLOCK));
}

int fdEnd(int fd)
{
	return(clr_fl(fd, O_NONBLOCK));
}

int fdIsFile(int fd)
{
	struct stat st;

	if (fstat(fd, &st) != 0) {
		print_error(stderr, "fstat() failed");
		return(0);
	}
	if (S_ISREG(st.st_mode)) return(1);
	return(0);
}

int fdFileSize(int fd, uint64 *size)
{
	struct stat st;

	if (size == 0) {
		print_error(stderr, "size == NULL");
		return(1);
	}
	if (fstat(fd, &st) != 0) {
		print_error(stderr, "fstat() failed");
		return(1);
	}
	*size = (uint64)st.st_size;
	return(0);
}