⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 popt.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING   file accompanying popt source distributions, available from   ftp://ftp.redhat.com/pub/code/popt */#include "system.h"#include "findme.h"#include "poptint.h"#ifndef HAVE_STRERRORstatic char * strerror(int errno) {    extern int sys_nerr;    extern char * sys_errlist[];    if ((0 <= errno) && (errno < sys_nerr))	return sys_errlist[errno];    else	return POPT_("unknown errno");}#endifvoid poptSetExecPath(poptContext con, const char * path, int allowAbsolute) {    if (con->execPath) xfree(con->execPath);    con->execPath = xstrdup(path);    con->execAbsolute = allowAbsolute;}static void invokeCallbacks(poptContext con, const struct poptOption * table,			    int post) {    const struct poptOption * opt = table;    poptCallbackType cb;    while (opt->longName || opt->shortName || opt->arg) {	if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {	    invokeCallbacks(con, opt->arg, post);	} else if (((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) &&		   ((!post && (opt->argInfo & POPT_CBFLAG_PRE)) ||		    ( post && (opt->argInfo & POPT_CBFLAG_POST)))) {	    cb = (poptCallbackType)opt->arg;	    cb(con, post ? POPT_CALLBACK_REASON_POST : POPT_CALLBACK_REASON_PRE,	       NULL, NULL, opt->descrip);	}	opt++;    }}poptContext poptGetContext(const char * name, int argc, const char ** argv,			   const struct poptOption * options, int flags) {    poptContext con = malloc(sizeof(*con));    memset(con, 0, sizeof(*con));    con->os = con->optionStack;    con->os->argc = argc;    con->os->argv = argv;    con->os->argb = NULL;    if (!(flags & POPT_CONTEXT_KEEP_FIRST))	con->os->next = 1;			/* skip argv[0] */    con->leftovers = calloc( (argc + 1), sizeof(char *) );    con->options = options;    con->aliases = NULL;    con->numAliases = 0;    con->flags = flags;    con->execs = NULL;    con->numExecs = 0;    con->finalArgvAlloced = argc * 2;    con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );    con->execAbsolute = 1;    con->arg_strip = NULL;    if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))	con->flags |= POPT_CONTEXT_POSIXMEHARDER;    if (name)	con->appName = strcpy(malloc(strlen(name) + 1), name);    invokeCallbacks(con, con->options, 0);    return con;}static void cleanOSE(struct optionStackEntry *os){    if (os->nextArg) {	xfree(os->nextArg);	os->nextArg = NULL;    }    if (os->argv) {	xfree(os->argv);	os->argv = NULL;    }    if (os->argb) {	PBM_FREE(os->argb);	os->argb = NULL;    }}void poptResetContext(poptContext con) {    int i;    while (con->os > con->optionStack) {	cleanOSE(con->os--);    }    if (con->os->argb) {	PBM_FREE(con->os->argb);	con->os->argb = NULL;    }    con->os->currAlias = NULL;    con->os->nextCharArg = NULL;    con->os->nextArg = NULL;    con->os->next = 1;			/* skip argv[0] */    con->numLeftovers = 0;    con->nextLeftover = 0;    con->restLeftover = 0;    con->doExec = NULL;    for (i = 0; i < con->finalArgvCount; i++) {	if (con->finalArgv[i]) {	    xfree(con->finalArgv[i]);	    con->finalArgv[i] = NULL;	}    }    con->finalArgvCount = 0;    if (con->arg_strip) {	PBM_FREE(con->arg_strip);	con->arg_strip = NULL;    }}/* Only one of longName, shortName may be set at a time */static int handleExec(poptContext con, char * longName, char shortName) {    int i;    i = con->numExecs - 1;    if (longName) {	while (i >= 0 && (!con->execs[i].longName ||	    strcmp(con->execs[i].longName, longName))) i--;    } else {	while (i >= 0 &&	    con->execs[i].shortName != shortName) i--;    }    if (i < 0) return 0;    if (con->flags & POPT_CONTEXT_NO_EXEC)	return 1;    if (con->doExec == NULL) {	con->doExec = con->execs + i;	return 1;    }    /* We already have an exec to do; remember this option for next       time 'round */    if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {	con->finalArgvAlloced += 10;	con->finalArgv = realloc(con->finalArgv,			sizeof(*con->finalArgv) * con->finalArgvAlloced);    }    i = con->finalArgvCount++;    {	char *s  = malloc((longName ? strlen(longName) : 0) + 3);	if (longName)	    sprintf(s, "--%s", longName);	else	    sprintf(s, "-%c", shortName);	con->finalArgv[i] = s;    }    return 1;}/* Only one of longName, shortName may be set at a time */static int handleAlias(poptContext con, const char * longName, char shortName,		       /*@keep@*/ const char * nextCharArg) {    int i;    if (con->os->currAlias && con->os->currAlias->longName && longName &&	!strcmp(con->os->currAlias->longName, longName))	return 0;    if (con->os->currAlias && shortName &&	    shortName == con->os->currAlias->shortName)	return 0;    i = con->numAliases - 1;    if (longName) {	while (i >= 0 && (!con->aliases[i].longName ||	    strcmp(con->aliases[i].longName, longName))) i--;    } else {	while (i >= 0 &&	    con->aliases[i].shortName != shortName) i--;    }    if (i < 0) return 0;    if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)	return POPT_ERROR_OPTSTOODEEP;    if (nextCharArg && *nextCharArg)	con->os->nextCharArg = nextCharArg;    con->os++;    con->os->next = 0;    con->os->stuffed = 0;    con->os->nextArg = NULL;    con->os->nextCharArg = NULL;    con->os->currAlias = con->aliases + i;    poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,		&con->os->argc, &con->os->argv);    con->os->argb = NULL;    return 1;}static void execCommand(poptContext con) {    const char ** argv;    int pos = 0;    const char * script = con->doExec->script;    argv = malloc(sizeof(*argv) *			(6 + con->numLeftovers + con->finalArgvCount));    if (!con->execAbsolute && strchr(script, '/')) return;    if (!strchr(script, '/') && con->execPath) {	char *s = malloc(strlen(con->execPath) + strlen(script) + 2);	sprintf(s, "%s/%s", con->execPath, script);	argv[pos] = s;    } else {	argv[pos] = script;    }    pos++;    argv[pos] = findProgramPath(con->os->argv[0]);    if (argv[pos]) pos++;    argv[pos++] = ";";    memcpy(argv + pos, con->finalArgv, sizeof(*argv) * con->finalArgvCount);    pos += con->finalArgvCount;    if (con->numLeftovers) {	argv[pos++] = "--";	memcpy(argv + pos, con->leftovers, sizeof(*argv) * con->numLeftovers);	pos += con->numLeftovers;    }    argv[pos++] = NULL;#ifdef __hpux    setresuid(getuid(), getuid(),-1);#else/* * XXX " ... on BSD systems setuid() should be preferred over setreuid()" * XXX 	sez' Timur Bakeyev <mc@bat.ru> * XXX	from Norbert Warmuth <nwarmuth@privat.circular.de> */#if defined(HAVE_SETUID)    setuid(getuid());#elif defined (HAVE_SETREUID)    setreuid(getuid(), getuid()); /*hlauer: not portable to hpux9.01 */#else    ; /* Can't drop privileges */#endif#endif    execvp(argv[0], (char *const *)argv);}/*@observer@*/ static const struct poptOption *findOption(const struct poptOption * table, const char * longName,    char shortName,    /*@out@*/ poptCallbackType * callback, /*@out@*/ const void ** callbackData,    int singleDash){    const struct poptOption * opt = table;    const struct poptOption * opt2;    const struct poptOption * cb = NULL;    /* This happens when a single - is given */    if (singleDash && !shortName && !*longName)	shortName = '-';    while (opt->longName || opt->shortName || opt->arg) {	if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {	    opt2 = findOption(opt->arg, longName, shortName, callback,			      callbackData, singleDash);	    if (opt2) {		if (*callback && !*callbackData)		    *callbackData = opt->descrip;		return opt2;	    }	} else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) {	    cb = opt;	} else if (longName && opt->longName &&		   (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) &&		   !strcmp(longName, opt->longName)) {	    break;	} else if (shortName && shortName == opt->shortName) {	    break;	}	opt++;    }    if (!opt->longName && !opt->shortName) return NULL;    *callbackData = NULL;    *callback = NULL;    if (cb) {	*callback = (poptCallbackType)cb->arg;	if (!(cb->argInfo & POPT_CBFLAG_INC_DATA))	    *callbackData = cb->descrip;    }    return opt;}static const char *findNextArg(poptContext con, unsigned argx, int delete){    struct optionStackEntry * os = con->os;    const char * arg;    do {	int i;	arg = NULL;	while (os->next == os->argc && os > con->optionStack) os--;	if (os->next == os->argc && os == con->optionStack) break;	for (i = os->next; i < os->argc; i++) {	    if (os->argb && PBM_ISSET(i, os->argb)) continue;	    if (*os->argv[i] == '-') continue;	    if (--argx > 0) continue;	    arg = os->argv[i];	    if (delete) {		if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);		PBM_SET(i, os->argb);	    }	    break;	}	if (os > con->optionStack) os--;    } while (arg == NULL);    return arg;}static /*@only@*/ const char * expandNextArg(poptContext con, const char * s){    const char *a;    size_t alen;    char *t, *te;    size_t tn = strlen(s) + 1;    char c;    te = t = malloc(tn);;    while ((c = *s++) != '\0') {	switch (c) {#if 0	/* XXX can't do this */	case '\\':	/* escape */	    c = *s++;	    break;#endif	case '!':	    if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))		break;	    if ((a = findNextArg(con, 1, 1)) == NULL)		break;	    s += 3;	    alen = strlen(a);	    tn += alen;	    *te = '\0';	    t = realloc(t, tn);	    te = t + strlen(t);	    strncpy(te, a, alen); te += alen;	    continue;	    /*@notreached@*/ break;	default:	    break;	}	*te++ = c;    }    *te = '\0';    t = realloc(t, strlen(t)+1);	/* XXX memory leak, hard to plug */    return t;}static void poptStripArg(poptContext con, int which){    if(con->arg_strip == NULL) {	con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);    }    PBM_SET(which, con->arg_strip);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -