summaryrefslogtreecommitdiffstats
path: root/debian/grub-extras/disabled/gpxe/src/include/gpxe/posix_io.h
blob: 3063dfff29950271b9ec9ccb6e26286883d40224 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef _GPXE_POSIX_IO_H
#define _GPXE_POSIX_IO_H

/** @file
 *
 * POSIX-like I/O
 *
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>
#include <gpxe/uaccess.h>

/** Minimum file descriptor that will ever be allocated */
#define POSIX_FD_MIN ( 1 )

/** Maximum file descriptor that will ever be allocated */
#define POSIX_FD_MAX ( 31 )

/** File descriptor set as used for select() */
typedef uint32_t fd_set;

extern int open ( const char *uri_string );
extern ssize_t read_user ( int fd, userptr_t buffer,
			   off_t offset, size_t len );
extern int select ( fd_set *readfds, int wait );
extern ssize_t fsize ( int fd );
extern int close ( int fd );

/**
 * Zero a file descriptor set
 *
 * @v set		File descriptor set
 */
static inline __attribute__ (( always_inline )) void
FD_ZERO ( fd_set *set ) {
	*set = 0;
}

/**
 * Set a bit within a file descriptor set
 *
 * @v fd		File descriptor
 * @v set		File descriptor set
 */
static inline __attribute__ (( always_inline )) void
FD_SET ( int fd, fd_set *set ) {
	*set |= ( 1 << fd );
}

/**
 * Clear a bit within a file descriptor set
 *
 * @v fd		File descriptor
 * @v set		File descriptor set
 */
static inline __attribute__ (( always_inline )) void
FD_CLR ( int fd, fd_set *set ) {
	*set &= ~( 1 << fd );
}

/**
 * Test a bit within a file descriptor set
 *
 * @v fd		File descriptor
 * @v set		File descriptor set
 * @ret is_set		Corresponding bit is set
 */
static inline __attribute__ (( always_inline )) int
FD_ISSET ( int fd, fd_set *set ) {
	return ( *set & ( 1 << fd ) );
}

/**
 * Read data from file
 *
 * @v fd		File descriptor
 * @v buf		Data buffer
 * @v len		Maximum length to read
 * @ret len		Actual length read, or negative error number
 */
static inline ssize_t read ( int fd, void *buf, size_t len ) {
	return read_user ( fd, virt_to_user ( buf ), 0, len );
}

#endif /* _GPXE_POSIX_IO_H */