本文整理汇总了C++中xstrndup函数的典型用法代码示例。如果您正苦于以下问题:C++ xstrndup函数的具体用法?C++ xstrndup怎么用?C++ xstrndup使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了xstrndup函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: foreach_proc
static int foreach_proc(void *rock,
const char *key,
size_t keylen,
const char *data,
size_t datalen)
{
struct seendata sd = SEENDATA_INITIALIZER;
struct seendata_rock *sr = (struct seendata_rock *)rock;
char *name = xstrndup(key, keylen);
int r;
parse_data(data, datalen, &sd);
r = (sr->f)(name, &sd, sr->rock);
seen_freedata(&sd);
free(name);
return r;
}
示例2: strstr
char *xstrdup_between(const char *src, const char *open, const char *close)
{
const char *start = strstr(src, open);
if (start == NULL)
{
log_debug("Open tag not found: '%s'", open);
return NULL;
}
start += strlen(open);
const char *stop = strstr(start, close);
if (stop == NULL)
{
log_debug("Close tag not found: '%s'", close);
return NULL;
}
return xstrndup(start, stop - start);
}
示例3: VarRefParseFromNamespaceAndScope
VarRef *VarRefParseFromScope(const char *var_ref_string, const char *scope)
{
if (!scope)
{
return VarRefParseFromNamespaceAndScope(var_ref_string, NULL, NULL, CF_NS, '.');
}
const char *scope_start = strchr(scope, CF_NS);
if (scope_start)
{
char *ns = xstrndup(scope, scope_start - scope);
VarRef *ref = VarRefParseFromNamespaceAndScope(var_ref_string, ns, scope_start + 1, CF_NS, '.');
free(ns);
return ref;
}
else
{
return VarRefParseFromNamespaceAndScope(var_ref_string, NULL, scope, CF_NS, '.');
}
}
示例4: ff_regexp
static long ff_regexp(const char *line, long len,
char *buffer, long buffer_size, void *priv)
{
char *line_buffer;
struct ff_regs *regs = priv;
regmatch_t pmatch[2];
int i;
int result = -1;
/* Exclude terminating newline (and cr) from matching */
if (len > 0 && line[len-1] == '\n') {
if (len > 1 && line[len-2] == '\r')
len -= 2;
else
len--;
}
line_buffer = xstrndup(line, len); /* make NUL terminated */
for (i = 0; i < regs->nr; i++) {
struct ff_reg *reg = regs->array + i;
if (!regexec(®->re, line_buffer, 2, pmatch, 0)) {
if (reg->negate)
goto fail;
break;
}
}
if (regs->nr <= i)
goto fail;
i = pmatch[1].rm_so >= 0 ? 1 : 0;
line += pmatch[i].rm_so;
result = pmatch[i].rm_eo - pmatch[i].rm_so;
if (result > buffer_size)
result = buffer_size;
while (result > 0 && (isspace(line[result - 1])))
result--;
memcpy(buffer, line, result);
fail:
free(line_buffer);
return result;
}
示例5: rewriteURLCompile
rewritetoken *
rewriteURLCompile(const char *urlfmt)
{
rewritetoken *head = NULL;
rewritetoken **tail = &head;
rewritetoken *_new = NULL;
const char *stt = urlfmt;
debug(85, 3) ("rewriteURLCompile(%s)\n", urlfmt);
while (*urlfmt != '\0') {
int urlEncode = 0;
while (*urlfmt != '\0' && *urlfmt != '%')
++urlfmt;
if (urlfmt != stt) {
_new = newRedirectTokenStr(RFT_STRING,
xstrndup(stt, urlfmt - stt + 1), urlfmt - stt, 0);
*tail = _new;
tail = &_new->next;
if (*urlfmt == '\0')
break;
stt = ++urlfmt;
}
switch (urlfmt[0]) {
case '#':
stt = ++urlfmt;
urlEncode = 1;
break;
case '%':
stt = ++urlfmt;
_new = newRedirectTokenStr(RFT_STRING, xstrdup("%"), 1, 0);
*tail = _new;
tail = &_new->next;
continue;
break;
}
_new = newRedirectToken(&urlfmt, urlEncode);
*tail = _new;
tail = &_new->next;
stt = urlfmt;
}
return head;
}
示例6: add_mount_options
/*
* For local mounts, add heartbeat=none.
* For userspace clusterstack, add cluster_stack=xxxx.
* For o2cb with local heartbeat, add heartbeat=local.
* For o2cb with global heartbeat, add heartbeat=global.
*/
static errcode_t add_mount_options(ocfs2_filesys *fs,
struct o2cb_cluster_desc *cluster,
char **optstr)
{
char *add, *extra = NULL;
char stackstr[strlen(OCFS2_CLUSTER_STACK_ARG) + OCFS2_STACK_LABEL_LEN + 1];
struct ocfs2_super_block *sb = OCFS2_RAW_SB(fs->fs_super);
if (ocfs2_mount_local(fs) || ocfs2_is_hard_readonly(fs)) {
add = OCFS2_HB_NONE;
goto addit;
}
if (cluster->c_stack &&
strcmp(cluster->c_stack, OCFS2_CLASSIC_CLUSTER_STACK)) {
snprintf(stackstr, sizeof(stackstr), "%s%s",
OCFS2_CLUSTER_STACK_ARG, cluster->c_stack);
add = stackstr;
goto addit;
}
if (ocfs2_cluster_o2cb_global_heartbeat(sb)) {
add = OCFS2_HB_GLOBAL;
goto addit;
}
add = OCFS2_HB_LOCAL;
addit:
if (*optstr && *(*optstr))
extra = xstrconcat3(*optstr, ",", add);
else
extra = xstrndup(add, strlen(add));
if (!extra)
return OCFS2_ET_NO_MEMORY;
*optstr = extra;
return 0;
}
示例7: mr_pass
/*! \brief PASS command handler
*
* \param source_p Pointer to allocated Client struct from which the message
* originally comes from. This can be a local or remote client.
* \param parc Integer holding the number of supplied arguments.
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL
* pointers.
* \note Valid arguments for this command are:
* - parv[0] = command
* - parv[1] = password
* - parv[2] = unused
* - parv[3] = TS protocol version
* - parv[4] = server ID (SID)
*/
static int
mr_pass(struct Client *source_p, int parc, char *parv[])
{
assert(MyConnect(source_p));
if (EmptyString(parv[1]))
{
sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "PASS");
return 0;
}
MyFree(source_p->connection->password);
source_p->connection->password = xstrndup(parv[1], IRCD_MIN(strlen(parv[1]), PASSWDLEN));
/* Only do this stuff if we are doing ts6 */
if (parc > 4)
if (atoi(parv[3]) >= 6 && valid_sid(parv[4]))
strlcpy(source_p->id, parv[4], sizeof(source_p->id));
return 0;
}
示例8: get_status
/* if num = 1, it returns the want status, 2 returns flag, 3 returns status */
unsigned int get_status(const unsigned int status_node, const int num)
{
char *status_string = name_hashtable[status_hashtable[status_node]->status];
char *state_sub_string;
unsigned int state_sub_num;
int len;
int i;
/* set tmp_string to point to the start of the word number */
for (i = 1; i < num; i++) {
/* skip past a word */
status_string += strcspn(status_string, " ");
/* skip past the seperating spaces */
status_string += strspn(status_string, " ");
}
len = strcspn(status_string, " \n\0");
state_sub_string = xstrndup(status_string, len);
state_sub_num = search_name_hashtable(state_sub_string);
free(state_sub_string);
return(state_sub_num);
}
示例9: xcalloc
static struct remote *make_remote(const char *name, int len)
{
struct remote *ret;
int i;
for (i = 0; i < remotes_nr; i++) {
if (len ? (!strncmp(name, remotes[i]->name, len) &&
!remotes[i]->name[len]) :
!strcmp(name, remotes[i]->name))
return remotes[i];
}
ret = xcalloc(1, sizeof(struct remote));
ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
remotes[remotes_nr++] = ret;
if (len)
ret->name = xstrndup(name, len);
else
ret->name = xstrdup(name);
return ret;
}
示例10: statusline_addhlstr
/* Same as statusline_addstr(), but hilight "<str>" sequences if the terminal
* supports it */
static void
statusline_addhlstr(const char *str)
{
#if defined(A_BOLD) && defined(A_NORMAL) && defined(A_DIM)
const char *p = str, *start = str;
char *tmp;
int pos = 0;
while(1) {
if(!*p || strchr("<>", *p)) {
if(p - start > 0) {
wattrset(bottom, (*p == '>') ? A_BOLD : A_NORMAL);
tmp = xstrndup(start, p - start);
mvwaddstr(bottom, 1, pos, tmp);
pos += strwidth(tmp);
free(tmp);
}
if(*p) {
start = p + 1;
/* show tag markers */
wattrset(bottom, A_DIM);
mvwaddch(bottom, 1, pos++, *p);
}
}
if(!*p) {
wattrset(bottom, A_NORMAL);
break;
}
p++;
}
#else
mvwaddstr(bottom, 1, 0, str);
#endif
refresh();
wrefresh(bottom);
}
示例11: save_headers
/* "save headers" callback */
static size_t
save_headers(void *buffer_pv, size_t count, size_t nmemb, void *ptr)
{
abrt_post_state_t* state = (abrt_post_state_t*)ptr;
size_t size = count * nmemb;
char *h = xstrndup((char*)buffer_pv, size);
strchrnul(h, '\r')[0] = '\0';
strchrnul(h, '\n')[0] = '\0';
unsigned cnt = state->header_cnt;
/* Check for the case when curl follows a redirect:
* header 0: 'HTTP/1.1 301 Moved Permanently'
* header 1: 'Connection: close'
* header 2: 'Location: NEW_URL'
* header 3: ''
* header 0: 'HTTP/1.1 200 OK' <-- we need to forget all hdrs and start anew
*/
if (cnt != 0
&& strncmp(h, "HTTP/", 5) == 0
&& state->headers[cnt-1][0] == '\0' /* prev header is an empty string */
) {
char **headers = state->headers;
if (headers)
{
while (*headers)
free(*headers++);
}
cnt = 0;
}
VERB3 log("save_headers: header %d: '%s'", cnt, h);
state->headers = (char**)xrealloc(state->headers, (cnt+2) * sizeof(state->headers[0]));
state->headers[cnt] = h;
state->header_cnt = ++cnt;
state->headers[cnt] = NULL;
return size;
}
示例12: listscripts
/* list the scripts user has available */
int listscripts(struct protstream *conn)
{
DIR *dp;
struct dirent *dir;
size_t length;
/* open the directory */
dp=opendir(".");
if (dp==NULL)
{
prot_printf(conn,"NO \"Error opening directory\"\r\n");
return TIMSIEVE_FAIL;
}
while ((dir=readdir(dp)) != NULL) /* while there are files here */
{
length=strlen(dir->d_name);
if (length >= strlen(".script")) /* if ends in .script */
{
if (strcmp(dir->d_name + (length - 7), ".script")==0)
{
char *namewo = xstrndup(dir->d_name, length-7);
if (isactive(namewo))
prot_printf(conn,"\"%s\" ACTIVE\r\n", namewo);
else
prot_printf(conn,"\"%s\"\r\n", namewo);
free(namewo);
}
}
}
closedir(dp);
prot_printf(conn,"OK\r\n");
return TIMSIEVE_OK;
}
示例13: mr_pass
/*! \brief PASS command handler
*
* \param source_p Pointer to allocated Client struct from which the message
* originally comes from. This can be a local or remote client.
* \param parc Integer holding the number of supplied arguments.
* \param parv Argument vector where parv[0] .. parv[parc-1] are non-NULL
* pointers.
* \note Valid arguments for this command are:
* - parv[0] = command
* - parv[1] = password
* - parv[2] = optional extra version information
* - parv[3] = TS protocol version
* - parv[4] = server ID (SID)
*/
static int
mr_pass(struct Client *source_p, int parc, char *parv[])
{
assert(MyConnect(source_p));
if (EmptyString(parv[1]))
{
sendto_one_numeric(source_p, &me, ERR_NEEDMOREPARAMS, "PASS");
return 0;
}
MyFree(source_p->connection->password);
source_p->connection->password = xstrndup(parv[1], IRCD_MIN(strlen(parv[1]), PASSWDLEN));
if (parc > 2)
{
/*
* It looks to me as if orabidoo wanted to have more
* than one set of option strings possible here...
* i.e. ":AABBTS" as long as TS was the last two chars
* however, as we are now using CAPAB, I think we can
* safely assume if there is a ":TS" then it's a TS server
* -Dianora
*/
if (!irccmp(parv[2], "TS") && source_p->tsinfo == 0)
source_p->tsinfo = TS_DOESTS;
}
/* Only do this stuff if we are doing ts6 */
if (parc > 4)
{
if (atoi(parv[3]) >= 6 && valid_sid(parv[4]))
{
strlcpy(source_p->id, parv[4], sizeof(source_p->id));
SetCapable(source_p, CAP_TS6);
}
}
return 0;
}
示例14: select_match
void select_match(const char *expr) {
const char *ptr;
union arg *args;
int rc, operator;
char buffer[128];
for(ptr = expr;
*ptr && *ptr != '=' && *ptr != '~'
&& *ptr != '<' && *ptr != '>' && *ptr != '!';
++ptr)
;
if(!*ptr)
fatal(0, "invalid match expression '%s'", expr);
args = xmalloc(3 * sizeof *args);
args[0].string = xstrndup(expr, ptr - expr);
ptr = get_operator(ptr, &operator);
if(!ptr)
fatal(0, "%s: unrecognized match operator\n", expr);
if(*ptr == ':')
++ptr;
switch(operator) {
case '~':
rc = regcomp(&args[1].regex, ptr, REG_ICASE|REG_NOSUB);
if(rc) {
regerror(rc, &args[1].regex, buffer, sizeof buffer);
fatal(0, "regexec: %s", buffer);
}
select_add(select_regex_match, args, 2);
break;
case IDENTICAL:
args[1].string = xstrdup(ptr);
select_add(select_string_match, args, 2);
break;
default:
args[1].operator = operator;
args[2].string = xstrdup(ptr);
select_add(select_compare, args, 3);
break;
}
}
示例15: do_onequota
static int do_onequota(void *rock,
const char *key, size_t keylen,
const char *data, size_t datalen)
{
int r = 0;
struct quota quota;
struct quota_foreach_t *fd = (struct quota_foreach_t *)rock;
char *root = xstrndup(key, keylen);
int iswrite = fd->tid ? 1 : 0;
quota_init("a, root);
/* XXX - error if not parsable? */
if (datalen && !quota_parseval(data, datalen, "a, iswrite)) {
r = fd->proc("a, fd->rock);
}
quota_free("a);
free(root);
return r;
}