passwd program

This is a discussion on passwd program within the C Programming forums, part of the General Programming Boards category; hello all, I have been given the task of creating a passwd program that uses the 'struct passwd' structure, in ...

  1. #1
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    passwd program

    hello all, I have been given the task of creating a passwd program that uses the 'struct passwd' structure, in order to 1) read to first ten values of /etc/passwd file and display the userID and the userName. 2) Also once this is done, modify the program, so that it will store the first ten entry's from the password file into an array of structures. 3)print out the usernames and UID's this time using the array.

    I have overcome the first part of the question (1) by using this:
    Code:
     #include<stdio.h>
    #include<pwd.h>   
    main()
    {
    int i;	
    struct passwd *pwd;
     setpwent(); 
    	
    for(i=0;i<10;i++) 		 
          {
    	pwd=getpwent();
            printf("%s %d \n", (*pwd).pw_name,(*pwd).pw_uid); 
          }
    endpwent();	
    }
    }
    For part two, do need to declare another structure before main, with a character pointer for the username value and an int for the UID. like so:

    Code:
    typedef struct store
    char *ptr_string;
    int x;
    } point;
    Or do i have to use the struct passwd *ptr strucutre somehow? Need help on how to store the UID and Username into an array of structures please.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need an array of structures, which you get just like you get an array of anything else, by tacking [10] onto the end of the name of the variable when you declare it.

  3. #3
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    i.e. point[10]?

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah

  5. #5
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    thanks but using which structure - a new one declared before main or using the 'struct passwd *ptr' one already declared?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Using the structure that getpwent is going to return.

    Edit: Or do you mean can you re-use the variable? You could, I suppose; I don't remember the semantics of getpwent to remember whether or not it returns a pointer to a static variable. If so, you would need to use an extra pointer (which you already have) and a separate array. If not, you could just make an array of pointers.
    Last edited by tabstop; 01-07-2009 at 11:54 AM.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    http://linux.about.com/library/cmd/blcmdl3_getpwent.htm
    Doesn't have anything static, so you can use an array of struct passwd to do your job

  8. #8
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    it seems i'm not allowed to alter struct passwd as it only accepts these:
    http://www.mkssoftware.com/docs/man5...t_passwd.5.asp

    so i think i have to create an alternative structure.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course you're not allowed to alter struct password. Since you don't want to alter struct password, that's just fine and dandy. You want an array of them, you don't want to change them.

  10. #10
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Code:
    #include<stdio.h>
    #include<pwd.h> 
    typedef struct store
    {
    char *ptr_string;
    int x;
    } point; /*point data type*/
    main()
    {
    	int i;	
          struct passwd *pwd; 
    point array[10];
          setpwent(); 	   	
    for(i=0;i<10;i++)
    {
    pwd=getpwent();
    printf("%s %d \n", (*pwd).pw_name,(*pwd).pw_uid); 
    }
          endpwent();	
    }
    i've created the 'point' datatype here and an array of 10 elements named 'array' , but am not sure how to store the values (UID and Username) from the structure into it. The normal method would be using point array [10] = {{and then the values} [etc}} - but i cant seem to store the values of pwd into it. Or am i going completely the wrong way about this. I'm completely stuck now.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You move things around with
    Code:
    where_you_want_it = what_you_want_in_it;
    You know that what you want in it is (*pwd).pw_name and (*pwd).pw_uid. Now you just have to decide where you want it. Presumably you want it in array[i].ptr_string and array[i].x (you should have a better name than x for that, though). (Oh and you should look into using -> as well.)

  12. #12
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    thanks once again tabstop

    thank once again tabstop - following your instructions I now have a working program and compiles with no errors.

    Code:
    #include<stdio.h>
    #include<pwd.h>   		
    
    typedef struct store
    {
    char *ptr_string;
    int user;
    } point; /*point data type*/
    
    main()
    {
    point array[10];
    int i;	
    struct passwd *pwd; 
    	setpwent(); 	   
    	
    for(i=0;i<10;i++)
    {
    pwd=getpwent();
    array[i].ptr_string = (*pwd).pw_name;
    array[i].user = (*pwd).pw_uid ;
    printf("%s %d \n", array[i].ptr_string, array[i].user);
     
    }
    
          endpwent();	
    
    }
    I took you advice and looked up '->' operator and basically found that is a pointer to a struct to differentiate between structure pointers and other pointers. However tabstop I used the '->' operator in the exact place of the '.' (dot)in the printf statement and I received this error:
    Error - invalid type argument of '->'.

    Nonetheless, I think my program now satisfies the requirements set out in the question.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I wonder where you looked up -> to see that, then. -> doesn't (really?) differentiate between structure pointers and other pointers, as such; what it does is does the dereferencing and accesses the subfield at the same time. So this
    Code:
    (*pwd).pw_uid
    becomes this
    Code:
    pwd->pw_uid
    (I suppose the differentiation comes from the fact that the left side must point to something that has a subfield (struct or union) not just any old thing.)

  14. #14
    cus
    cus is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    i found it in 'c programming - in easy steps' and was labelled in the margin as a 'hot tip'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21