c - How to use fgetpwent()? -


i trying list of users in system (linux, fedora). , i've heard function:fgetpwent 1 need mission. sad part didnt find documentation or example of how use function. if give me example, great, in advance.

no idea why ever have used it:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <crypt.h> #include <pwd.h> #include <sys/types.h>  char *testentry = "testread";  static void read_etc_passwd (void) {   struct passwd *pwd_entry = null;    int found = 0;    setpwent(); // go top of /etc/passwd    while (!found && (pwd_entry = getpwent())){     if (0 == strcmp (testentry, pwd_entry->pw_name)){         found = 1;     }   }   if (found) {     printf ("name = %s\nhome = %s\n", pwd_entry->pw_name,             pwd_entry->pw_dir);   } else {     puts("could not find entry looking for, or"          "some error occurred");   } }   void change_etc_passwd (void){   struct passwd *pwd = null;   file *pwd_fd = null;   file *pwd_new = null;   int result = 0;     pwd_fd = fopen ("/etc/passwd", "r");   pwd_new = fopen ("/tmp/passwd.neu", "a");    // assuming everthing went fine (bad idea)   while (pwd = fgetpwent (pwd_fd)){     if (0 == strcmp (pwd->pw_name, testentry)){       pwd->pw_passwd = crypt ("new_pwd", "aa");     }     result = putpwent(pwd, pwd_new);     if (result < 0){       fprintf (stderr, "failed write entry, giving up\n");       exit (exit_failure);     }   } }    int main (void) {   /* handling of /etc/passwd */   read_etc_passwd ();   change_etc_passwd();   return 0; } 

add error handling , may work without breaking ;-)


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -