summaryrefslogtreecommitdiffstats
path: root/pw/fileupd.c
diff options
context:
space:
mode:
authorDavid Nugent <davidn@FreeBSD.org>1999-10-26 04:27:14 +0000
committerDavid Nugent <davidn@FreeBSD.org>1999-10-26 04:27:14 +0000
commit406891bd852b21f4a6469bac735795902e2893dc (patch)
tree5d83217a5353ef4f097e2eb4f222a029a082cefb /pw/fileupd.c
parente8f6c5579cf8419e33bbcba0ceef55b1cdc34145 (diff)
downloadpw-darwin-406891bd852b21f4a6469bac735795902e2893dc.tar.gz
pw-darwin-406891bd852b21f4a6469bac735795902e2893dc.tar.zst
pw-darwin-406891bd852b21f4a6469bac735795902e2893dc.zip
Clean up error handling in fileupdate(), which now returns 0 on success
instead of a boolean. This replicated through he front-end sub-functions relating to add, delete, modify entries in passwd & group files Errno is now preserved so output of errc()/warnc() will be less obfuscated by subsequent errors when reporting the problem. Add more intelligent error handling when attempting to modify/delete NIS entries with no corresponding local database entry. [MFC to stable in a couple of weeks to keep both in sync]
Diffstat (limited to 'pw/fileupd.c')
-rw-r--r--pw/fileupd.c62
1 files changed, 35 insertions, 27 deletions
diff --git a/pw/fileupd.c b/pw/fileupd.c
index 376f589..a846513 100644
--- a/pw/fileupd.c
+++ b/pw/fileupd.c
@@ -71,38 +71,44 @@ extendarray(char ***buf, int * buflen, int needed)
int
fileupdate(char const * filename, mode_t fmode, char const * newline, char const * prefix, int pfxlen, int updmode)
{
- int rc = 0;
+ int rc = 0;
if (pfxlen <= 1)
- errno = EINVAL;
+ rc = EINVAL;
else {
- int infd = open(filename, O_RDWR | O_CREAT, fmode);
+ int infd = open(filename, O_RDWR | O_CREAT, fmode);
- if (infd != -1) {
- FILE *infp = fdopen(infd, "r+");
+ if (infd == -1)
+ rc = errno;
+ else {
+ FILE *infp = fdopen(infd, "r+");
- if (infp == NULL)
+ if (infp == NULL) {
+ rc = errno; /* Assumes fopen(3) sets errno from open(2) */
close(infd);
- else {
- int outfd;
- char file[MAXPATHLEN];
+ } else {
+ int outfd;
+ char file[MAXPATHLEN];
strcpy(file, filename);
strcat(file, ".new");
outfd = open(file, O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK, fmode);
- if (outfd != -1) {
- FILE *outfp = fdopen(outfd, "w+");
+ if (outfd == -1)
+ rc = errno;
+ else {
+ FILE *outfp = fdopen(outfd, "w+");
- if (outfp == NULL)
+ if (outfp == NULL) {
+ rc = errno;
close(outfd);
- else {
- int updated = UPD_CREATE;
+ } else {
+ int updated = UPD_CREATE;
int linesize = PWBUFSZ;
- char *line = malloc(linesize);
+ char *line = malloc(linesize);
nextline:
while (fgets(line, linesize, infp) != NULL) {
- char *p = strchr(line, '\n');
+ char *p = strchr(line, '\n');
while ((p = strchr(line, '\n')) == NULL) {
int l;
@@ -143,7 +149,11 @@ fileupdate(char const * filename, mode_t fmode, char const * newline, char const
* then error
*/
if (updmode != updated)
- errno = (updmode == UPD_CREATE) ? EEXIST : ENOENT;
+ /* -1 return means:
+ * update,delete=no user entry
+ * create=entry exists
+ */
+ rc = -1;
else {
/*
@@ -155,9 +165,9 @@ fileupdate(char const * filename, mode_t fmode, char const * newline, char const
/*
* Flush the file and check for the result
*/
- rc = fflush(outfp) != EOF;
- if (rc) {
-
+ if (fflush(outfp) == EOF)
+ rc = errno; /* Failed to update */
+ else {
/*
* Copy data back into the
* original file and truncate
@@ -168,18 +178,16 @@ fileupdate(char const * filename, mode_t fmode, char const * newline, char const
fputs(line, infp);
/*
- * If there was a problem with copying
- * we will just rename 'file.new'
- * to 'file'.
+ * If there was a problem with copying
+ * we will just rename 'file.new'
+ * to 'file'.
* This is a gross hack, but we may have
* corrupted the original file
* Unfortunately, it will lose the inode
- * and hence the lock.
+ * and hence the lock.
*/
- if (fflush(infp) == EOF || ferror(infp)) {
- rc = errno; /* Preserve errno for return */
+ if (fflush(infp) == EOF || ferror(infp))
rename(file, filename);
- }
else
ftruncate(infd, ftell(infp));
}