Another small fix. :-(
[midnight-commander.git] / vfs / names.c
blob7228f312469dd05a7f1f7508dfe8020cd08874f5
1 /* Look up user and/or group names.
2 Copyright (C) 1988, 1992 Free Software Foundation
4 From GNU Tar.
6 GNU Tar is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published
8 by the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Tar is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Library General Public License for more details.
16 * Namespace: finduname, finduid, findgname, findgid.
20 * Look up a user or group name from a uid/gid, maintaining a cache.
21 * FIXME, for now it's a one-entry cache.
22 * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
23 * This file should be modified for non-unix systems to do something
24 * reasonable.
27 #include <config.h>
28 #include <sys/types.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <pwd.h>
33 #include <grp.h>
35 #include "names.h"
37 #ifndef TUNMLEN
38 #define TUNMLEN 256
39 #endif
40 #ifndef TGNMLEN
41 #define TGNMLEN 256
42 #endif
44 static int saveuid = -993;
45 static char saveuname[TUNMLEN];
46 static int my_uid = -993;
48 static int savegid = -993;
49 static char savegname[TGNMLEN];
50 static int my_gid = -993;
52 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
53 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
55 int
56 finduid (char *uname)
58 struct passwd *pw;
60 if (uname[0] != saveuname[0] /* Quick test w/o proc call */
61 ||0 != strncmp (uname, saveuname, TUNMLEN)) {
62 strncpy (saveuname, uname, TUNMLEN);
63 pw = getpwnam (uname);
64 if (pw) {
65 saveuid = pw->pw_uid;
66 } else {
67 saveuid = myuid;
70 return saveuid;
73 int
74 findgid (char *gname)
76 struct group *gr;
78 if (gname[0] != savegname[0] /* Quick test w/o proc call */
79 ||0 != strncmp (gname, savegname, TUNMLEN)) {
80 strncpy (savegname, gname, TUNMLEN);
81 gr = getgrnam (gname);
82 if (gr) {
83 savegid = gr->gr_gid;
84 } else {
85 savegid = mygid;
88 return savegid;