getgrent.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 830 行 · 第 1/2 页
C
830 行
setgrent_local(); return(NULL); } gp = interpret(line1, strlen(line1)); switch(line1[0]) { case '+': goto again_local; case '-': goto again_local; default: return(gp); }}struct group *getgrent_bind(){ char bindbuf[64]; struct group *gp = NULL; sprintf(bindbuf, "group-%d", svc_getgrbind);#ifdef DEBUG fprintf(stderr, "getgrent_bind(%s)\n", bindbuf);#endif DEBUG if ((gp = getgrnam_bind(bindbuf)) == NULL) return(NULL); svc_getgrbind++; return(gp);}struct group *getgrent_yp(){ char line1[BUFSIZ+1]; static struct group *savegp, *gp;#ifdef DEBUG fprintf (stderr,"getgrent_yp\n");#endif DEBUG if (grf == NULL && (grf = fopen(GROUP, "r")) == NULL && (domain = yellowup(1)) == NULL) return(NULL);again: if (yp) { gp = interpretwithsave(yp, yplen, savegp); free(yp); getnextfromyellow(); if (onminuslist(gp)) goto again; else return(gp); } else if (fgets(line1, BUFSIZ, grf) == NULL) return(NULL); gp = interpret(line1, strlen(line1)); switch(line1[0]) { case '+': if (strcmp(gp->gr_name, "+") == 0) { getfirstfromyellow(); savegp = save(gp); goto again; } savegp = save(gp); gp = getnamefromyellow(gp->gr_name+1, savegp); if (gp == NULL) goto again; else if (onminuslist(gp)) goto again; else return(gp); case '-': addtominuslist(gp->gr_name+1); goto again; default: if (onminuslist(gp)) goto again; return(gp); }}static char *grskip(p,c) register char *p; register c;{ while(*p && *p != c && *p != '\n') ++p; if(*p) *p++ = 0; return(p);}static struct group *interpret(val, len) char *val;{ register char *p, **q; static struct group gp; static char line[BUFSIZ+1]; strncpy(line, val, len); p = line; line[len] = '\n'; line[len+1] = 0; gp.gr_name = p; gp.gr_passwd = p = grskip(p,':'); gp.gr_gid = atoi(p = grskip(p,':')); gp.gr_mem = gr_mem; p = grskip(p,':'); grskip(p,'\n'); q = gr_mem; while(*p){ if (q < &gr_mem[MAXGRP-1]) *q++ = p; p = grskip(p,','); } *q = NULL; return(&gp);}staticfreeminuslist() { struct list *ls; for (ls = minuslist; ls != NULL; ls = ls->nxt) { free(ls->name); free(ls); } minuslist = NULL;}static struct group *interpretwithsave(val, len, savegp) char *val; struct group *savegp;{ struct group *gp; gp = interpret(val, len); if (savegp->gr_passwd && *savegp->gr_passwd) gp->gr_passwd = savegp->gr_passwd; if (savegp->gr_mem && *savegp->gr_mem) gp->gr_mem = savegp->gr_mem; return(gp);}staticonminuslist(gp) struct group *gp;{ struct list *ls; register char *nm; nm = gp->gr_name; for (ls = minuslist; ls != NULL; ls = ls->nxt) if (strcmp(ls->name, nm) == 0) return(1); return(0);}staticgetnextfromyellow(){ int reason = 0; char *key = NULL; int keylen = 0; if (reason = yp_next(domain, "group.byname", oldyp, oldyplen, &key, &keylen, &yp, &yplen)) {#ifdef DEBUG fprintf(stderr, "reason yp_next failed is %d\n", reason);#endif yp = NULL; } if (oldyp) free(oldyp); oldyp = key; oldyplen = keylen;}staticgetfirstfromyellow(){ int reason = 0; char *key = NULL; int keylen = 0; if (reason = yp_first(domain, "group.byname", &key, &keylen, &yp, &yplen)) {#ifdef DEBUG fprintf(stderr, "reason yp_first failed is %d\n", reason);#endif yp = NULL; } if (oldyp) free(oldyp); oldyp = key; oldyplen = keylen;}static struct group *getnamefromyellow(name, savegp) char *name; struct group *savegp;{ struct group *gp; int reason; char *val = NULL; int vallen = 0; if (reason = yp_match(domain, "group.byname", name, strlen(name), &val, &vallen)) {#ifdef DEBUG fprintf(stderr, "reason yp_next failed is %d\n", reason);#endif return(NULL); } else { gp = interpret(val, vallen); free(val); if (savegp->gr_passwd && *savegp->gr_passwd) gp->gr_passwd = savegp->gr_passwd; if (savegp->gr_mem && *savegp->gr_mem) gp->gr_mem = savegp->gr_mem; return(gp); }}staticaddtominuslist(name) char *name;{ struct list *ls; char *buf; ls = (struct list *)malloc(sizeof(struct list)); buf = (char *)malloc(strlen(name) + 1); strcpy(buf, name); ls->name = buf; ls->nxt = minuslist; minuslist = ls;}/* * save away psswd, gr_mem fields, which are the only * ones which can be specified in a local + entry to override the * value in the yellow pages */static struct group *save(gp) struct group *gp;{ static struct group *sv; char *gr_mem[MAXGRP]; char **av, **q; int lnth; /* * free up stuff from last time around */ if (sv) { for (av = sv->gr_mem; *av != NULL; av++) { if (q >= &gr_mem[MAXGRP-1]) break; free(*q); q++; } free(sv->gr_passwd); free(sv->gr_mem); free(sv); } sv = (struct group *)malloc(sizeof(struct group)); sv->gr_passwd = (char *)malloc(strlen(gp->gr_passwd) + 1); strcpy(sv->gr_passwd, gp->gr_passwd); q = gr_mem; for (av = gp->gr_mem; *av != NULL; av++) { if (q >= &gr_mem[MAXGRP-1]) break; *q = (char *)malloc(strlen(*av) + 1); strcpy(*q, *av); q++; } *q = 0; lnth = (sizeof (char *)) * (q - gr_mem + 1); sv->gr_mem = (char **)malloc(lnth); bcopy(gr_mem, sv->gr_mem, lnth); return(sv);}staticmatchname(line1, gpp, name) char line1[]; struct group **gpp; char *name;{ struct group *savegp; struct group *gp = *gpp; switch(line1[0]) { case '+': if (strcmp(gp->gr_name, "+") == 0) { savegp = save(gp); gp = getnamefromyellow(name, savegp); if (gp) { *gpp = gp; return(1); } else return(0); } if (strcmp(gp->gr_name+1, name) == 0) { savegp = save(gp); gp = getnamefromyellow(gp->gr_name+1, savegp); if (gp) { *gpp = gp; return(1); } else return(0); } break; case '-': if (strcmp(gp->gr_name+1, name) == 0) { *gpp = NULL; return(1); } break; default: if (strcmp(gp->gr_name, name) == 0) return(1); } return(0);}staticmatchgid(line1, gpp, gid) char line1[]; struct group **gpp;{ struct group *savegp; struct group *gp = *gpp; switch(line1[0]) { case '+': if (strcmp(gp->gr_name, "+") == 0) { savegp = save(gp); gp = getgidfromyellow(gid, savegp); if (gp) { *gpp = gp; return(1); } else return(0); } savegp = save(gp); gp = getnamefromyellow(gp->gr_name+1, savegp); if (gp && gp->gr_gid == gid) { *gpp = gp; return(1); } else return(0); case '-': if (gid == gidof(gp->gr_name+1)) { *gpp = NULL; return(1); } break; default: if (gp->gr_gid == gid) return(1); } return(0);}staticgidof(name) char *name;{ struct group *gp; struct group nullgp; nullgp = NULLGP; gp = getnamefromyellow(name, &nullgp); if (gp) return(gp->gr_gid); else return(MAXINT);}static struct group *getgidfromyellow(gid, savegp) int gid; struct group *savegp;{ struct group *gp; int reason = 0; char *val = NULL; int vallen = 0; char gidstr[20]; sprintf(gidstr, "%d", gid); if (reason = yp_match(domain, "group.bygid", gidstr, strlen(gidstr), &val, &vallen)) {#ifdef DEBUG fprintf(stderr, "reason yp_next failed is %d\n", reason);#endif return(NULL); } else { gp = interpret(val, vallen); free(val); if (savegp->gr_passwd && *savegp->gr_passwd) gp->gr_passwd = savegp->gr_passwd; if (savegp->gr_mem && *savegp->gr_mem) gp->gr_mem = savegp->gr_mem; return(gp); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?