From 4cfdc68fd862a4e80f42f14aa92cb25db08b2466 Mon Sep 17 00:00:00 2001
From: Sergey Poznyakoff <gray@gnu.org>
Date: Sat, 2 Jul 2022 19:29:47 +0300
Subject: [PATCH 12/14] Improve handling of -u in gdbm_load

* tools/gdbm_load.c (main): Imply the owner login group if owner name
is followed by a :, and the current login group otherwise.
* doc/gdbm.texi: Document changes.
---
 doc/gdbm.texi     | 13 ++++++++-----
 tools/gdbm_load.c |  9 +++++++--
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/doc/gdbm.texi b/doc/gdbm.texi
index 4fa841f..037ae07 100644
--- a/doc/gdbm.texi
+++ b/doc/gdbm.texi
@@ -4119,13 +4119,16 @@ dump file will be added to that database, without removing the
 existing keys.  To overwrite the existing keys from the dump file, use
 @option{--update --replace}.
 
-@item -u @var{user}[:@var{group}]
-@itemx --user=@var{user}[:@var{group}]
-Set file owner.  The @var{user} can be either a valid user name or
+@item -u @var{owner}[:[@var{group}]]
+@itemx --user=@var{owner}[:[@var{group}]]
+Set file owner.  The @var{owner} can be either a valid user name or
 UID.  Similarly, the @var{group} is either a valid group name or GID.
-If @var{group} is not given, the main group of @var{user} is used.
+If @var{group} is not given, the main group of @var{owner} is implied, if
+@var{owner} is followed by a @samp{:}, otherwise the login group of the
+current user is implied.
 
-User and group parts can be separated by a dot, instead of the colon.
+User and group parts can be separated by a dot, instead of the colon,
+but such usage is discouraged.
 
 @item -h
 @itemx --help
diff --git a/tools/gdbm_load.c b/tools/gdbm_load.c
index 5d5e50e..e7e2ab1 100644
--- a/tools/gdbm_load.c
+++ b/tools/gdbm_load.c
@@ -148,9 +148,10 @@ main (int argc, char **argv)
 	{
 	  size_t len;
 	  struct passwd *pw;
+	  int delim;
 	  
 	  len = strcspn (optarg, ".:");
-	  if (optarg[len])
+	  if ((delim = optarg[len]) != 0)
 	    optarg[len++] = 0;
 	  pw = getpwnam (optarg);
 	  if (pw)
@@ -187,7 +188,7 @@ main (int argc, char **argv)
 		    }
 		}
 	    }
-	  else
+	  else if (delim)
 	    {
 	      if (!pw)
 		{
@@ -200,6 +201,10 @@ main (int argc, char **argv)
 		}
 	      owner_gid = pw->pw_gid;
 	    }
+	  else
+	    {
+	      owner_gid = getgid();
+	    }
 	  meta_mask |= GDBM_META_MASK_OWNER;
 	}
 	break;
-- 
2.33.0

