本文整理汇总了C++中xcalloc函数的典型用法代码示例。如果您正苦于以下问题:C++ xcalloc函数的具体用法?C++ xcalloc怎么用?C++ xcalloc使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了xcalloc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: split_argv
/*
* Splits 's' into an argument vector. Handles quoted string and basic
* escape characters (\\, \", \'). Caller must free the argument vector
* and its members.
*/
static int
split_argv(const char *s, int *argcp, char ***argvp)
{
int r = SSH_ERR_INTERNAL_ERROR;
int argc = 0, quote, i, j;
char *arg, **argv = xcalloc(1, sizeof(*argv));
*argvp = NULL;
*argcp = 0;
for (i = 0; s[i] != '\0'; i++) {
/* Skip leading whitespace */
if (s[i] == ' ' || s[i] == '\t')
continue;
/* Start of a token */
quote = 0;
if (s[i] == '\\' &&
(s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\'))
i++;
else if (s[i] == '\'' || s[i] == '"')
quote = s[i++];
argv = xreallocarray(argv, (argc + 2), sizeof(*argv));
arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1);
argv[argc] = NULL;
/* Copy the token in, removing escapes */
for (j = 0; s[i] != '\0'; i++) {
if (s[i] == '\\') {
if (s[i + 1] == '\'' ||
s[i + 1] == '\"' ||
s[i + 1] == '\\') {
i++; /* Skip '\' */
arg[j++] = s[i];
} else {
/* Unrecognised escape */
arg[j++] = s[i];
}
} else if (quote == 0 && (s[i] == ' ' || s[i] == '\t'))
break; /* done */
else if (quote != 0 && s[i] == quote)
break; /* done */
else
arg[j++] = s[i];
}
if (s[i] == '\0') {
if (quote != 0) {
/* Ran out of string looking for close quote */
r = SSH_ERR_INVALID_FORMAT;
goto out;
}
break;
}
}
/* Success */
*argcp = argc;
*argvp = argv;
argc = 0;
argv = NULL;
r = 0;
out:
if (argc != 0 && argv != NULL) {
for (i = 0; i < argc; i++)
free(argv[i]);
free(argv);
}
return r;
}
示例2: load_subtree
static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,
struct int_node *node, unsigned int n)
{
unsigned char object_sha1[20];
unsigned int prefix_len;
void *buf;
struct tree_desc desc;
struct name_entry entry;
int len, path_len;
unsigned char type;
struct leaf_node *l;
buf = fill_tree_descriptor(&desc, subtree->val_sha1);
if (!buf)
die("Could not read %s for notes-index",
sha1_to_hex(subtree->val_sha1));
prefix_len = subtree->key_sha1[19];
assert(prefix_len * 2 >= n);
memcpy(object_sha1, subtree->key_sha1, prefix_len);
while (tree_entry(&desc, &entry)) {
path_len = strlen(entry.path);
len = get_sha1_hex_segment(entry.path, path_len,
object_sha1 + prefix_len, 20 - prefix_len);
if (len < 0)
goto handle_non_note; /* entry.path is not a SHA1 */
len += prefix_len;
/*
* If object SHA1 is complete (len == 20), assume note object
* If object SHA1 is incomplete (len < 20), and current
* component consists of 2 hex chars, assume note subtree
*/
if (len <= 20) {
type = PTR_TYPE_NOTE;
l = (struct leaf_node *)
xcalloc(sizeof(struct leaf_node), 1);
hashcpy(l->key_sha1, object_sha1);
hashcpy(l->val_sha1, entry.sha1);
if (len < 20) {
if (!S_ISDIR(entry.mode) || path_len != 2)
goto handle_non_note; /* not subtree */
l->key_sha1[19] = (unsigned char) len;
type = PTR_TYPE_SUBTREE;
}
if (note_tree_insert(t, node, n, l, type,
combine_notes_concatenate))
die("Failed to load %s %s into notes tree "
"from %s",
type == PTR_TYPE_NOTE ? "note" : "subtree",
sha1_to_hex(l->key_sha1), t->ref);
}
continue;
handle_non_note:
/*
* Determine full path for this non-note entry:
* The filename is already found in entry.path, but the
* directory part of the path must be deduced from the subtree
* containing this entry. We assume here that the overall notes
* tree follows a strict byte-based progressive fanout
* structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not
* e.g. 4/36 fanout). This means that if a non-note is found at
* path "dead/beef", the following code will register it as
* being found on "de/ad/beef".
* On the other hand, if you use such non-obvious non-note
* paths in the middle of a notes tree, you deserve what's
* coming to you ;). Note that for non-notes that are not
* SHA1-like at the top level, there will be no problems.
*
* To conclude, it is strongly advised to make sure non-notes
* have at least one non-hex character in the top-level path
* component.
*/
{
char non_note_path[PATH_MAX];
char *p = non_note_path;
const char *q = sha1_to_hex(subtree->key_sha1);
int i;
for (i = 0; i < prefix_len; i++) {
*p++ = *q++;
*p++ = *q++;
*p++ = '/';
}
strcpy(p, entry.path);
add_non_note(t, non_note_path, entry.mode, entry.sha1);
}
}
free(buf);
}
示例3: threaded_lazy_init_name_hash
static void threaded_lazy_init_name_hash(
struct index_state *istate)
{
int nr_each;
int k_start;
int t;
struct lazy_entry *lazy_entries;
struct lazy_dir_thread_data *td_dir;
struct lazy_name_thread_data *td_name;
k_start = 0;
nr_each = DIV_ROUND_UP(istate->cache_nr, lazy_nr_dir_threads);
lazy_entries = xcalloc(istate->cache_nr, sizeof(struct lazy_entry));
td_dir = xcalloc(lazy_nr_dir_threads, sizeof(struct lazy_dir_thread_data));
td_name = xcalloc(1, sizeof(struct lazy_name_thread_data));
init_dir_mutex();
/*
* Phase 1:
* Build "istate->dir_hash" using n "dir" threads (and a read-only index).
*/
for (t = 0; t < lazy_nr_dir_threads; t++) {
struct lazy_dir_thread_data *td_dir_t = td_dir + t;
td_dir_t->istate = istate;
td_dir_t->lazy_entries = lazy_entries;
td_dir_t->k_start = k_start;
k_start += nr_each;
if (k_start > istate->cache_nr)
k_start = istate->cache_nr;
td_dir_t->k_end = k_start;
if (pthread_create(&td_dir_t->pthread, NULL, lazy_dir_thread_proc, td_dir_t))
die("unable to create lazy_dir_thread");
}
for (t = 0; t < lazy_nr_dir_threads; t++) {
struct lazy_dir_thread_data *td_dir_t = td_dir + t;
if (pthread_join(td_dir_t->pthread, NULL))
die("unable to join lazy_dir_thread");
}
/*
* Phase 2:
* Iterate over all index entries and add them to the "istate->name_hash"
* using a single "name" background thread.
* (Testing showed it wasn't worth running more than 1 thread for this.)
*
* Meanwhile, finish updating the parent directory ref-counts for each
* index entry using the current thread. (This step is very fast and
* doesn't need threading.)
*/
td_name->istate = istate;
td_name->lazy_entries = lazy_entries;
if (pthread_create(&td_name->pthread, NULL, lazy_name_thread_proc, td_name))
die("unable to create lazy_name_thread");
lazy_update_dir_ref_counts(istate, lazy_entries);
if (pthread_join(td_name->pthread, NULL))
die("unable to join lazy_name_thread");
cleanup_dir_mutex();
free(td_name);
free(td_dir);
free(lazy_entries);
}
示例4: kex_choose_conf
static void
kex_choose_conf(Kex *kex)
{
Newkeys *newkeys;
char **my, **peer;
char **cprop, **sprop;
int nenc, nmac, ncomp;
u_int mode, ctos, need;
int first_kex_follows, type;
#ifdef NONE_CIPHER_ENABLED
int auth_flag;
#endif
my = kex_buf2prop(&kex->my, NULL);
peer = kex_buf2prop(&kex->peer, &first_kex_follows);
if (kex->server) {
cprop=peer;
sprop=my;
} else {
cprop=my;
sprop=peer;
}
/* Check whether server offers roaming */
if (!kex->server) {
char *roaming;
roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL);
if (roaming) {
kex->roaming = 1;
xfree(roaming);
}
}
/* Algorithm Negotiation */
#ifdef NONE_CIPHER_ENABLED
auth_flag = packet_get_authentication_state();
debug ("AUTH STATE is %d", auth_flag);
#endif
for (mode = 0; mode < MODE_MAX; mode++) {
newkeys = xcalloc(1, sizeof(*newkeys));
kex->newkeys[mode] = newkeys;
ctos = (!kex->server && mode == MODE_OUT) ||
(kex->server && mode == MODE_IN);
nenc = ctos ? PROPOSAL_ENC_ALGS_CTOS : PROPOSAL_ENC_ALGS_STOC;
nmac = ctos ? PROPOSAL_MAC_ALGS_CTOS : PROPOSAL_MAC_ALGS_STOC;
ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
choose_enc (&newkeys->enc, cprop[nenc], sprop[nenc]);
choose_mac (&newkeys->mac, cprop[nmac], sprop[nmac]);
choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
#ifdef NONE_CIPHER_ENABLED
debug("REQUESTED ENC.NAME is '%s'", newkeys->enc.name);
if (strcmp(newkeys->enc.name, "none") == 0) {
debug("Requesting NONE. Authflag is %d", auth_flag);
if (auth_flag == 1)
debug("None requested post authentication.");
else
fatal("Pre-authentication none cipher requests "
"are not allowed.");
}
#endif
debug("kex: %s %s %s %s",
ctos ? "client->server" : "server->client",
newkeys->enc.name,
newkeys->mac.name,
newkeys->comp.name);
}
choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
need = 0;
for (mode = 0; mode < MODE_MAX; mode++) {
newkeys = kex->newkeys[mode];
if (need < newkeys->enc.key_len)
need = newkeys->enc.key_len;
if (need < newkeys->enc.block_size)
need = newkeys->enc.block_size;
if (need < newkeys->mac.key_len)
need = newkeys->mac.key_len;
}
/* XXX need runden? */
kex->we_need = need;
/* ignore the next message if the proposals do not match */
if (first_kex_follows && !proposals_match(my, peer) &&
!(datafellows & SSH_BUG_FIRSTKEX)) {
type = packet_read();
debug2("skipping next packet (type %u)", type);
}
kex_prop_free(my);
kex_prop_free(peer);
}
示例5: test_xcalloc_fail
static void
test_xcalloc_fail() {
expect_string( mock_die, output, "Out of memory, xcalloc failed" );
expect_assert_failure( xcalloc( 1, 1 ) );
}
示例6: lpx_warm_up
int lpx_warm_up(LPX *lp)
{ int m, n, j, k, ret, type, stat, p_stat, d_stat;
double lb, ub, prim, dual, tol_bnd, tol_dj, dir;
double *row_prim, *row_dual, *col_prim, *col_dual, sum;
m = lpx_get_num_rows(lp);
n = lpx_get_num_cols(lp);
/* reinvert the basis matrix, if necessary */
if (lpx_is_b_avail(lp))
ret = LPX_E_OK;
else
{ if (m == 0 || n == 0)
{ ret = LPX_E_EMPTY;
goto done;
}
#if 0
ret = lpx_invert(lp);
switch (ret)
{ case 0:
ret = LPX_E_OK;
break;
case 1:
case 2:
ret = LPX_E_SING;
goto done;
case 3:
ret = LPX_E_BADB;
goto done;
default:
xassert(ret != ret);
}
#else
switch (glp_factorize(lp))
{ case 0:
ret = LPX_E_OK;
break;
case GLP_EBADB:
ret = LPX_E_BADB;
goto done;
case GLP_ESING:
case GLP_ECOND:
ret = LPX_E_SING;
goto done;
default:
xassert(lp != lp);
}
#endif
}
/* allocate working arrays */
row_prim = xcalloc(1+m, sizeof(double));
row_dual = xcalloc(1+m, sizeof(double));
col_prim = xcalloc(1+n, sizeof(double));
col_dual = xcalloc(1+n, sizeof(double));
/* compute primal basic solution components */
lpx_eval_b_prim(lp, row_prim, col_prim);
/* determine primal status of basic solution */
tol_bnd = 3.0 * lpx_get_real_parm(lp, LPX_K_TOLBND);
p_stat = LPX_P_FEAS;
for (k = 1; k <= m+n; k++)
{ if (k <= m)
{ type = lpx_get_row_type(lp, k);
lb = lpx_get_row_lb(lp, k);
ub = lpx_get_row_ub(lp, k);
prim = row_prim[k];
}
else
{ type = lpx_get_col_type(lp, k-m);
lb = lpx_get_col_lb(lp, k-m);
ub = lpx_get_col_ub(lp, k-m);
prim = col_prim[k-m];
}
if (type == LPX_LO || type == LPX_DB || type == LPX_FX)
{ /* variable x[k] has lower bound */
if (prim < lb - tol_bnd * (1.0 + fabs(lb)))
{ p_stat = LPX_P_INFEAS;
break;
}
}
if (type == LPX_UP || type == LPX_DB || type == LPX_FX)
{ /* variable x[k] has upper bound */
if (prim > ub + tol_bnd * (1.0 + fabs(ub)))
{ p_stat = LPX_P_INFEAS;
break;
}
}
}
/* compute dual basic solution components */
lpx_eval_b_dual(lp, row_dual, col_dual);
/* determine dual status of basic solution */
tol_dj = 3.0 * lpx_get_real_parm(lp, LPX_K_TOLDJ);
dir = (lpx_get_obj_dir(lp) == LPX_MIN ? +1.0 : -1.0);
d_stat = LPX_D_FEAS;
for (k = 1; k <= m+n; k++)
{ if (k <= m)
{ stat = lpx_get_row_stat(lp, k);
dual = row_dual[k];
}
else
{ stat = lpx_get_col_stat(lp, k-m);
dual = col_dual[k-m];
}
//.........这里部分代码省略.........
示例7: lpx_transform_row
int lpx_transform_row(LPX *lp, int len, int ind[], double val[])
{ int i, j, k, m, n, t, lll, *iii;
double alfa, *a, *aB, *rho, *vvv;
if (!lpx_is_b_avail(lp))
xfault("lpx_transform_row: LP basis is not available\n");
m = lpx_get_num_rows(lp);
n = lpx_get_num_cols(lp);
/* unpack the row to be transformed to the array a */
a = xcalloc(1+n, sizeof(double));
for (j = 1; j <= n; j++) a[j] = 0.0;
if (!(0 <= len && len <= n))
xfault("lpx_transform_row: len = %d; invalid row length\n",
len);
for (t = 1; t <= len; t++)
{ j = ind[t];
if (!(1 <= j && j <= n))
xfault("lpx_transform_row: ind[%d] = %d; column index out o"
"f range\n", t, j);
if (val[t] == 0.0)
xfault("lpx_transform_row: val[%d] = 0; zero coefficient no"
"t allowed\n", t);
if (a[j] != 0.0)
xfault("lpx_transform_row: ind[%d] = %d; duplicate column i"
"ndices not allowed\n", t, j);
a[j] = val[t];
}
/* construct the vector aB */
aB = xcalloc(1+m, sizeof(double));
for (i = 1; i <= m; i++)
{ k = lpx_get_b_info(lp, i);
/* xB[i] is k-th original variable */
xassert(1 <= k && k <= m+n);
aB[i] = (k <= m ? 0.0 : a[k-m]);
}
/* solve the system B'*rho = aB to compute the vector rho */
rho = aB, lpx_btran(lp, rho);
/* compute coefficients at non-basic auxiliary variables */
len = 0;
for (i = 1; i <= m; i++)
{ if (lpx_get_row_stat(lp, i) != LPX_BS)
{ alfa = - rho[i];
if (alfa != 0.0)
{ len++;
ind[len] = i;
val[len] = alfa;
}
}
}
/* compute coefficients at non-basic structural variables */
iii = xcalloc(1+m, sizeof(int));
vvv = xcalloc(1+m, sizeof(double));
for (j = 1; j <= n; j++)
{ if (lpx_get_col_stat(lp, j) != LPX_BS)
{ alfa = a[j];
lll = lpx_get_mat_col(lp, j, iii, vvv);
for (t = 1; t <= lll; t++) alfa += vvv[t] * rho[iii[t]];
if (alfa != 0.0)
{ len++;
ind[len] = m+j;
val[len] = alfa;
}
}
}
xassert(len <= n);
xfree(iii);
xfree(vvv);
xfree(aB);
xfree(a);
return len;
}
示例8: dolist
/*ARGSUSED*/
void
dolist(Char **v, struct command *c)
{
Char **globbed;
int i, k;
struct stat st;
USE(c);
if (*++v == NULL) {
struct Strbuf word = Strbuf_INIT;
Strbuf_terminate(&word);
cleanup_push(&word, Strbuf_cleanup);
(void) t_search(&word, LIST, TW_ZERO, 0, STRNULL, 0);
cleanup_until(&word);
return;
}
v = glob_all_or_error(v);
globbed = v;
cleanup_push(globbed, blk_cleanup);
for (k = 0; v[k] != NULL && v[k][0] != '-'; k++)
continue;
if (v[k]) {
/*
* We cannot process a flag therefore we let ls do it right.
*/
Char *lspath;
struct command *t;
struct wordent cmd, *nextword, *lastword;
Char *cp;
struct varent *vp;
if (setintr) {
pintr_disabled++;
cleanup_push(&pintr_disabled, disabled_cleanup);
}
if (seterr) {
xfree(seterr);
seterr = NULL;
}
lspath = STRls;
STRmCF[1] = 'C';
STRmCF[3] = '\0';
/* Look at listflags, to add -A to the flags, to get a path
of ls if necessary */
if ((vp = adrof(STRlistflags)) != NULL && vp->vec != NULL &&
vp->vec[0] != STRNULL) {
if (vp->vec[1] != NULL && vp->vec[1][0] != '\0')
lspath = vp->vec[1];
for (cp = vp->vec[0]; *cp; cp++)
switch (*cp) {
case 'x':
STRmCF[1] = 'x';
break;
case 'a':
STRmCF[3] = 'a';
break;
case 'A':
STRmCF[3] = 'A';
break;
default:
break;
}
}
cmd.word = STRNULL;
lastword = &cmd;
nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(lspath);
lastword->next = nextword;
nextword->prev = lastword;
lastword = nextword;
nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(STRmCF);
lastword->next = nextword;
nextword->prev = lastword;
#if defined(KANJI) && defined(SHORT_STRINGS) && defined(DSPMBYTE)
if (dspmbyte_ls) {
lastword = nextword;
nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(STRmmliteral);
lastword->next = nextword;
nextword->prev = lastword;
}
#endif
#ifdef COLOR_LS_F
if (color_context_ls) {
lastword = nextword;
nextword = xcalloc(1, sizeof cmd);
nextword->word = Strsave(STRmmcolormauto);
lastword->next = nextword;
nextword->prev = lastword;
}
#endif /* COLOR_LS_F */
lastword = nextword;
for (cp = *v; cp; cp = *++v) {
nextword = xcalloc(1, sizeof cmd);
nextword->word = quote(Strsave(cp));
//.........这里部分代码省略.........
示例9: aliasrun
/*
* Karl Kleinpaste, 21oct1983.
* Set up a one-word alias command, for use for special things.
* This code is based on the mainline of process().
*/
void
aliasrun(int cnt, Char *s1, Char *s2)
{
struct wordent w, *new1, *new2; /* for holding alias name */
struct command *t = NULL;
jmp_buf_t osetexit;
int status;
size_t omark;
getexit(osetexit);
if (seterr) {
xfree(seterr);
seterr = NULL; /* don't repeatedly print err msg. */
}
w.word = STRNULL;
new1 = xcalloc(1, sizeof w);
new1->word = Strsave(s1);
if (cnt == 1) {
/* build a lex list with one word. */
w.next = w.prev = new1;
new1->next = new1->prev = &w;
}
else {
/* build a lex list with two words. */
new2 = xcalloc(1, sizeof w);
new2->word = Strsave(s2);
w.next = new2->prev = new1;
new1->next = w.prev = new2;
new1->prev = new2->next = &w;
}
cleanup_push(&w, lex_cleanup);
/* Save the old status */
status = getn(varval(STRstatus));
/* expand aliases like process() does. */
alias(&w);
/* build a syntax tree for the command. */
t = syntax(w.next, &w, 0);
cleanup_push(t, syntax_cleanup);
if (seterr)
stderror(ERR_OLD);
psavejob();
cleanup_push(&cnt, psavejob_cleanup); /* cnt is used only as a marker */
/* catch any errors here */
omark = cleanup_push_mark();
if (setexit() == 0)
/* execute the parse tree. */
/*
* From: Michael Schroeder <[email protected]>
* was execute(t, tpgrp);
*/
execute(t, tpgrp > 0 ? tpgrp : -1, NULL, NULL, TRUE);
/* reset the error catcher to the old place */
cleanup_pop_mark(omark);
resexit(osetexit);
if (haderr) {
haderr = 0;
/*
* Either precmd, or cwdcmd, or periodic had an error. Call it again so
* that it is removed
*/
if (precmd_active)
precmd();
if (postcmd_active)
postcmd();
#ifdef notdef
/*
* XXX: On the other hand, just interrupting them causes an error too.
* So if we hit ^C in the middle of cwdcmd or periodic the alias gets
* removed. We don't want that. Note that we want to remove precmd
* though, cause that could lead into an infinite loop. This should be
* fixed correctly, but then haderr should give us the whole exit
* status not just true or false.
*/
else if (cwdcmd_active)
cwd_cmd();
else if (beepcmd_active)
beep_cmd();
else if (periodic_active)
period_cmd();
#endif /* notdef */
}
cleanup_until(&w);
pendjob();
/* Restore status */
setv(STRstatus, putn((tcsh_number_t)status), VAR_READWRITE);
}
示例10: insert_we
/* The actual "aliasing" of for backgrounds() is done here
with the aid of insert_we(). */
static void
insert(struct wordent *pl, int file_args)
{
struct wordent *now, *last;
Char *cmd, *bcmd, *cp1, *cp2;
size_t cmd_len;
Char *upause = STRunderpause;
size_t p_len = Strlen(upause);
cmd_len = Strlen(pl->word);
cmd = xcalloc(1, (cmd_len + 1) * sizeof(Char));
(void) Strcpy(cmd, pl->word);
/* Do insertions at beginning, first replace command word */
if (file_args) {
now = pl;
xfree(now->word);
now->word = xcalloc(1, 5 * sizeof(Char));
(void) Strcpy(now->word, STRecho);
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, 6 * sizeof(Char));
(void) Strcpy(now->word, STRbackqpwd);
insert_we(now, pl);
for (last = now; *last->word != '\n' && *last->word != ';';
last = last->next)
continue;
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, 2 * sizeof(Char));
(void) Strcpy(now->word, STRgt);
insert_we(now, last->prev);
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, 2 * sizeof(Char));
(void) Strcpy(now->word, STRbang);
insert_we(now, last->prev);
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, (cmd_len + p_len + 4) * sizeof(Char));
cp1 = now->word;
cp2 = cmd;
*cp1++ = '~';
*cp1++ = '/';
*cp1++ = '.';
while ((*cp1++ = *cp2++) != '\0')
continue;
cp1--;
cp2 = upause;
while ((*cp1++ = *cp2++) != '\0')
continue;
insert_we(now, last->prev);
now = xcalloc(1, sizeof(struct wordent));
now->word = xcalloc(1, 2 * sizeof(Char));
(void) Strcpy(now->word, STRsemi);
insert_we(now, last->prev);
bcmd = xcalloc(1, (cmd_len + 2) * sizeof(Char));
*bcmd = '%';
Strcpy(bcmd + 1, cmd);
now = xcalloc(1, sizeof(struct wordent));
now->word = bcmd;
insert_we(now, last->prev);
}
else {
struct wordent *del;
now = pl;
xfree(now->word);
now->word = xcalloc(1, (cmd_len + 2) * sizeof(Char));
*now->word = '%';
Strcpy(now->word + 1, cmd);
for (now = now->next;
*now->word != '\n' && *now->word != ';' && now != pl;) {
now->prev->next = now->next;
now->next->prev = now->prev;
xfree(now->word);
del = now;
now = now->next;
xfree(del);
}
}
}
示例11: cmd_grep
//.........这里部分代码省略.........
if ((opt.regflags != REG_NEWLINE) && opt.fixed)
die("cannot mix --fixed-strings and regexp");
#ifndef NO_PTHREADS
if (online_cpus() == 1 || !grep_threads_ok(&opt))
use_threads = 0;
if (use_threads) {
if (opt.pre_context || opt.post_context)
print_hunk_marks_between_files = 1;
start_threads(&opt);
}
#else
use_threads = 0;
#endif
compile_grep_patterns(&opt);
/* Check revs and then paths */
for (i = 0; i < argc; i++) {
const char *arg = argv[i];
unsigned char sha1[20];
/* Is it a rev? */
if (!get_sha1(arg, sha1)) {
struct object *object = parse_object(sha1);
if (!object)
die("bad object %s", arg);
add_object_array(object, arg, &list);
continue;
}
if (!strcmp(arg, "--")) {
i++;
seen_dashdash = 1;
}
break;
}
/* The rest are paths */
if (!seen_dashdash) {
int j;
for (j = i; j < argc; j++)
verify_filename(prefix, argv[j]);
}
if (i < argc)
paths = get_pathspec(prefix, argv + i);
else if (prefix) {
paths = xcalloc(2, sizeof(const char *));
paths[0] = prefix;
paths[1] = NULL;
}
if (show_in_pager && (cached || list.nr))
die("--open-files-in-pager only works on the worktree");
if (show_in_pager && opt.pattern_list && !opt.pattern_list->next) {
const char *pager = path_list.items[0].string;
int len = strlen(pager);
if (len > 4 && is_dir_sep(pager[len - 5]))
pager += len - 4;
if (!strcmp("less", pager) || !strcmp("vi", pager)) {
struct strbuf buf = STRBUF_INIT;
strbuf_addf(&buf, "+/%s%s",
strcmp("less", pager) ? "" : "*",
opt.pattern_list->pattern);
string_list_append(&path_list, buf.buf);
strbuf_detach(&buf, NULL);
}
}
if (!show_in_pager)
setup_pager();
if (!use_index) {
if (cached)
die("--cached cannot be used with --no-index.");
if (list.nr)
die("--no-index cannot be used with revs.");
hit = grep_directory(&opt, paths);
} else if (!list.nr) {
if (!cached)
setup_work_tree();
hit = grep_cache(&opt, paths, cached);
} else {
if (cached)
die("both --cached and trees are given.");
hit = grep_objects(&opt, paths, &list);
}
if (use_threads)
hit |= wait_all();
if (hit && show_in_pager)
run_pager(&opt, prefix);
free_grep_patterns(&opt);
return !hit;
}
示例12: subprocess
/*
* Runs command in a subprocess. Returns pid on success and a FILE* to the
* subprocess' stdout or 0 on failure.
* NB. "command" is only used for logging.
*/
static pid_t
subprocess(const char *tag, struct passwd *pw, const char *command,
int ac, char **av, FILE **child)
{
FILE *f;
struct stat st;
int devnull, p[2], i;
pid_t pid;
char *cp, errmsg[512];
u_int envsize;
char **child_env;
*child = NULL;
debug3("%s: %s command \"%s\" running as %s", __func__,
tag, command, pw->pw_name);
/* Verify the path exists and is safe-ish to execute */
if (*av[0] != '/') {
error("%s path is not absolute", tag);
return 0;
}
temporarily_use_uid(pw);
if (stat(av[0], &st) < 0) {
error("Could not stat %s \"%s\": %s", tag,
av[0], strerror(errno));
restore_uid();
return 0;
}
if (auth_secure_path(av[0], &st, NULL, 0,
errmsg, sizeof(errmsg)) != 0) {
error("Unsafe %s \"%s\": %s", tag, av[0], errmsg);
restore_uid();
return 0;
}
/*
* Run the command; stderr is left in place, stdout is the
* authorized_keys output.
*/
if (pipe(p) != 0) {
error("%s: pipe: %s", tag, strerror(errno));
restore_uid();
return 0;
}
/*
* Don't want to call this in the child, where it can fatal() and
* run cleanup_exit() code.
*/
restore_uid();
switch ((pid = fork())) {
case -1: /* error */
error("%s: fork: %s", tag, strerror(errno));
close(p[0]);
close(p[1]);
return 0;
case 0: /* child */
/* Prepare a minimal environment for the child. */
envsize = 5;
child_env = xcalloc(sizeof(*child_env), envsize);
child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH);
child_set_env(&child_env, &envsize, "USER", pw->pw_name);
child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name);
child_set_env(&child_env, &envsize, "HOME", pw->pw_dir);
if ((cp = getenv("LANG")) != NULL)
child_set_env(&child_env, &envsize, "LANG", cp);
for (i = 0; i < NSIG; i++)
signal(i, SIG_DFL);
if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
error("%s: open %s: %s", tag, _PATH_DEVNULL,
strerror(errno));
_exit(1);
}
/* Keep stderr around a while longer to catch errors */
if (dup2(devnull, STDIN_FILENO) == -1 ||
dup2(p[1], STDOUT_FILENO) == -1) {
error("%s: dup2: %s", tag, strerror(errno));
_exit(1);
}
closefrom(STDERR_FILENO + 1);
/* Don't use permanently_set_uid() here to avoid fatal() */
if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid,
strerror(errno));
_exit(1);
}
if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid,
strerror(errno));
_exit(1);
//.........这里部分代码省略.........
示例13: profile_init
/*--------------------------------------------------------------------*/
void profile_init(profile_t *p, u32 num_events) {
p->num_events = num_events;
p->total_time = -read_clock();
p->events = (u64 *)xcalloc(num_events, sizeof(u64));
}
示例14: lpx_eval_b_prim
void lpx_eval_b_prim(LPX *lp, double row_prim[], double col_prim[])
{ int i, j, k, m, n, stat, len, *ind;
double xN, *NxN, *xB, *val;
if (!lpx_is_b_avail(lp))
xfault("lpx_eval_b_prim: LP basis is not available\n");
m = lpx_get_num_rows(lp);
n = lpx_get_num_cols(lp);
/* store values of non-basic auxiliary and structural variables
and compute the right-hand side vector (-N*xN) */
NxN = xcalloc(1+m, sizeof(double));
for (i = 1; i <= m; i++) NxN[i] = 0.0;
/* walk through auxiliary variables */
for (i = 1; i <= m; i++)
{ /* obtain status of i-th auxiliary variable */
stat = lpx_get_row_stat(lp, i);
/* if it is basic, skip it */
if (stat == LPX_BS) continue;
/* i-th auxiliary variable is non-basic; get its value */
switch (stat)
{ case LPX_NL: xN = lpx_get_row_lb(lp, i); break;
case LPX_NU: xN = lpx_get_row_ub(lp, i); break;
case LPX_NF: xN = 0.0; break;
case LPX_NS: xN = lpx_get_row_lb(lp, i); break;
default: xassert(lp != lp);
}
/* store the value of non-basic auxiliary variable */
row_prim[i] = xN;
/* and add corresponding term to the right-hand side vector */
NxN[i] -= xN;
}
/* walk through structural variables */
ind = xcalloc(1+m, sizeof(int));
val = xcalloc(1+m, sizeof(double));
for (j = 1; j <= n; j++)
{ /* obtain status of j-th structural variable */
stat = lpx_get_col_stat(lp, j);
/* if it basic, skip it */
if (stat == LPX_BS) continue;
/* j-th structural variable is non-basic; get its value */
switch (stat)
{ case LPX_NL: xN = lpx_get_col_lb(lp, j); break;
case LPX_NU: xN = lpx_get_col_ub(lp, j); break;
case LPX_NF: xN = 0.0; break;
case LPX_NS: xN = lpx_get_col_lb(lp, j); break;
default: xassert(lp != lp);
}
/* store the value of non-basic structural variable */
col_prim[j] = xN;
/* and add corresponding term to the right-hand side vector */
if (xN != 0.0)
{ len = lpx_get_mat_col(lp, j, ind, val);
for (k = 1; k <= len; k++) NxN[ind[k]] += val[k] * xN;
}
}
xfree(ind);
xfree(val);
/* solve the system B*xB = (-N*xN) to compute the vector xB */
xB = NxN, lpx_ftran(lp, xB);
/* store values of basic auxiliary and structural variables */
for (i = 1; i <= m; i++)
{ k = lpx_get_b_info(lp, i);
xassert(1 <= k && k <= m+n);
if (k <= m)
row_prim[k] = xB[i];
else
col_prim[k-m] = xB[i];
}
xfree(NxN);
return;
}
示例15: glp_mpl_build_prob
void glp_mpl_build_prob(glp_tran *tran, glp_prob *prob)
{ /* build LP/MIP problem instance from the model */
int m, n, i, j, t, kind, type, len, *ind;
double lb, ub, *val;
if (tran->phase != 3)
xerror("glp_mpl_build_prob: invalid call sequence\n");
/* erase the problem object */
glp_erase_prob(prob);
/* set problem name */
glp_set_prob_name(prob, mpl_get_prob_name(tran));
/* build rows (constraints) */
m = mpl_get_num_rows(tran);
if (m > 0)
glp_add_rows(prob, m);
for (i = 1; i <= m; i++)
{ /* set row name */
glp_set_row_name(prob, i, mpl_get_row_name(tran, i));
/* set row bounds */
type = mpl_get_row_bnds(tran, i, &lb, &ub);
switch (type)
{ case MPL_FR: type = GLP_FR; break;
case MPL_LO: type = GLP_LO; break;
case MPL_UP: type = GLP_UP; break;
case MPL_DB: type = GLP_DB; break;
case MPL_FX: type = GLP_FX; break;
default: xassert(type != type);
}
if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
{ type = GLP_FX;
if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
}
glp_set_row_bnds(prob, i, type, lb, ub);
/* warn about non-zero constant term */
if (mpl_get_row_c0(tran, i) != 0.0)
xprintf("glp_mpl_build_prob: row %s; constant term %.12g ig"
"nored\n",
mpl_get_row_name(tran, i), mpl_get_row_c0(tran, i));
}
/* build columns (variables) */
n = mpl_get_num_cols(tran);
if (n > 0)
glp_add_cols(prob, n);
for (j = 1; j <= n; j++)
{ /* set column name */
glp_set_col_name(prob, j, mpl_get_col_name(tran, j));
/* set column kind */
kind = mpl_get_col_kind(tran, j);
switch (kind)
{ case MPL_NUM:
break;
case MPL_INT:
case MPL_BIN:
glp_set_col_kind(prob, j, GLP_IV);
break;
default:
xassert(kind != kind);
}
/* set column bounds */
type = mpl_get_col_bnds(tran, j, &lb, &ub);
switch (type)
{ case MPL_FR: type = GLP_FR; break;
case MPL_LO: type = GLP_LO; break;
case MPL_UP: type = GLP_UP; break;
case MPL_DB: type = GLP_DB; break;
case MPL_FX: type = GLP_FX; break;
default: xassert(type != type);
}
if (kind == MPL_BIN)
{ if (type == GLP_FR || type == GLP_UP || lb < 0.0) lb = 0.0;
if (type == GLP_FR || type == GLP_LO || ub > 1.0) ub = 1.0;
type = GLP_DB;
}
if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
{ type = GLP_FX;
if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
}
glp_set_col_bnds(prob, j, type, lb, ub);
}
/* load the constraint matrix */
ind = xcalloc(1+n, sizeof(int));
val = xcalloc(1+n, sizeof(double));
for (i = 1; i <= m; i++)
{ len = mpl_get_mat_row(tran, i, ind, val);
glp_set_mat_row(prob, i, len, ind, val);
}
/* build objective function (the first objective is used) */
for (i = 1; i <= m; i++)
{ kind = mpl_get_row_kind(tran, i);
if (kind == MPL_MIN || kind == MPL_MAX)
{ /* set objective name */
glp_set_obj_name(prob, mpl_get_row_name(tran, i));
/* set optimization direction */
glp_set_obj_dir(prob, kind == MPL_MIN ? GLP_MIN : GLP_MAX);
/* set constant term */
glp_set_obj_coef(prob, 0, mpl_get_row_c0(tran, i));
/* set objective coefficients */
len = mpl_get_mat_row(tran, i, ind, val);
for (t = 1; t <= len; t++)
glp_set_obj_coef(prob, ind[t], val[t]);
break;
//.........这里部分代码省略.........