Branch
Hash :
6ca831b0
Author :
Date :
2025-09-16T18:57:41
stringeq: prefer memeq to memcmp in other modules * lib/argmatch.c, lib/argmatch.h, lib/backupfile.c, lib/bcp47.c: * lib/boot-time.c, lib/csharpcomp.c, lib/csharpexec.c: * lib/file-has-acl.c, lib/gen-uni-tables.c, lib/get_ppid_of.c: * lib/get_progname_of.c, lib/getlogin_r.c, lib/getprogname.c: * lib/getumask.c, lib/isnan.c, lib/mbchar.h, lib/mem-hash-map.c: * lib/memcoll.c, lib/progname.c, lib/progreloc.c: * lib/pthread_sigmask.c, lib/quotearg.c, lib/readutmp.c: * lib/same.c, lib/signbitd.c, lib/signbitf.c, lib/signbitl.c: * lib/string-desc.c, lib/string.c, lib/string.in.h: * lib/unictype/3level.h, lib/unictype/3levelbit.h: * lib/uniname/uniname.c, lib/vc-mtime.c: Prefer memeq to memcmp when either will do. Do not make this change to files shared with glibc. Do not make the change to test files, at least not for now. * lib/gen-uni-tables.c (memeq): New static function, in same style. * modules/argmatch, modules/backupfile, modules/bcp47: * modules/boot-time, modules/csharpcomp, modules/csharpexec: * modules/file-has-acl: * modules/get_ppid_of, modules/get_progname_of: * modules/getlogin_r, modules/getprogname, modules/getumask: * modules/isnan, modules/mbchar, modules/mem-hash-map: * modules/memcoll, modules/progname, modules/pthread_sigmask: * modules/quotearg, modules/readutmp, modules/relocatable-prog: * modules/relocatable-prog-wrapper, modules/same, modules/signbit: * modules/string-desc, modules/stringeq, modules/uniname/uniname: * modules/vc-mtime: (Depends-on): Add stringeq.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/* Provide a working getlogin_r for systems which lack it.
Copyright (C) 2005-2007, 2010-2025 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Paul Eggert, Derek Price, and Bruno Haible. */
#include <config.h>
/* Specification. */
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "malloca.h"
#if defined _WIN32 && ! defined __CYGWIN__
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
/* Don't assume that UNICODE is not defined. */
# undef GetUserName
# define GetUserName GetUserNameA
#else
# if !HAVE_DECL_GETLOGIN
extern char *getlogin (void);
# endif
#endif
#if defined __linux__ || defined __ANDROID__
# include <fcntl.h>
# include <pwd.h>
# include <stdlib.h>
# include <string.h>
# include <sys/stat.h>
#endif
/* See unistd.in.h for documentation. */
int
getlogin_r (char *name, size_t size)
{
#undef getlogin_r
#if defined _WIN32 && ! defined __CYGWIN__
/* Native Windows platform. */
DWORD sz;
/* When size > 0x7fff, the doc says that GetUserName will fail.
Actually, on Windows XP SP3, it succeeds. But let's be safe,
for the sake of older Windows versions. */
if (size > 0x7fff)
size = 0x7fff;
sz = size;
if (!GetUserName (name, &sz))
{
if (GetLastError () == ERROR_INSUFFICIENT_BUFFER)
/* In this case, the doc says that sz contains the required size, but
actually, on Windows XP SP3, it contains 2 * the required size. */
return ERANGE;
else
return ENOENT;
}
return 0;
#elif defined __linux__ || defined __ANDROID__
/* Linux. */
{
/* Read the login uid from the /proc file system. */
int fd = open ("/proc/self/loginuid", O_RDONLY);
if (fd >= 0)
{
char buf[20 + 1];
int n = read (fd, buf, sizeof (buf) - 1);
if (n > 0)
{
buf[n] = '\0';
char *endptr;
unsigned long uid = strtoul (buf, &endptr, 10);
if (endptr == buf + n && uid != (uid_t) -1)
{
/* Convert the uid to a user name. */
struct passwd pbuf1;
char pbuf2[1024];
struct passwd *p;
if (getpwuid_r (uid, &pbuf1, pbuf2, sizeof (pbuf2), &p) == 0)
{
if (strlen (p->pw_name) < size)
{
strcpy (name, p->pw_name);
close (fd);
return 0;
}
return ERANGE;
}
}
}
close (fd);
}
}
{
/* Find the tty connected to the current process. */
char tty[1024];
if (ttyname_r (STDIN_FILENO, tty, sizeof (tty)) == 0)
{
/* We cannot use read_utmp here, since it is not multithread-safe. */
/* Fallback for systems which don't maintain an utmp database
or for ttys that are not recorded in that the utmp database:
Look at the owner of that tty. */
struct stat statbuf;
if (stat (tty, &statbuf) >= 0)
{
uid_t uid = statbuf.st_uid;
/* Convert the uid to a user name. */
struct passwd pbuf1;
char pbuf2[1024];
struct passwd *p;
if (getpwuid_r (uid, &pbuf1, pbuf2, sizeof (pbuf2), &p) == 0)
{
if (strlen (p->pw_name) < size)
{
strcpy (name, p->pw_name);
return 0;
}
return ERANGE;
}
}
}
}
/* ENOENT is a reasonable errno value if getlogin returns NULL. */
return ENOENT;
#elif HAVE_GETLOGIN_R
/* Platform with a getlogin_r() function. */
int ret = getlogin_r (name, size);
if (ret == 0)
{
const char *nul = memchr (name, '\0', size);
if (nul == NULL)
/* name contains a truncated result. */
return ERANGE;
if (size > 0 && nul == name + size - 1)
{
/* strlen(name) == size-1. Determine whether the untruncated result
would have had length size-1 or size. */
char *room = (char *) malloca (size + 1);
if (room == NULL)
return ENOMEM;
ret = getlogin_r (room, size + 1);
/* The untruncated result should be the same as in the first call. */
if (ret == 0 && !memeq (name, room, size))
/* The untruncated result would have been different. */
ret = ERANGE;
freea (room);
}
}
return ret;
#else
/* Platform with a getlogin() function. */
char *n;
size_t nlen;
errno = 0;
n = getlogin ();
if (!n)
/* ENOENT is a reasonable errno value if getlogin returns NULL. */
return (errno != 0 ? errno : ENOENT);
nlen = strlen (n);
if (size <= nlen)
return ERANGE;
memcpy (name, n, nlen + 1);
return 0;
#endif
}