bagpiper@csvax.caltech.EDU (08/10/87)
In uudecode.c there is a function called getpwnam. (struct passwd getpwnam) Since I am not using a true unix (primix 2.0 yuch) I need to know what this function does so that I can try to rewrite it. Could anybody please either send me a functional speck of getpwnam, or, could somebody send me a piece of code that does what unix getpwname does, but on a PR1ME 9955. P.S. I realize that this is not really a 'c' question, but I don't read unix.*. Thanx, Michael Hunter UUCP : ....{seismo, rutgers, ames}!cit-vax!oxy!bagpiper Box 241 ARPA : oxy!bagpiper@csvax.caltech.edu Occidental College BITNET: oxy!bagpiper@hamlet.bitnet Los Angeles, CA 90041 CSNET : oxy!bagpiper%csvax.caltech.edu@relay.cs.net
maw@auc.UUCP (Michael A. Walker) (08/16/87)
In article <8725@brl-adm.ARPA>, oxy!bagpiper@csvax.caltech.EDU (Michael Paul Hunter) writes: > > In uudecode.c there is a function called getpwnam. (struct passwd getpwnam) > Since I am not using a true unix (primix 2.0 yuch) I need to know what this > function does so that I can try to rewrite it. Could anybody please either > send me a functional speck of getpwnam, or, could somebody send me a piece > of code that does what unix getpwname does, but on a PR1ME 9955. The function getpwnam() returns a pointer to the entry in the password file for a specified login name. The following is an example of how it might be used in a program(probably only in UN*X enviornments: $cat sample.c #include <stdio.h> #include <pwd.h> main(argc, argv) int argc; char *argv[]; { struct passwd *getpwnam(), *pwentry; if (argc < 2) { fprintf(stderr, "Usage: user name expected\n"); exit (0); } pwentry = getpwnam(argv[1]); if(pwentry == (struct passwd *)NULL ){ fprint(stderr,"Can't find %s in pwd file\n", argv[1]; exit (0); } else printf("%d\n",pwentry-pw_uid); } The above program would produce an output like this: $sample foo 127 $ where foo is the specified login. However, to mimic getpwnam(), one can very simply write a program that will search the password file and retrieve the desired information. Here's an example that retrieves all of the users login name: #include <stdio.h> /* * Field number of name in passwd */ #define FIELD 4 main() { FILE *fp; /*declares a pointer to a file*/ char buf[120], name[80]; int m1, m2, count; /* Open password file */ if((fp = fopen("/etc/passwd", "r")) == NULL){ perror("Opening /etc/passwd"); /*print this error message to stderr*/ exit(1); /* Abort program */ } printf("List of Users\n"); /* * Read in a line from passwd file */ while(fgets(buf, 119, fp) != NULL){ /* read in a line of code at a time until the end of the file*/ m1 = 0; count = 0; /* * Search for fourth colon (beginning of name field) */ while(count < FIELD && buf[m1] != NULL){ if(buf[m1] == ':') ++count; ++m1; } m2 = m1 + 1; /* * Search for next colon (end of name field) */ while(buf[m2] != ':' && buf[m1] != NULL) ++m2; /* * Terminate string */ buf[m2] = NULL; /* * Copy to name variable */ strcpy(name, buf+m1); /* copy the context of buffer to name */ printf("%s\n", name); } printf("Done\n"); } This code can be easily modified get the login name of one user. I believe this code is generic enough to run in any environment. At most, you would have to change the name and directory of the password file(from /etc/passwd to /yoursystem/password_file), and the strcpy function if it is not available on your system. I am not very well versed in primix, but if you don't have the fopen(), perror(), and fgets() functions, very good implementations of these are given in Kernighan & Ritchie's[1] C programming book. I hope this helps. -----mike REFERENCE 1. Brian W. Kernighan, Dennis M. Ritchie, "The C Programming Language," Prentice-Hall, Englewood Cliffs, NJ., 1978. -- <-----------------------------------------------------------------------------> < Michael A. Walker | Operator | AUC Computational Center > < gatech!gt-cmmsr!auc!maw | <<<<>>>> | "There is strength in diversity." > <----------------------------------------------------------------------------->
maw@auc.UUCP (Michael A. Walker) (08/16/87)
In article <32131@auc.UUCP>, maw@auc.UUCP (Michael A. Walker) writes: > In article <8725@brl-adm.ARPA>, oxy!bagpiper@csvax.caltech.EDU (Michael Paul Hunter) writes: > > > > In uudecode.c there is a function called getpwnam. (struct passwd getpwnam) > > Since I am not using a true unix (primix 2.0 yuch) I need to know what this > > function does so that I can try to rewrite it. Could anybody please either > > send me a functional speck of getpwnam, or, could somebody send me a piece > > of code that does what unix getpwname does, but on a PR1ME 9955. > > The function getpwnam() returns a pointer to the entry in the password > file for a specified login name. The following is an example of how it might > be used in a program(probably only in UN*X enviornments: > ----cut out stuff--- > > However, to mimic getpwnam(), one can very simply write a program that will > search the password file and retrieve the desired information. Here's an > example that retrieves all of the users login name: ^^^^^ ^^^^^ ^^^^ This is not true. The second program I wrote in the last message, returns the contents of the pw_comments part of the /etc/passwd file( field 5 ). To return the all of the users login names, you would have to define FIELD to be 0 instead of 4. Also, you can get other data from the password file by changing the FIELD definition to a number one less than the actual field you want. Please forgive me of the error. -----mike P. S. Both of my messages were written with the following structure of the password file in mind(delimited by :'s): struct passwd { char *pw_name; char *pw_passwd; int pw_uid; int pw_gid; char *pw_age; char *pw_comment; char *pw_gecos; char *pw_dir; char *pw_shell; }; -- <-----------------------------------------------------------------------------> < Michael A. Walker | Operator | AUC Computational Center > < gatech!gt-cmmsr!auc!maw | <<<<>>>> | "There is strength in diversity." > <----------------------------------------------------------------------------->