shadow/libmisc
Alejandro Colomar f45498a6c2 libmisc/write_full.c: Improve write_full()
Documentation:

-  Correct the comment documenting the function:

   write_full() doesn't write "up to" count bytes (which is write(2)'s
   behavior, and exactly what this function is designed to avoid), but
   rather exactly count bytes (on success).

-  While fixing the documentation, take the time to add a man-page-like
   comment as in other APIs.  Especially, since we'll have to document
   a few other changes from this patch, such as the modified return
   values.

-  Partial writes are still possible on error.  It's the caller's
   responsibility to handle that possibility.

API:

-  In write(2), it's useful to know how many bytes were transferred,
   since it can have short writes.  In this API, since it either writes
   it all or fails, that value is useless, and callers only want to know
   if it succeeded or not.  Thus, just return 0 or -1.

Implementation:

-  Use `== -1` instead of `< 0` to check for write(2) syscall errors.
   This is wisdom from Michael Kerrisk.  This convention is useful
   because it more explicitly tells maintainers that the only value
   which can lead to that path is -1.  Otherwise, a maintainer of the
   code might be confused to think that other negative values are
   possible.  Keep it simple.

-  The path under `if (res == 0)` was unreachable, since the loop
   condition `while (count > 0)` precludes that possibility.  Remove the
   dead code.

-  Use a temporary variable of type `const char *` to avoid a cast.

-  Rename `res`, which just holds the result from write(2), to `w`,
   which more clearly shows that it's just a very-short-lived variable
   (by it's one-letter name), and also relates itself more to write(2).
   I find it more readable.

-  Move the definition of `w` to the top of the function.  Now that the
   function is significantly shorter, the lifetime of the variable is
   clearer, and I find it more readable this way.

Use:

-  Also use `== -1` to check errors.

Cc: Christian Göttsche <cgzones@googlemail.com>
Cc: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2023-08-18 20:35:15 -05:00
..
.indent.pro Commit the last version from the PLD CVS repository. 2007-10-07 14:36:51 +00:00
addgrps.c Simplify allocation APIs 2023-06-08 09:05:39 -05:00
age.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
agetpass.c libmisc: include freezero 2023-08-10 09:29:17 -05:00
alloc.c libmisc: Add safer allocation functions 2023-02-23 20:28:43 -06:00
audit_help.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
basename.c Fix null dereference in basename 2023-03-27 10:10:37 -05:00
bit.c Add bit manipulation functions 2023-01-30 18:24:15 +01:00
btrfs.c Declare read-only data const 2022-08-06 11:27:56 -05:00
chkname.c lib: replace USER_NAME_MAX_LENGTH macro 2023-08-02 10:13:28 -05:00
chkname.h Update licensing info 2021-12-23 19:36:50 -06:00
chowndir.c Avoid races in chown_tree() 2022-08-17 12:34:01 -05:00
chowntty.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
cleanup_group.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
cleanup_user.c Fix typos 2023-02-16 13:23:08 -06:00
cleanup.c Update licensing info 2021-12-23 19:36:50 -06:00
console.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
copydir.c libmisc/write_full.c: Improve write_full() 2023-08-18 20:35:15 -05:00
csrand.c libmisc/csrand.c: Update comments 2023-06-05 16:50:40 -05:00
date_to_str.c Fix crash with large timestamps 2023-03-28 13:00:38 +02:00
entry.c libmisc: Move xmalloc.c to alloc.c 2023-02-23 20:28:43 -06:00
env.c Simplify allocation APIs 2023-06-08 09:05:39 -05:00
failure.c libmisc/write_full.c: Improve write_full() 2023-08-18 20:35:15 -05:00
failure.h utmp: move failtmp() 2023-08-02 10:13:28 -05:00
find_new_gid.c Use safer allocation macros 2023-02-23 20:28:43 -06:00
find_new_sub_gids.c Don't redefine errno(3) 2022-12-22 11:43:29 +01:00
find_new_sub_uids.c Don't redefine errno(3) 2022-12-22 11:43:29 +01:00
find_new_uid.c Simplify allocation APIs 2023-06-08 09:05:39 -05:00
freezero.c libmisc: add freezero source code 2023-08-10 09:29:17 -05:00
freezero.h libmisc: add freezero source code 2023-08-10 09:29:17 -05:00
getdate.h Update licensing info 2021-12-23 19:36:50 -06:00
getdate.y Drop alloca(3) 2023-06-08 09:05:39 -05:00
getgr_nam_gid.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
getrange.c Declare read-only parameters const 2022-08-06 11:27:56 -05:00
gettime.c Use strict prototype in definition 2023-03-20 08:47:52 +01:00
hushed.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
idmapping.c libmisc/write_full.c: Improve write_full() 2023-08-18 20:35:15 -05:00
idmapping.h Add include for uid_t 2022-08-06 11:27:56 -05:00
isexpired.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
limits.c libmisc: call active_sessions_count() 2023-08-02 10:13:28 -05:00
list.c Simplify allocation APIs 2023-06-08 09:05:39 -05:00
log.c libmisc/write_full.c: Improve write_full() 2023-08-18 20:35:15 -05:00
logind.c libmisc: implement active_sessions_count() 2023-08-02 10:13:28 -05:00
loginprompt.c login_prompt: Simplify login_prompt API 2023-05-03 07:54:28 -05:00
mail.c Simplify allocation APIs 2023-06-08 09:05:39 -05:00
Makefile.am libmisc: add freezero source code 2023-08-10 09:29:17 -05:00
mempcpy.c Add mempcpy(3) 2023-02-16 11:29:33 +01:00
motd.c libmisc: Move xmalloc.c to alloc.c 2023-02-23 20:28:43 -06:00
myname.c Update licensing info 2021-12-23 19:36:50 -06:00
obscure.c libmisc: include freezero 2023-08-10 09:29:17 -05:00
pam_pass_non_interactive.c Use safer allocation macros 2023-02-23 20:28:43 -06:00
pam_pass.c Don't redefine errno(3) 2022-12-22 11:43:29 +01:00
prefix_flag.c configure: check whether fgetpwent_r is available before marking xprefix_getpwnam_r as reentrant 2023-06-09 16:22:24 +02:00
pwd2spwd.c passwd: fall back to non-PAM code when prefix is used 2023-06-09 16:22:24 +02:00
pwd_init.c Assume SIGTTOU is defined 2022-12-15 16:22:05 -06:00
pwdcheck.c Call NULL by its name 2023-02-02 13:08:30 -06:00
readpassphrase.c libmisc: add readpassphrase source code 2023-08-10 09:29:17 -05:00
readpassphrase.h Replace __{BEGIN,END}_DECLS with #ifdef __cplusplus 2023-08-18 18:04:11 -05:00
remove_tree.c Avoid races in remove_tree() 2022-08-17 12:34:01 -05:00
rlogin.c Assume B[0-9]* macros are defined 2022-12-15 16:22:05 -06:00
root_flag.c libmisc: Use safer chroot/chdir sequence 2023-05-25 08:25:42 -05:00
salt.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
setugid.c Update licensing info 2021-12-23 19:36:50 -06:00
setupenv.c Simplify allocation APIs 2023-06-08 09:05:39 -05:00
shell.c Call NULL by its name 2023-02-02 13:08:30 -06:00
stpecpy.c Add stpecpy() 2023-02-16 11:29:33 +01:00
stpeprintf.c Add stpeprintf() 2023-02-16 11:29:33 +01:00
strtoday.c strtoday: remove unnecessary cast 2023-06-09 07:48:48 -05:00
sub.c libmisc: Use safer chroot/chdir sequence 2023-05-25 08:25:42 -05:00
sulog.c Call NULL by its name 2023-02-02 13:08:30 -06:00
ttytype.c ttytype(): Fix race 2023-02-09 10:03:03 -06:00
tz.c Remove superfluous casts 2023-02-09 10:03:03 -06:00
ulimit.c Remove comments that survived the Helicoprion 2022-12-15 16:22:05 -06:00
user_busy.c Fix typo 2023-02-16 13:23:08 -06:00
utmp.c libmisc/write_full.c: Improve write_full() 2023-08-18 20:35:15 -05:00
valid.c Update licensing info 2021-12-23 19:36:50 -06:00
xgetgrgid.c Fix comments 2023-02-16 13:23:08 -06:00
xgetgrnam.c Fix comments 2023-02-16 13:23:08 -06:00
xgetpwnam.c libmisc: fix grammar 2023-01-26 22:44:39 -06:00
xgetpwuid.c Fix comments 2023-02-16 13:23:08 -06:00
xgetspnam.c Fix comments 2023-02-16 13:23:08 -06:00
xgetXXbyYY.c Simplify allocation APIs 2023-06-08 09:05:39 -05:00
xprefix_getpwnam.c configure: check whether fgetpwent_r is available before marking xprefix_getpwnam_r as reentrant 2023-06-09 16:22:24 +02:00
yesno.c libmisc/yesno.c: Fix regression 2023-04-28 11:22:48 -05:00