ru.po: Corrections from Evgeny Bulgakov <bgav@netvision.net.il>
[midnight-commander.git] / vfs / names.c
blob0f454d5e1c46d93393400d357bce64bb08fe0d12
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 user and/or group names.
22 * This file should be modified for non-unix systems to do something
23 * reasonable.
26 #include <config.h>
27 #include <sys/types.h>
28 #include <string.h>
29 #include <unistd.h>
30 #define TAR_NAMES
31 #include "tar.h"
32 #include "names.h"
34 #include <stdio.h>
35 #include <pwd.h>
36 #include <grp.h>
38 #ifndef TUNMLEN
39 #define TUNMLEN 256
40 #endif
41 #ifndef TGNMLEN
42 #define TGNMLEN 256
43 #endif
45 static int saveuid = -993;
46 static char saveuname[TUNMLEN];
47 static int my_uid = -993;
49 static int savegid = -993;
50 static char savegname[TGNMLEN];
51 static int my_gid = -993;
53 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
54 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
57 * Look up a user or group name from a uid/gid, maintaining a cache.
58 * FIXME, for now it's a one-entry cache.
59 * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
61 * This is ifdef'd because on Suns, it drags in about 38K of "yellow
62 * pages" code, roughly doubling the program size. Thanks guys.
64 void finduname (char *uname, int uid)
66 struct passwd *pw;
67 #ifndef HAVE_GETPWUID
68 extern struct passwd *getpwuid ();
69 #endif
71 if (uid != saveuid) {
72 saveuid = uid;
73 saveuname[0] = '\0';
74 pw = getpwuid (uid);
75 if (pw)
76 strncpy (saveuname, pw->pw_name, TUNMLEN);
78 strncpy (uname, saveuname, TUNMLEN);
81 int finduid (char *uname)
83 struct passwd *pw;
84 extern struct passwd *getpwnam ();
86 if (uname[0] != saveuname[0]/* Quick test w/o proc call */
87 ||0 != strncmp (uname, saveuname, TUNMLEN)) {
88 strncpy (saveuname, uname, TUNMLEN);
89 pw = getpwnam (uname);
90 if (pw) {
91 saveuid = pw->pw_uid;
92 } else {
93 saveuid = myuid;
96 return saveuid;
100 void findgname (char *gname, int gid)
102 struct group *gr;
103 #ifndef HAVE_GETGRGID
104 extern struct group *getgrgid ();
105 #endif
107 if (gid != savegid) {
108 savegid = gid;
109 savegname[0] = '\0';
110 (void) setgrent ();
111 gr = getgrgid (gid);
112 if (gr)
113 strncpy (savegname, gr->gr_name, TGNMLEN);
115 (void) strncpy (gname, savegname, TGNMLEN);
119 int findgid (char *gname)
121 struct group *gr;
122 extern struct group *getgrnam ();
124 if (gname[0] != savegname[0]/* Quick test w/o proc call */
125 ||0 != strncmp (gname, savegname, TUNMLEN)) {
126 strncpy (savegname, gname, TUNMLEN);
127 gr = getgrnam (gname);
128 if (gr) {
129 savegid = gr->gr_gid;
130 } else {
131 savegid = mygid;
134 return savegid;