Mercurial > libjeffpc
changeset 814:be934c5f8a42
io: add write_file_at
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Sat, 29 Aug 2020 12:00:28 -0400 |
parents | d563f6d72c1e |
children | a445315f2016 |
files | include/jeffpc/io.h io.c mapfile-vers |
diffstat | 3 files changed, 13 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/include/jeffpc/io.h Tue Aug 25 19:57:25 2020 -0400 +++ b/include/jeffpc/io.h Sat Aug 29 12:00:28 2020 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2018 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * Copyright (c) 2011-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -40,7 +40,8 @@ extern int xpwrite(int fd, const void *buf, size_t nbyte, off_t off); extern char *read_file_common(int dirfd, const char *fname, struct stat *sb); -extern int write_file(const char *fname, const char *data, size_t len); +extern int write_file_at(int dirfd, const char *fname, const char *data, + size_t len); static inline int xwrite_str(int fd, const char *s) { @@ -69,6 +70,11 @@ return ret; } +static inline int write_file(const char *fname, const void *data, size_t len) +{ + return write_file_at(AT_FDCWD, fname, data, len); +} + static inline int xopen(const char *fname, int flags, mode_t mode) { int fd;
--- a/io.c Tue Aug 25 19:57:25 2020 -0400 +++ b/io.c Sat Aug 29 12:00:28 2020 -0400 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2017 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * Copyright (c) 2011-2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -88,13 +88,13 @@ return out; } -int write_file(const char *fname, const char *data, size_t len) +int write_file_at(int dirfd, const char *fname, const char *data, size_t len) { int ret; int fd; - fd = xopen(fname, O_WRONLY | O_CREAT | O_EXCL, - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + fd = xopenat(dirfd, fname, O_WRONLY | O_CREAT | O_EXCL, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd < 0) return fd;