"Fossies" - the Fresh Open Source Software Archive

Member "nss_ldap-265/tests/testpw6.c" (6 Nov 2009, 3478 Bytes) of package /linux/privat/old/nss_ldap-265.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 
    2 /* $Id: testpw6.c,v 1.2 2006/01/10 18:06:39 lukeh Exp $ */
    3 
    4 /* This program just tests getpwent/getpwnam. You want to have nss_ldap
    5  * plugged in, so to speak, to test anything useful.
    6  */
    7 
    8 #include "config.h"
    9 
   10 #ifdef _REENTRANT
   11 #ifdef HAVE_PTHREAD_H
   12 #include <pthread.h>
   13 #else
   14 #include <thread.h>
   15 #endif /* _REENTRANT */
   16 
   17 #endif /* HAVE_PTHREAD_H */
   18 #include <stdio.h>
   19 #include <pwd.h>
   20 #include <sys/types.h>
   21 #include <stdlib.h>
   22 #include <unistd.h>
   23 #include <string.h>
   24 
   25 void test_passwd (void);
   26 void scan_passwd (void);
   27 
   28 int ARGC;
   29 char **ARGV;
   30 
   31 #define MAX_THREADS 8
   32 
   33 void
   34 main (int argc, char **argv)
   35 {
   36 #ifdef _REENTRANT
   37   int i;
   38 #ifdef HAVE_PTHREAD_H
   39   pthread_t tids[MAX_THREADS];
   40 #endif
   41 #endif
   42   pid_t pid;
   43   ARGC = argc;
   44   ARGV = argv;
   45 
   46   /* PRIME */
   47   scan_passwd();
   48 
   49    pid = fork();
   50    if (pid == 0) {
   51     printf("IN CHILD\n");
   52    } else {
   53     printf("IN PARENT\n");
   54    }
   55 
   56 #ifdef _REENTRANT
   57   for (i = 0; i < MAX_THREADS; i++)
   58     {
   59 #ifdef HAVE_PTHREAD_H
   60       pthread_create(&tids[i], NULL, test_passwd, NULL);
   61 #else
   62       thread_t tid;
   63       thr_create (NULL, 0, test_passwd, NULL, 0, &tid);
   64       thr_continue (tid);
   65 #endif /* HAVE_PTHREAD_H */
   66     }
   67 #ifdef HAVE_PTHREAD_H
   68   for (i = 0; i < MAX_THREADS; i++) pthread_join(tids[i], NULL);
   69 #else
   70   while (thr_join (NULL, NULL, NULL) == 0);
   71 #endif
   72 #else
   73   test_passwd ();
   74 #endif
   75   exit (0);
   76 }
   77 
   78 #ifdef _REENTRANT
   79 static void
   80 ret (int status)
   81 {
   82 #ifdef HAVE_PTHREAD_H
   83   pthread_exit(&status);
   84 #else
   85   thr_exit (&status);
   86 #endif
   87 }
   88 #else
   89 #define ret exit
   90 #endif
   91 
   92 void
   93 test_passwd (void)
   94 {
   95   struct passwd *pw;
   96   uid_t uid;
   97 #ifdef _REENTRANT
   98   char buf[1024];
   99   struct passwd pbuf;
  100 #endif
  101   int pid;
  102 
  103   printf (">>>>>> getpwnam(\"%s\")\n", ARGC > 1 ? ARGV[1] : "testuser");
  104 #ifdef _REENTRANT
  105 #if GETHOSTBYNAME_R_ARGS == 6
  106   if (getpwnam_r (ARGC > 1 ? ARGV[1] : "testuser", &pbuf, buf, sizeof (buf), &pw) != 0) pw = NULL;
  107 #else
  108   pw = getpwnam_r (ARGC > 1 ? ARGV[1] : "testuser", &pbuf, buf, sizeof (buf));
  109 #endif
  110 #else
  111   pw = getpwnam (ARGC > 1 ? ARGV[1] : "testuser");
  112 #endif
  113 
  114   if (!pw)
  115     ret (1);
  116 
  117   printf ("%s:%s:%d:%d:%s:%s:%s\n", pw->pw_name, pw->pw_passwd, pw->pw_uid,
  118       pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell);
  119   uid = pw->pw_uid;
  120 
  121 
  122   printf (">>>>>> getpwuid(%d)\n", uid);
  123 
  124 #ifdef _REENTRANT
  125 #if GETHOSTBYNAME_R_ARGS == 6
  126   if (getpwuid_r (uid, &pbuf, buf, sizeof(buf), &pw) != 0) pw = NULL;
  127 #else
  128   pw = getpwuid_r (uid, &pbuf, buf, sizeof (buf));
  129 #endif
  130 #else
  131   pw = getpwuid (uid);
  132 #endif
  133 
  134   if (!pw)
  135     ret (1);
  136 
  137 
  138   printf ("%s:%s:%d:%d:%s:%s:%s\n", pw->pw_name, pw->pw_passwd, pw->pw_uid,
  139       pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell);
  140 
  141   if (ARGC > 2 && !strcmp (ARGV[2], "no"))
  142     {
  143       printf (">>>>>> Enumeration skipped.\n");
  144     }
  145   else
  146     {
  147       printf (">>>>>> setpwent()\n");
  148       setpwent ();
  149 
  150       printf (">>>>>> getpwent()\n");
  151       scan_passwd ();
  152 
  153       printf (">>>>>> endpwent()\n");
  154       endpwent ();
  155     }
  156 
  157   ret (0);
  158 }
  159 
  160 void
  161 scan_passwd (void)
  162 {
  163   int i = 1;
  164   struct passwd *p;
  165 #ifdef _REENTRANT
  166   char buf[1024];
  167   struct passwd pbuf;
  168 #endif
  169 
  170 
  171 #ifdef _REENTRANT
  172 #if GETHOSTBYNAME_R_ARGS == 6
  173   while (getpwent_r (&pbuf, buf, sizeof (buf), &p) == 0 && p != NULL)
  174 #else
  175   while ((p = getpwent_r (&pbuf, buf, sizeof (buf))) != NULL)
  176 #endif
  177 #else
  178   while ((p = getpwent ()) != NULL)
  179 #endif
  180     {
  181       printf ("%s:%s:%d:%d:%s:%s:%s\n",
  182           p->pw_name,
  183           p->pw_passwd,
  184           p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
  185       i++;
  186     }
  187 }