Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!bloom-beacon!gatech!gt-cmmsr!auc!maw From: maw@auc.UUCP (Michael A. Walker) Newsgroups: comp.lang.c Subject: Re: getpwnam Message-ID: <32131@auc.UUCP> Date: Sun, 16-Aug-87 13:34:42 EDT Article-I.D.: auc.32131 Posted: Sun Aug 16 13:34:42 1987 Date-Received: Sun, 16-Aug-87 22:49:53 EDT References: <8725@brl-adm.ARPA> Organization: Atlanta University, Atlanta, GA Lines: 125 Summary: getpwnam: the lazy way(sometimes) 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 #include 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 /* * 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." > <----------------------------------------------------------------------------->