]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - login-utils/newgrp.c
whereis: use xstrncpy()
[thirdparty/util-linux.git] / login-utils / newgrp.c
index 367333ec3bbc318ce614b694265eb96c7e8f06e4..5e4b4caa025ebfe2f301b3fa1f2b3513047c3177 100644 (file)
@@ -60,6 +60,7 @@ static char *xgetpass(FILE *input, const char *prompt)
        return pass;
 }
 
+#ifndef HAVE_EXPLICIT_BZERO
 /* Ensure memory is set to value c without compiler optimization getting
  * into way that could happen with memset(3). */
 static int xmemset_s(void *v, size_t sz, const int c)
@@ -72,6 +73,7 @@ static int xmemset_s(void *v, size_t sz, const int c)
                *p++ = c;
        return 0;
 }
+#endif
 
 /* try to read password from gshadow */
 static char *get_gshadow_pwd(const char *groupname)
@@ -148,7 +150,11 @@ static int allow_setgid(const struct passwd *pe, const struct group *ge)
        if (pwd && *pwd && (xpwd = xgetpass(stdin, _("Password: ")))) {
                char *cbuf = crypt(xpwd, pwd);
 
+#ifdef HAVE_EXPLICIT_BZERO
+               explicit_bzero(xpwd, strlen(xpwd));
+#else
                xmemset_s(xpwd, strlen(xpwd), 0);
+#endif
                free(xpwd);
                if (!cbuf)
                        warn(_("crypt failed"));
@@ -160,19 +166,19 @@ static int allow_setgid(const struct passwd *pe, const struct group *ge)
        return FALSE;
 }
 
-static void __attribute__((__noreturn__)) usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
-       fprintf(out, USAGE_HEADER);
+       FILE *out = stdout;
+       fputs(USAGE_HEADER, out);
        fprintf(out, _(" %s <group>\n"), program_invocation_short_name);
 
        fputs(USAGE_SEPARATOR, out);
        fputs(_("Log in to a new group.\n"), out);
 
-       fprintf(out, USAGE_OPTIONS);
-       fprintf(out, USAGE_HELP);
-       fprintf(out, USAGE_VERSION);
-       fprintf(out, USAGE_MAN_TAIL("newgrp(1)"));
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       fputs(USAGE_OPTIONS, out);
+       printf(USAGE_HELP_OPTIONS(16));
+       printf(USAGE_MAN_TAIL("newgrp(1)"));
+       exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char *argv[])
@@ -190,15 +196,14 @@ int main(int argc, char *argv[])
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1)
                switch (ch) {
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'h':
-                       usage(stdout);
+                       usage();
                default:
                        errtryhelp(EXIT_FAILURE);
                }
@@ -230,8 +235,5 @@ int main(int argc, char *argv[])
        shell = (pw_entry->pw_shell && *pw_entry->pw_shell ?
                                pw_entry->pw_shell : _PATH_BSHELL);
        execl(shell, shell, (char *)0);
-       warn(_("failed to execute %s"), shell);
-       fflush(stderr);
-
-       return EXIT_FAILURE;
+       errexec(shell);
 }