#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
+#include <string.h>
#include "c.h"
#include "fileutils.h"
*p = '\0';
return p + 1;
}
+
+/* Copies the contents of a file. Returns -1 on read error, -2 on write error. */
+int ul_copy_file(int from, int to)
+{
+ ssize_t nr, nw, off;
+ char buf[8 * 1024];
+
+ while ((nr = read(from, buf, sizeof(buf))) > 0)
+ for (off = 0; nr > 0; nr -= nw, off += nw)
+ if ((nw = write(to, buf + off, nr)) < 0)
+ return -2;
+ if (nr < 0)
+ return -1;
+#ifdef HAVE_EXPLICIT_BZERO
+ explicit_bzero(buf, sizeof(buf));
+#endif
+ return 0;
+}
void pw_error (char *, int, int);
-static void copyfile(int from, int to)
-{
- int nr, nw, off;
- char buf[8 * 1024];
-
- while ((nr = read(from, buf, sizeof(buf))) > 0)
- for (off = 0; nr > 0; nr -= nw, off += nw)
- if ((nw = write(to, buf + off, nr)) < 0)
- pw_error(tmp_file, 1, 1);
-
- if (nr < 0)
- pw_error(orig_file, 1, 1);
-#ifdef HAVE_EXPLICIT_BZERO
- explicit_bzero(buf, sizeof(buf));
-#endif
-}
-
static void pw_init(void)
{
struct rlimit rlim;
{
FILE *fd;
char *tmpname = NULL;
+ int res;
if ((fd = xfmkstemp(&tmpname, "/etc", ".vipw")) == NULL) {
ulckpwdf();
err(EXIT_FAILURE, _("can't open temporary file"));
}
- copyfile(lockfd, fileno(fd));
tmp_file = tmpname;
+ res = ul_copy_file(lockfd, fileno(fd));
+ if (res == -1)
+ pw_error(orig_file, 1, 1);
+ else if (res == -2)
+ pw_error(tmp_file, 1, 1);
return fd;
}