]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/islocal.c
2 * islocal.c - returns true if user is registered in the local
3 * /etc/passwd file. Written by Alvaro Martinez Echevarria,
4 * alvaro@enano.etsit.upm.es, to allow peaceful coexistence with yp. Nov 94.
6 * Hacked a bit by poe@daimi.aau.dk
7 * See also ftp://ftp.daimi.aau.dk/pub/linux/poe/admutil*
9 * Hacked by Peter Breitenlohner, peb@mppmu.mpg.de,
10 * to distinguish user names where one is a prefix of the other,
11 * and to use "pathnames.h". Oct 5, 96.
13 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
14 * - added Native Language Support
16 * 2008-04-06 James Youngman, jay@gnu.org
17 * - Completely rewritten to remove assumption that /etc/passwd
18 * lines are < 1024 characters long. Also added unit tests.
27 #include "pathnames.h"
29 static int is_local_in_file(const char *user
, const char *filename
)
36 if (NULL
== (f
= fopen(filename
, "r")))
41 while ((chin
= getc(f
)) != EOF
) {
43 /* Looking for the start of the next line. */
45 /* Start matching username at the next char. */
51 if (0 == user
[match
]) {
54 /* next line has no test coverage,
55 * but it is just an optimisation
59 /* we read a whole username, but it
60 * is the wrong user. Skip to the
64 } else if ('\n' == chin
) {
65 /* This line contains no colon; it's
66 * malformed. No skip since we are already
67 * at the start of the next line. */
69 } else if (chin
!= user
[match
]) {
70 /* username does not match. */
81 int is_local(const char *user
)
84 if ((rv
= is_local_in_file(user
, _PATH_PASSWD
)) < 0) {
86 fprintf(stderr
, _("Failed to open %s for reading, exiting."),
95 int main(int argc
, char *argv
[])
98 fprintf(stderr
, "usage: %s <passwdfile> <username> [...]\n",
103 for (i
= 2; i
< argc
; i
++) {
104 const int rv
= is_local_in_file(argv
[i
], argv
[1]);
109 printf("%d:%s\n", rv
, argv
[i
]);