auto_look.c
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 1,000 行 · 第 1/2 页
C
1,000 行
if (mfs->mfs_dir == NULL) return -1; mfs->mfs_subdir = strdup( *(subdir+1) ? subdir : ""); if (mfs->mfs_subdir == NULL) return -1; } getword(w, wq, lp, lq, ' '); } return 0;bad_entry: syslog(LOG_ERR, "bad entry in map %s \"%s\"", mapname, w); return 1;}free_mapent(me) struct mapent *me;{ struct mapfs *mfs; struct mapent *m; while (me) { while (me->map_fs) { mfs = me->map_fs; if (mfs->mfs_host) free(mfs->mfs_host); if (mfs->mfs_dir) free(mfs->mfs_dir); if (mfs->mfs_subdir) free(mfs->mfs_subdir); me->map_fs = mfs->mfs_next; free((char *)mfs); } if (me->map_root) free(me->map_root); if (me->map_mntpnt) free(me->map_mntpnt); if (me->map_mntopts) free(me->map_mntopts); m = me->map_next; free((char *)me); /* from all this misery */ me = m; }}/* * Gets the next token from the string "p" and copies * it into "w". Both "wq" and "w" are quote vectors * for "w" and "p". Delim is the character to be used * as a delimiter for the scan. A space means "whitespace". */voidgetword(w, wq, p, pq, delim) char *w, *wq, **p, **pq, delim;{ while ((delim == ' ' ? isspace(**p) : **p == delim) && **pq == ' ') (*p)++, (*pq)++; while (**p && !((delim == ' ' ? isspace(**p) : **p == delim) && **pq == ' ')) { *w++ = *(*p)++; *wq++ = *(*pq)++; } *w = '\0'; *wq = '\0';}/* * Performs text expansions in the string "pline". * "plineq" is the quote vector for "pline". * An identifier prefixed by "$" is replaced by the * corresponding environment variable string. A "&" * is replaced by the key string for the map entry. */voidmacro_expand(key, pline, plineq) char *key, *pline, *plineq;{ register char *p, *q; register char *bp, *bq; register char *s; char buffp[1024], buffq[1024]; char envbuf[64], *pe; int expand = 0; char *getenv(); p = pline ; q = plineq; bp = buffp ; bq = buffq; while (*p) { if (*p == '&' && *q == ' ') { /* insert key */ for (s = key ; *s ; s++) { *bp++ = *s; *bq++ = ' '; } expand++; p++; q++; continue; } if (*p == '$' && *q == ' ') { /* insert env var */ p++; q++; pe = envbuf; if (*p == '{') { p++ ; q++; while (*p && *p != '}') { *pe++ = *p++; q++; } if (*p) { p++ ; q++; } } else { while (*p && isalnum(*p)) { *pe++ = *p++; q++; } } *pe = '\0'; s = getenv(envbuf); if (s) { while (*s) { *bp++ = *s++; *bq++ = ' '; } expand++; } continue; } *bp++ = *p++; *bq++ = *q++; } if (!expand) return; *bp = '\0'; *bq = '\0'; (void) strcpy(pline , buffp); (void) strcpy(plineq, buffq);}/* Removes quotes from the string "str" and returns * the quoting information in "qbuf". e.g. * original str: 'the "quick brown" f\ox' * unquoted str: 'the quick brown fox' * qbuf: ' ^^^^^^^^^^^ ^ ' */voidunquote(str, qbuf) char *str, *qbuf;{ register int escaped, inquote, quoted; register char *ip, *bp, *qp; char buf[2048]; escaped = inquote = quoted = 0; for (ip = str, bp = buf, qp = qbuf ; *ip ; ip++) { if (!escaped) { if (*ip == '\\') { escaped = 1; quoted++; continue; } else if (*ip == '"') { inquote = !inquote; quoted++; continue; } } *bp++ = *ip; *qp++ = (inquote || escaped) ? '^' : ' '; escaped = 0; } *bp = '\0'; *qp = '\0'; if (quoted) (void) strcpy(str, buf);}struct mapent *getmapent_passwd(mapopts, login, cred) char *mapopts, *login; struct authunix_parms *cred;{ struct mapent *me; struct mapfs *mfs; struct passwd *pw; char buf[64]; char *rindex(); char *p; int c; if (login[0] == '~' && login[1] == 0) { pw = getpwuid(cred->aup_uid); if (pw) login = pw->pw_name; } else pw = getpwnam(login); if (pw == NULL) return ((struct mapent *)NULL); for (c = 0, p = pw->pw_dir ; *p ; p++) if (*p == '/') c++; if (c != 3) /* expect "/dir/host/user" */ return ((struct mapent *)NULL); me = (struct mapent *)malloc(sizeof *me); if (me == NULL) goto alloc_failed; bzero((char *) me, sizeof *me); me->map_mntopts = strdup(mapopts); if (me->map_mntopts == NULL) goto alloc_failed; mfs = (struct mapfs *)malloc(sizeof *mfs); if (mfs == NULL) goto alloc_failed; bzero((char *) mfs, sizeof *mfs); me->map_fs = mfs; (void) strcpy(buf, "/"); (void) strcat(buf, login); mfs->mfs_subdir = strdup(buf); p = rindex(pw->pw_dir, '/'); *p = '\0'; p = rindex(pw->pw_dir, '/'); mfs->mfs_host = strdup(p+1); if (mfs->mfs_host == NULL) goto alloc_failed; me->map_root = strdup(p); if (me->map_root == NULL) goto alloc_failed; me->map_mntpnt = strdup(""); if (me->map_mntpnt == NULL) goto alloc_failed; mfs->mfs_dir = strdup(pw->pw_dir); if (mfs->mfs_dir == NULL) goto alloc_failed; (void) endpwent(); return (me);alloc_failed: syslog(LOG_ERR, "Memory allocation failed: %m"); free_mapent(me); return ((struct mapent *)NULL);}struct mapent *getmapent_hosts(mapopts, host) char *mapopts, *host;{ struct mapent *me, *ms, *mp; struct mapfs *mfs; struct exports *ex = NULL; struct exports *exlist, *texlist, **texp, *exnext; struct groups *gr; enum clnt_stat clnt_stat, pingmount(), callrpc(); char name[MAXPATHLEN]; int elen; struct hostent *hp; struct sockaddr_in sin; CLIENT *cl; int s; hp = gethostbyname(host); if (hp == NULL) return ((struct mapent *)NULL); /* check for special case: host is me */ if ((*(u_long *)hp->h_addr) == my_addr.s_addr) { ms = (struct mapent *)malloc(sizeof *ms); if (ms == NULL) goto alloc_failed; bzero((char *) ms, sizeof *ms); ms->map_root = strdup(""); if (ms->map_root == NULL) goto alloc_failed; ms->map_mntpnt = strdup(""); if (ms->map_mntpnt == NULL) goto alloc_failed; ms->map_mntopts = strdup(""); if (ms->map_mntopts == NULL) goto alloc_failed; mfs = (struct mapfs *)malloc(sizeof *mfs); if (mfs == NULL) goto alloc_failed; bzero((char *) mfs, sizeof *mfs); mfs = (struct mapfs *)malloc(sizeof *mfs); if (mfs == NULL) goto alloc_failed; bzero((char *) mfs, sizeof *mfs); ms->map_fs = mfs; mfs->mfs_host = strdup(self); if (mfs->mfs_host == NULL) goto alloc_failed; mfs->mfs_addr = my_addr; mfs->mfs_dir = strdup("/"); if (mfs->mfs_dir == NULL) goto alloc_failed; mfs->mfs_subdir = strdup(""); if (mfs->mfs_subdir == NULL) goto alloc_failed; return (ms); } /* ping the null procedure of the remote mount daemon */ if (pingmount(*(struct in_addr *)hp->h_addr) != RPC_SUCCESS) return ((struct mapent *)NULL); (void) bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length); sin.sin_family = AF_INET; sin.sin_port=0; s = RPC_ANYSOCK; /* * First try tcp, then drop back to udp if * tcp is unavailable (an old version of mountd perhaps) * Using tcp is preferred because it can handle * arbitrarily long export lists. */ cl = clnttcp_create(&sin, MOUNTPROG, MOUNTVERS, &s, 0, 0); /* get export list of host */ if (cl == NULL) { clnt_stat = callrpc(host, MOUNTPROG, MOUNTVERS, MOUNTPROC_EXPORT, xdr_void, 0, xdr_exports, &ex); } else { clnt_stat = (enum clnt_stat) clnt_call(cl, MOUNTPROC_EXPORT, xdr_void, 0, xdr_exports, &ex, TIMEOUT); } if (clnt_stat != RPC_SUCCESS) { syslog(LOG_ERR, "%s: exports: %s",host,clnt_sperrno(clnt_stat)); if (cl != NULL) { (void) close(s); clnt_destroy(cl); } return ((struct mapent *)NULL); } if (ex == NULL) return ((struct mapent *)NULL); /* now sort by length of names - to get mount order right */ exlist = ex; texlist = NULL; for (ex = exlist; ex; ex = exnext) { exnext = ex->ex_next; ex->ex_next = 0; elen = strlen(ex->ex_name); /* check netgroup list */ if (ex->ex_groups) { for ( gr = ex->ex_groups ; gr ; gr = gr->g_next) { if (strcmp(gr->g_name, self) == 0 || in_netgroup(gr->g_name, self, mydomain)) break; } if (gr == NULL) { freeex(ex); continue; } } for (texp = &texlist; *texp; texp = &((*texp)->ex_next)) if (elen < strlen((*texp)->ex_name)) break; ex->ex_next = *texp; *texp = ex; } exlist = texlist; /* Now create a mapent from the export list */ ms = NULL; me = NULL; for (ex = exlist; ex; ex = ex->ex_next) { mp = me; me = (struct mapent *)malloc(sizeof *me); if (me == NULL) goto alloc_failed; bzero((char *) me, sizeof *me); if (ms == NULL) ms = me; else mp->map_next = me; (void) strcpy(name, "/"); (void) strcat(name, host); me->map_root = strdup(name); if (me->map_root == NULL) goto alloc_failed; if (strcmp(ex->ex_name, "/") == 0) me->map_mntpnt = strdup(""); else me->map_mntpnt = strdup(ex->ex_name); if (me->map_mntpnt == NULL) goto alloc_failed; me->map_mntopts = strdup(mapopts); if (me->map_mntopts == NULL) goto alloc_failed; mfs = (struct mapfs *)malloc(sizeof *mfs); if (mfs == NULL) goto alloc_failed; bzero((char *) mfs, sizeof *mfs); me->map_fs = mfs; mfs->mfs_host = strdup(host); if (mfs->mfs_host == NULL) goto alloc_failed; mfs->mfs_addr = *(struct in_addr *)hp->h_addr; mfs->mfs_dir = strdup(ex->ex_name); if (mfs->mfs_dir == NULL) goto alloc_failed; mfs->mfs_subdir = strdup(""); if (mfs->mfs_subdir == NULL) goto alloc_failed; } freeex(exlist); if (cl != NULL) { (void) close(s); clnt_destroy(cl); } return (ms);alloc_failed: syslog(LOG_ERR, "Memory allocation failed: %m"); free_mapent(ms); freeex(exlist); if (cl != NULL) { (void) close(s); clnt_destroy(cl); } return ((struct mapent *)NULL);}#define MAXGRPLIST 512/* * Check cached netgroup info before calling innetgr(). * Two lists are maintained here: * membership groups and non-membership groups. */intin_netgroup(group, hostname, domain) char *group, *hostname, *domain;{ static char grplist[MAXGRPLIST+1]; static char nogrplist[MAXGRPLIST+1]; char key[256]; char *ypline = NULL; int yplen; register char *gr, *p; static time_t last; static int cache_time = 300; /* 5 min */ if (time_now > last + cache_time) { last = time_now; grplist[0] = '\0'; nogrplist[0] = '\0'; (void) strcpy(key, hostname); (void) strcat(key, "."); (void) strcat(key, domain); if (yp_match(domain, "netgroup.byhost", key, strlen(key), &ypline, &yplen) == 0) { (void) strncpy(grplist, ypline, MIN(yplen, MAXGRPLIST)); grplist[MIN(yplen, MAXGRPLIST)] = '\0'; free(ypline); } } for (gr = grplist ; *gr ; gr = p ) { for (p = gr ; *p && *p != ',' ; p++) ; if (strncmp(group, gr, p - gr) == 0) return 1; if (*p == ',') p++; } for (gr = nogrplist ; *gr ; gr = p ) { for (p = gr ; *p && *p != ',' ; p++) ; if (strncmp(group, gr, p - gr) == 0) return 0; if (*p == ',') p++; } if (innetgr(group, hostname, (char *)NULL, domain)) { if (strlen(grplist)+1+strlen(group) > MAXGRPLIST) return 1; if (*grplist) (void) strcat(grplist, ","); (void) strcat(grplist, group); return 1; } else { if (strlen(nogrplist)+1+strlen(group) > MAXGRPLIST) return 1; if (*nogrplist) (void) strcat(nogrplist, ","); (void) strcat(nogrplist, group); return 0; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?