training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

ex-6.2.c (948B)


      1 /*
      2  * Advanced Programming in the UNIX(r) Environment
      3  *
      4  * Exercise 6.2
      5 */
      6 
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 
     10 #include <paths.h>	/* Needed for _PATH_SHADOW */
     11 
     12 #if defined(_PATH_SHADOW)
     13 # include <shadow.h>
     14 #else
     15 # include <pwd.h>
     16 #endif
     17 
     18 int
     19 main(int argc, char *argv[])
     20 {
     21    char *user, *pass;
     22 
     23 #if defined(_PATH_SHADOW)
     24    struct spwd *p;
     25 #else
     26    struct passwd *p;
     27 #endif
     28 
     29    /* Check the arguments */
     30    if( argc != 2 ) {
     31       printf("Usage: %s <username>\n", argv[0]);
     32       return EXIT_FAILURE;
     33    }
     34 
     35    /* Get the informations */
     36 #if defined(_PATH_SHADOW)
     37    if( (p = getspnam(argv[1])) == NULL )
     38       return EXIT_FAILURE;
     39 
     40    user = p->sp_namp;
     41    pass = p->sp_pwdp;
     42 #else
     43    if( (p = getpwnam(argv[1])) == NULL )
     44       return EXIT_FAILURE;
     45 
     46    user = p->pw_name;
     47    pass = p->pw_passwd;
     48 #endif
     49 
     50    /* Show the informations */
     51    printf("Encrypted password for %s: %s\n", user, pass);
     52 
     53    return EXIT_SUCCESS;
     54 } /* eof main() */
     55