~zulcss/samba/server-dailies-3.4

« back to all changes in this revision

Viewing changes to testsuite/nsswitch/getent.c

  • Committer: Chuck Short
  • Date: 2010-09-28 20:38:39 UTC
  • Revision ID: zulcss@ubuntu.com-20100928203839-pgjulytsi9ue63x1
Initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Cut down version of getent which only returns passwd and group database
 
2
   entries and seems to compile on most systems without too much fuss.
 
3
   Original copyright notice below. */
 
4
 
 
5
/* Copyright (c) 1998, 1999, 2000 Free Software Foundation, Inc.
 
6
   This file is part of the GNU C Library.
 
7
   Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
 
8
 
 
9
   The GNU C Library is free software; you can redistribute it and/or
 
10
   modify it under the terms of the GNU Lesser General Public License as
 
11
   published by the Free Software Foundation; either version 3 of the
 
12
   License, or (at your option) any later version.
 
13
 
 
14
   The GNU C Library is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
   Library General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU Lesser General Public
 
20
   License along with the GNU C Library; see the file COPYING.LIB.  If not, 
 
21
   see <http://www.gnu.org/licenses/>. */
 
22
 
 
23
#include <stdio.h>
 
24
#include <pwd.h>
 
25
#include <grp.h>
 
26
 
 
27
group_keys (int number, char *key[])
 
28
{
 
29
  int result = 0;
 
30
  int i;
 
31
 
 
32
  for (i = 0; i < number; ++i)
 
33
    {
 
34
      struct group *grp;
 
35
 
 
36
      if (isdigit (key[i][0]))
 
37
        grp = getgrgid (atol (key[i]));
 
38
      else
 
39
        grp = getgrnam (key[i]);
 
40
 
 
41
      if (grp == NULL)
 
42
        result = 2;
 
43
      else
 
44
        print_group (grp);
 
45
    }
 
46
 
 
47
  return result;
 
48
}
 
49
 
 
50
passwd_keys (int number, char *key[])
 
51
{
 
52
  int result = 0;
 
53
  int i;
 
54
 
 
55
  for (i = 0; i < number; ++i)
 
56
    {
 
57
      struct passwd *pwd;
 
58
 
 
59
      if (isdigit (key[i][0]))
 
60
        pwd = getpwuid (atol (key[i]));
 
61
      else
 
62
        pwd = getpwnam (key[i]);
 
63
 
 
64
      if (pwd == NULL)
 
65
        result = 2;
 
66
      else
 
67
        print_passwd (pwd);
 
68
    }
 
69
 
 
70
  return result;
 
71
}
 
72
 
 
73
print_group (struct group *grp)
 
74
{
 
75
  unsigned int i = 0;
 
76
 
 
77
  printf ("%s:%s:%ld:", grp->gr_name ? grp->gr_name : "",
 
78
          grp->gr_passwd ? grp->gr_passwd : "",
 
79
          (unsigned long)grp->gr_gid);
 
80
 
 
81
  while (grp->gr_mem[i] != NULL)
 
82
    {
 
83
      fputs (grp->gr_mem[i], stdout);
 
84
      ++i;
 
85
      if (grp->gr_mem[i] != NULL)
 
86
        fputs (",", stdout);
 
87
    }
 
88
  fputs ("\n", stdout);
 
89
}
 
90
 
 
91
print_passwd (struct passwd *pwd)
 
92
{
 
93
  printf ("%s:%s:%ld:%ld:%s:%s:%s\n",
 
94
          pwd->pw_name ? pwd->pw_name : "",
 
95
          pwd->pw_passwd ? pwd->pw_passwd : "",
 
96
          (unsigned long)pwd->pw_uid,
 
97
          (unsigned long)pwd->pw_gid,
 
98
          pwd->pw_gecos ? pwd->pw_gecos : "",
 
99
          pwd->pw_dir ? pwd->pw_dir : "",
 
100
          pwd->pw_shell ? pwd->pw_shell : "");
 
101
}
 
102
 
 
103
int main(int argc, char **argv)
 
104
{
 
105
  switch(argv[1][0])
 
106
    {
 
107
    case 'g': /* group */
 
108
      if (strcmp (argv[1], "group") == 0)
 
109
        {
 
110
          if (argc == 2)
 
111
            {
 
112
              struct group *grp;
 
113
 
 
114
              setgrent ();
 
115
              while ((grp = getgrent()) != NULL)
 
116
                print_group (grp);
 
117
              endgrent ();
 
118
            }
 
119
          else
 
120
            return group_keys (argc - 2, &argv[2]);
 
121
        }
 
122
      else
 
123
        goto error;
 
124
      break;
 
125
 
 
126
   case 'p': /* passwd, protocols */
 
127
      if (strcmp (argv[1], "passwd") == 0)
 
128
        {
 
129
          if (argc == 2)
 
130
            {
 
131
              struct passwd *pwd;
 
132
 
 
133
              setpwent ();
 
134
              while ((pwd = getpwent()) != NULL)
 
135
                print_passwd (pwd);
 
136
              endpwent ();
 
137
            }
 
138
          else
 
139
            return passwd_keys (argc - 2, &argv[2]);
 
140
        }
 
141
      else
 
142
        goto error;
 
143
      break;
 
144
    default:
 
145
    error:
 
146
      fprintf (stderr, "Unknown database: %s\n", argv[1]);
 
147
      return 1;
 
148
    }
 
149
  return 0;
 
150
}