-
Notifications
You must be signed in to change notification settings - Fork 4
/
read_buf.c
81 lines (74 loc) · 1.73 KB
/
read_buf.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
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
#include "read_buf.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
static int Infile_is_open=0;
static int Infd=STDIN_FILENO;
int read_buf (char* buffer,
int wanttoread,
const char* const infile,
const int tail_bytes)
{
int bytesread = 0, hasrolled, newfd, sysread_bytes;
struct stat statbuf, fstatbuf;
if (!Infile_is_open && infile) {
while (0 > (newfd = open (infile, O_RDONLY))) {
fprintf (stderr, "Couldn't open %s (%s), trying again\n",
infile,
strerror (errno));
sleep (1);
}
lseek (newfd, -tail_bytes, SEEK_END);
if (Infd != STDIN_FILENO)
close (Infd);
Infd = newfd;
Infile_is_open = 1;
}
while (wanttoread > 0) {
hasrolled =
0 != infile
&&
0 == stat (infile, &statbuf)
&&
0 == fstat (Infd, &fstatbuf)
&&
statbuf.st_ino != fstatbuf.st_ino;
sysread_bytes = read (Infd, buffer, wanttoread);
if (infile == 0)
return (sysread_bytes > 0 ? sysread_bytes : 0);
if (sysread_bytes > 0) {
buffer += sysread_bytes;
wanttoread -= sysread_bytes;
bytesread += sysread_bytes;
}
if (wanttoread > 0) {
if (hasrolled) {
if (Infd != STDIN_FILENO)
close (Infd);
Infd = open (infile, O_RDONLY);
}
usleep (100);
}
}
return bytesread;
}
#ifdef COMPILER_CAN_READ_PSEUDOCODE
read () {
while (reading_dir && eof ()) {
has_rolled
= reading_dir && (current file's inode != current.mp3's inode);
if (has_rolled && eof ()) {
close();
open("<indir>/current.mp3");
} else {
usleep(100);
}
}
sysread ();
return bytes_read;
}
#endif