|
|
1.1 ! root 1: /* ! 2: * Coherent I/O Library. ! 3: * Routines to get the group file entry. ! 4: * (searches by next entry, name or numerical id). ! 5: */ ! 6: ! 7: #include <stdio.h> ! 8: #include <grp.h> ! 9: ! 10: #define field(x) { x=cp; while (*cp++); } ! 11: #define NGRPLINE 256 ! 12: #define NGRMEM 64 /* Maximum no. of members in a group */ ! 13: #define GRFILE "/etc/group" ! 14: ! 15: static char grline[NGRPLINE]; ! 16: static char *grmems[NGRMEM+1]; ! 17: static struct group gr; ! 18: static FILE *grfile = { NULL }; ! 19: ! 20: struct group * ! 21: getgrnam(name) ! 22: char *name; ! 23: { ! 24: register struct group *grp; ! 25: ! 26: setgrent(); ! 27: while ((grp = getgrent()) != NULL) ! 28: if (streq(name, grp->gr_name)) ! 29: return (grp); ! 30: return (NULL); ! 31: } ! 32: ! 33: struct group * ! 34: getgrgid(gid) ! 35: { ! 36: register struct group *grp; ! 37: ! 38: setgrent(); ! 39: while ((grp = getgrent()) != NULL) ! 40: if (gid == grp->gr_gid) ! 41: return (grp); ! 42: return (NULL); ! 43: } ! 44: ! 45: struct group * ! 46: getgrent() ! 47: { ! 48: register char *cp, *xp; ! 49: ! 50: if (grfile == NULL) ! 51: if ((grfile = fopen(GRFILE, "r")) == NULL) ! 52: return (NULL); ! 53: cp = grline; ! 54: { ! 55: register int c; ! 56: ! 57: while ((c = getc(grfile))!=EOF && c!='\n') { ! 58: if (c == ':') ! 59: c = '\0'; ! 60: if (cp < &grline[NGRPLINE-1]) ! 61: *cp++ = c; ! 62: } ! 63: if (c == EOF) ! 64: return (NULL); ! 65: } ! 66: *cp = '\0'; ! 67: cp = grline; ! 68: field(gr.gr_name); ! 69: field(gr.gr_passwd); ! 70: field(xp); ! 71: gr.gr_gid = atoi(xp); ! 72: { ! 73: register char **mp; ! 74: ! 75: gr.gr_mem = mp = grmems; ! 76: for (;;) { ! 77: if (*cp == '\0') ! 78: break; ! 79: *mp++ = cp; ! 80: while (*cp!=',' && *cp!='\0') ! 81: cp++; ! 82: if (*cp == ',') ! 83: *cp++ = '\0'; ! 84: } ! 85: *mp = NULL; ! 86: } ! 87: return (&gr); ! 88: } ! 89: ! 90: setgrent() ! 91: { ! 92: if (grfile != NULL) ! 93: rewind(grfile); ! 94: } ! 95: ! 96: endgrent() ! 97: { ! 98: if (grfile != NULL) { ! 99: fclose(grfile); ! 100: grfile = NULL; ! 101: } ! 102: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.