Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ extern void _file_init(void);

/* stdio locking functions */
extern void flockfile(FILE *stream);


extern int ftrylockfile(FILE *stream);


extern void funlockfile(FILE *stream);


Expand Down
47 changes: 40 additions & 7 deletions stdio/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ typedef struct {
} popen_FILE;


static const struct lockAttr flockAttr = {
.type = PH_LOCK_RECURSIVE
};


FILE *stdin, *stdout, *stderr;


Expand Down Expand Up @@ -185,28 +190,41 @@ FILE *fopen(const char *filename, const char *mode)
int m;
FILE *f;
int fd;
int err;

if ((m = string2mode(mode)) < 0) {
errno = EINVAL;
return NULL;
}

if ((fd = __safe_open(filename, m, DEFFILEMODE)) < 0) {
/* errno set by open */
return NULL;
}

if ((f = calloc(1, sizeof(FILE))) == NULL) {
err = errno;
__safe_close(fd);
errno = err;
return NULL;
}

if ((f->buffer = buffAlloc(BUFSIZ)) == NULL) {
err = errno;
__safe_close(fd);
free(f);
errno = err;
return NULL;
}

if ((err = mutexCreateWithAttr(&f->lock, &flockAttr)) < 0) {
buffFree(f->buffer, BUFSIZ);
__safe_close(fd);
free(f);
errno = abs(err);
return NULL;
}

mutexCreate(&f->lock);
f->bufsz = BUFSIZ;
f->fd = fd;
f->mode = m;
Expand All @@ -221,7 +239,7 @@ FILE *fopen(const char *filename, const char *mode)

FILE *fdopen(int fd, const char *mode)
{
int m, fdm;
int m, fdm, err;
FILE *f;

if ((m = string2mode(mode)) < 0) {
Expand Down Expand Up @@ -249,7 +267,14 @@ FILE *fdopen(int fd, const char *mode)
return NULL;
}

mutexCreate(&f->lock);
mutexCreateWithAttr(&f->lock, &flockAttr);
Comment thread
julianuziemblo marked this conversation as resolved.
if ((err = mutexCreateWithAttr(&f->lock, &flockAttr)) < 0) {
buffFree(f->buffer, BUFSIZ);
free(f);
errno = abs(err);
return NULL;
}

f->bufsz = BUFSIZ;
f->fd = fd;
f->mode = m;
Expand Down Expand Up @@ -1300,7 +1325,7 @@ FILE *popen(const char *command, const char *mode)
goto failed;
}

if (mutexCreate(&pf->file.lock) < 0) {
if (mutexCreateWithAttr(&pf->file.lock, &flockAttr) < 0) {
pf->file.lock = 0;
goto failed;
}
Expand Down Expand Up @@ -1401,16 +1426,16 @@ void _file_init(void)
stdout->bufsz = BUFSIZ;

stdin->bufeof = stdin->bufpos = BUFSIZ;
mutexCreate(&stdin->lock);
mutexCreateWithAttr(&stdin->lock, &flockAttr);

stdout->bufpos = 0;
stdout->flags = F_WRITING;
mutexCreate(&stdout->lock);
mutexCreateWithAttr(&stdout->lock, &flockAttr);

stderr->buffer = NULL;
stderr->bufsz = 0;
stderr->flags = F_WRITING;
mutexCreate(&stderr->lock);
mutexCreateWithAttr(&stderr->lock, &flockAttr);

if (isatty(stdout->fd)) {
stdout->flags |= F_LINE;
Expand All @@ -1428,11 +1453,19 @@ void _file_init(void)

void flockfile(FILE *file)
{
mutexLock(file->lock);
}


int ftrylockfile(FILE *file)
{
return mutexTry(file->lock);
}


void funlockfile(FILE *file)
{
mutexUnlock(file->lock);
}


Expand Down
Loading