本文整理汇总了C++中xrealloc函数的典型用法代码示例。如果您正苦于以下问题:C++ xrealloc函数的具体用法?C++ xrealloc怎么用?C++ xrealloc使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了xrealloc函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: load_all_part_state
/*
* load_all_part_state - load the partition state from file, recover on
* slurmctld restart. execute this after loading the configuration
* file data.
* NOTE: READ lock_slurmctld config before entry
*/
int load_all_part_state(void)
{
char *part_name = NULL, *allow_groups = NULL, *nodes = NULL;
char *state_file, *data = NULL;
uint32_t max_time, default_time, max_nodes, min_nodes;
uint32_t grace_time = 0;
time_t time;
uint16_t def_part_flag, flags, hidden, root_only;
uint16_t max_share, preempt_mode, priority, state_up;
struct part_record *part_ptr;
uint32_t data_size = 0, name_len;
int data_allocated, data_read = 0, error_code = 0, part_cnt = 0;
int state_fd;
Buf buffer;
char *ver_str = NULL;
char* allow_alloc_nodes = NULL;
uint16_t protocol_version = (uint16_t)NO_VAL;
char* alternate = NULL;
/* read the file */
lock_state_files();
state_fd = _open_part_state_file(&state_file);
if (state_fd < 0) {
info("No partition state file (%s) to recover",
state_file);
error_code = ENOENT;
} else {
data_allocated = BUF_SIZE;
data = xmalloc(data_allocated);
while (1) {
data_read = read(state_fd, &data[data_size],
BUF_SIZE);
if (data_read < 0) {
if (errno == EINTR)
continue;
else {
error("Read error on %s: %m",
state_file);
break;
}
} else if (data_read == 0) /* eof */
break;
data_size += data_read;
data_allocated += data_read;
xrealloc(data, data_allocated);
}
close(state_fd);
}
xfree(state_file);
unlock_state_files();
buffer = create_buf(data, data_size);
safe_unpackstr_xmalloc( &ver_str, &name_len, buffer);
debug3("Version string in part_state header is %s", ver_str);
if(ver_str) {
if (!strcmp(ver_str, PART_STATE_VERSION)) {
protocol_version = SLURM_PROTOCOL_VERSION;
} else if(!strcmp(ver_str, PART_2_1_STATE_VERSION)) {
protocol_version = SLURM_2_1_PROTOCOL_VERSION;
}
}
if (protocol_version == (uint16_t)NO_VAL) {
error("**********************************************************");
error("Can not recover partition state, data version incompatible");
error("**********************************************************");
xfree(ver_str);
free_buf(buffer);
return EFAULT;
}
xfree(ver_str);
safe_unpack_time(&time, buffer);
while (remaining_buf(buffer) > 0) {
if (protocol_version >= SLURM_2_3_PROTOCOL_VERSION) {
safe_unpackstr_xmalloc(&part_name, &name_len, buffer);
safe_unpack32(&grace_time, buffer);
safe_unpack32(&max_time, buffer);
safe_unpack32(&default_time, buffer);
safe_unpack32(&max_nodes, buffer);
safe_unpack32(&min_nodes, buffer);
safe_unpack16(&flags, buffer);
safe_unpack16(&max_share, buffer);
safe_unpack16(&preempt_mode, buffer);
safe_unpack16(&priority, buffer);
if (priority > part_max_priority)
part_max_priority = priority;
safe_unpack16(&state_up, buffer);
safe_unpackstr_xmalloc(&allow_groups,
&name_len, buffer);
//.........这里部分代码省略.........
示例2: _job_modify
//.........这里部分代码省略.........
}
if (env_ptr[i] == '\\') {
i++;
}
}
} else if (env_ptr[i+1] == '\'') {
for (i+=2; ; i++) {
if (env_ptr[i] == '\0') {
error("wiki: setting job %u "
"invalid environment "
"variables: %s",
jobid, env_ptr);
return EINVAL;
}
if (env_ptr[i] == '\'') {
i++;
break;
}
if (env_ptr[i] == '\\') {
i++;
}
}
}
}
if (isspace(env_ptr[i]) || (env_ptr[i] == ',')) {
if (!have_equal) {
error("wiki: setting job %u invalid "
"environment variables: %s",
jobid, env_ptr);
return EINVAL;
}
old_sep[0] = env_ptr[i];
env_ptr[i] = '\0';
xrealloc(job_ptr->details->env_sup,
sizeof(char *) *
(job_ptr->details->env_cnt+1));
job_ptr->details->env_sup
[job_ptr->details->env_cnt++] =
xstrdup(&env_ptr[begin]);
info("wiki: for job %u add env: %s",
jobid, &env_ptr[begin]);
env_ptr[i] = old_sep[0];
if (isspace(old_sep[0]))
break;
begin = i + 1;
have_equal = false;
}
}
}
if (new_time_limit) {
time_t old_time = job_ptr->time_limit;
job_ptr->time_limit = new_time_limit;
info("wiki: change job %u time_limit to %u",
jobid, new_time_limit);
/* Update end_time based upon change
* to preserve suspend time info */
job_ptr->end_time = job_ptr->end_time +
((job_ptr->time_limit -
old_time) * 60);
last_job_update = now;
}
if (bank_ptr &&
(update_job_account("wiki", job_ptr, bank_ptr) != SLURM_SUCCESS)) {
return EINVAL;
示例3: process_files
//.........这里部分代码省略.........
case 'T':
if (substituted) break;
/* Fall through */
/* Branch to label */
case 'b':
if (!sed_cmd->string) goto discard_commands;
else sed_cmd = branch_to(sed_cmd->string);
break;
/* Transliterate characters */
case 'y':
{
int i, j;
for (i = 0; pattern_space[i]; i++) {
for (j = 0; sed_cmd->string[j]; j += 2) {
if (pattern_space[i] == sed_cmd->string[j]) {
pattern_space[i] = sed_cmd->string[j + 1];
break;
}
}
}
break;
}
case 'g': /* Replace pattern space with hold space */
free(pattern_space);
pattern_space = xstrdup(G.hold_space ? G.hold_space : "");
break;
case 'G': /* Append newline and hold space to pattern space */
{
int pattern_space_size = 2;
int hold_space_size = 0;
if (pattern_space)
pattern_space_size += strlen(pattern_space);
if (G.hold_space)
hold_space_size = strlen(G.hold_space);
pattern_space = xrealloc(pattern_space,
pattern_space_size + hold_space_size);
if (pattern_space_size == 2)
pattern_space[0] = 0;
strcat(pattern_space, "\n");
if (G.hold_space)
strcat(pattern_space, G.hold_space);
last_gets_char = '\n';
break;
}
case 'h': /* Replace hold space with pattern space */
free(G.hold_space);
G.hold_space = xstrdup(pattern_space);
break;
case 'H': /* Append newline and pattern space to hold space */
{
int hold_space_size = 2;
int pattern_space_size = 0;
if (G.hold_space)
hold_space_size += strlen(G.hold_space);
if (pattern_space)
pattern_space_size = strlen(pattern_space);
G.hold_space = xrealloc(G.hold_space,
hold_space_size + pattern_space_size);
if (hold_space_size == 2)
*G.hold_space = 0;
strcat(G.hold_space, "\n");
if (pattern_space)
strcat(G.hold_space, pattern_space);
break;
}
case 'x': /* Exchange hold and pattern space */
{
char *tmp = pattern_space;
pattern_space = G.hold_space ? : xzalloc(1);
last_gets_char = '\n';
G.hold_space = tmp;
break;
}
}
}
}
/*
* exit point from sedding...
*/
discard_commands:
/* we will print the line unless we were told to be quiet ('-n')
or if the line was suppressed (ala 'd'elete) */
if (!G.be_quiet)
sed_puts(pattern_space, last_gets_char);
/* Delete and such jump here. */
discard_line:
flush_append();
free(pattern_space);
goto again;
}
示例4: read_lines
//.........这里部分代码省略.........
continue;
}
{
size_t new_linepos = linepos + 1;
if (c == '\t') {
new_linepos += 7;
new_linepos &= (~7);
}
if (new_linepos >= w)
break;
linepos = new_linepos;
}
/* ok, we will eat this char */
readpos++;
if (c == '\n') {
terminated = 1;
linepos = 0;
break;
}
/* NUL is substituted by '\n'! */
if (c == '\0') c = '\n';
*p++ = c;
*p = '\0';
} /* end of "read chars until we have a line" loop */
/* Corner case: linewrap with only "" wrapping to next line */
/* Looks ugly on screen, so we do not store this empty line */
if (!last_terminated && !current_line[0]) {
last_terminated = 1;
max_lineno++;
continue;
}
reached_eof:
last_terminated = terminated;
flines = xrealloc(flines, (max_fline+1) * sizeof(char *));
if (option_mask32 & FLAG_N) {
/* Width of 7 preserves tab spacing in the text */
flines[max_fline] = xasprintf(
(max_lineno <= 9999999) ? "%7u %s" : "%07u %s",
max_lineno % 10000000, current_line);
free(current_line);
if (terminated)
max_lineno++;
} else {
flines[max_fline] = xrealloc(current_line, strlen(current_line)+1);
}
if (max_fline >= MAXLINES) {
eof_error = 0; /* Pretend we saw EOF */
break;
}
if (max_fline > cur_fline + max_displayed_line) {
#if !ENABLE_FEATURE_LESS_REGEXP
break;
#else
if (wanted_match >= num_matches) { /* goto_match called us */
fill_match_lines(old_max_fline);
old_max_fline = max_fline;
}
if (wanted_match < num_matches)
break;
#endif
}
if (eof_error <= 0) {
if (eof_error < 0) {
if (errno == EAGAIN) {
/* not yet eof or error, reset flag (or else
* we will hog CPU - select() will return
示例5: phase2_getc
static int
phase2_getc ()
{
static char *buffer;
static size_t bufmax;
size_t buflen;
int lineno;
int c;
bool last_was_star;
if (phase2_pushback_length)
return phase2_pushback[--phase2_pushback_length];
if (char_in_line == 0)
{
/* Eat whitespace, to recognize ^[\t ]*# pattern. */
do
c = phase1_getc ();
while (c == '\t' || c == ' ');
if (c == '#')
{
/* sh comment. */
buflen = 0;
lineno = line_number;
for (;;)
{
c = phase1_getc ();
if (c == '\n' || c == EOF)
break;
/* We skip all leading white space, but not EOLs. */
if (!(buflen == 0 && (c == ' ' || c == '\t')))
{
if (buflen >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[buflen++] = c;
}
}
if (buflen >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[buflen] = '\0';
xgettext_comment_add (buffer);
last_comment_line = lineno;
return '\n';
}
}
else
c = phase1_getc ();
if (c == '/')
{
c = phase1_getc ();
switch (c)
{
default:
phase1_ungetc (c);
return '/';
case '*':
/* C comment. */
buflen = 0;
lineno = line_number;
last_was_star = false;
for (;;)
{
c = phase1_getc ();
if (c == EOF)
break;
/* We skip all leading white space, but not EOLs. */
if (buflen == 0 && (c == ' ' || c == '\t'))
continue;
if (buflen >= bufmax)
{
bufmax = 2 * bufmax + 10;
buffer = xrealloc (buffer, bufmax);
}
buffer[buflen++] = c;
switch (c)
{
case '\n':
--buflen;
while (buflen >= 1
&& (buffer[buflen - 1] == ' '
|| buffer[buflen - 1] == '\t'))
--buflen;
buffer[buflen] = '\0';
xgettext_comment_add (buffer);
buflen = 0;
lineno = line_number;
last_was_star = false;
continue;
case '*':
//.........这里部分代码省略.........
示例6: cmd_format_patch
//.........这里部分代码省略.........
if (!(o->flags & UNINTERESTING))
head = (struct commit *)o;
}
/* We can't generate a cover letter without any patches */
if (!head)
return 0;
}
if (ignore_if_in_upstream)
get_patch_ids(&rev, &ids, prefix);
if (!use_stdout)
realstdout = xfdopen(xdup(1), "w");
if (prepare_revision_walk(&rev))
die("revision walk setup failed");
rev.boundary = 1;
while ((commit = get_revision(&rev)) != NULL) {
if (commit->object.flags & BOUNDARY) {
boundary_count++;
origin = (boundary_count == 1) ? commit : NULL;
continue;
}
/* ignore merges */
if (commit->parents && commit->parents->next)
continue;
if (ignore_if_in_upstream &&
has_commit_patch_id(commit, &ids))
continue;
nr++;
list = xrealloc(list, nr * sizeof(list[0]));
list[nr - 1] = commit;
}
total = nr;
if (!keep_subject && auto_number && total > 1)
numbered = 1;
if (numbered)
rev.total = total + start_number - 1;
if (in_reply_to || thread || cover_letter)
rev.ref_message_ids = xcalloc(1, sizeof(struct string_list));
if (in_reply_to) {
const char *msgid = clean_message_id(in_reply_to);
string_list_append(msgid, rev.ref_message_ids);
}
rev.numbered_files = numbered_files;
rev.patch_suffix = fmt_patch_suffix;
if (cover_letter) {
if (thread)
gen_message_id(&rev, "cover");
make_cover_letter(&rev, use_stdout, numbered, numbered_files,
origin, nr, list, head);
total++;
start_number--;
}
rev.add_signoff = add_signoff;
while (0 <= --nr) {
int shown;
commit = list[nr];
rev.nr = total - nr + (start_number - 1);
/* Make the second and subsequent mails replies to the first */
if (thread) {
/* Have we already had a message ID? */
if (rev.message_id) {
示例7: mi_parse_argv
static void
mi_parse_argv (char *args, struct mi_parse *parse)
{
char *chp = args;
int argc = 0;
char **argv = xmalloc ((argc + 1) * sizeof (char *));
argv[argc] = NULL;
while (1)
{
char *arg;
/* skip leading white space */
while (isspace (*chp))
chp++;
/* Three possibilities: EOF, quoted string, or other text. */
switch (*chp)
{
case '\0':
parse->argv = argv;
parse->argc = argc;
return;
case '"':
{
/* A quoted string. */
int len;
char *start = chp + 1;
/* Determine the buffer size. */
chp = start;
len = 0;
while (*chp != '\0' && *chp != '"')
{
if (*chp == '\\')
{
chp++;
if (parse_escape (&chp) <= 0)
{
/* Do not allow split lines or "\000" */
freeargv (argv);
return;
}
}
else
chp++;
len++;
}
/* Insist on a closing quote. */
if (*chp != '"')
{
freeargv (argv);
return;
}
/* Insist on trailing white space. */
if (chp[1] != '\0' && !isspace (chp[1]))
{
freeargv (argv);
return;
}
/* create the buffer. */
arg = xmalloc ((len + 1) * sizeof (char));
/* And copy the characters in. */
chp = start;
len = 0;
while (*chp != '\0' && *chp != '"')
{
if (*chp == '\\')
{
chp++;
arg[len] = parse_escape (&chp);
}
else
arg[len] = *chp++;
len++;
}
arg[len] = '\0';
chp++; /* that closing quote. */
break;
}
default:
{
/* An unquoted string. Accumulate all non blank
characters into a buffer. */
int len;
char *start = chp;
while (*chp != '\0' && !isspace (*chp))
{
chp++;
}
len = chp - start;
arg = xmalloc ((len + 1) * sizeof (char));
strncpy (arg, start, len);
arg[len] = '\0';
break;
}
}
/* Append arg to argv. */
argv = xrealloc (argv, (argc + 2) * sizeof (char *));
argv[argc++] = arg;
argv[argc] = NULL;
}
}
示例8: tgetent
//.........这里部分代码省略.........
if (!indirect)
{
if (!bp)
bp = termcap_name;
else
strcpy (bp, termcap_name);
goto ret;
}
else
{ /* It has tc=. Need to read /etc/termcap. */
tcenv = termcap_name;
termcap_name = NULL;
}
}
if (!termcap_name || !filep)
termcap_name = TERMCAP_FILE;
/* Here we know we must search a file and termcap_name has its name. */
#ifdef MSDOS
fd = open (termcap_name, O_RDONLY|O_TEXT, 0);
#else
fd = open (termcap_name, O_RDONLY, 0);
#endif
if (fd < 0)
return -1;
buf.size = BUFSIZE;
/* Add 1 to size to ensure room for terminating null. */
buf.beg = (char *) xmalloc (buf.size + 1);
term = indirect ? indirect : (char *)name;
if (!bp)
{
malloc_size = indirect ? strlen (tcenv) + 1 : buf.size;
bp = (char *) xmalloc (malloc_size);
}
tc_search_point = bp1 = bp;
if (indirect)
/* Copy the data from the environment variable. */
{
strcpy (bp, tcenv);
bp1 += strlen (tcenv);
}
while (term)
{
/* Scan the file, reading it via buf, till find start of main entry. */
if (scan_file (term, fd, &buf) == 0)
{
close (fd);
xfree (buf.beg);
if (malloc_size)
xfree (bp);
return 0;
}
/* Free old `term' if appropriate. */
if (term != name)
xfree (term);
/* If BP is malloc'd by us, make sure it is big enough. */
if (malloc_size)
{
ptrdiff_t offset1 = bp1 - bp, offset2 = tc_search_point - bp;
malloc_size = offset1 + buf.size;
bp = termcap_name = (char *) xrealloc (bp, malloc_size);
bp1 = termcap_name + offset1;
tc_search_point = termcap_name + offset2;
}
/* Copy the line of the entry from buf into bp. */
termcap_name = buf.ptr;
while ((*bp1++ = c = *termcap_name++) && c != '\n')
/* Drop out any \ newline sequence. */
if (c == '\\' && *termcap_name == '\n')
{
bp1--;
termcap_name++;
}
*bp1 = '\0';
/* Does this entry refer to another terminal type's entry?
If something is found, copy it into heap and null-terminate it. */
tc_search_point = find_capability (tc_search_point, "tc");
term = tgetst1 (tc_search_point, (char **) 0);
}
close (fd);
xfree (buf.beg);
if (malloc_size)
bp = (char *) xrealloc (bp, bp1 - bp + 1);
ret:
term_entry = bp;
return 1;
}
示例9: read_token
/*
* Read and return a token. Tokens are string or character literals
* or else delimited by whitespace or by [(),{}].
* The latter are all returned as single characters.
*/
static char *
read_token(void)
{
int c, q;
char *buf;
unsigned int alc, off;
const char* delims = "(),{}";
while (1) {
c = getchar_skipping_comments();
if (c == EOF)
return NULL;
if (!isspace(c))
break;
}
alc = 16;
buf = xmalloc(alc + 1);
off = 0;
if(c == '"' || c == '\'') {
q = c;
buf[off] = c;
++off;
while (1) {
if (off+2 >= alc) { // room for c and maybe next char
alc *= 2;
buf = xrealloc(buf, alc + 1);
}
c = getchar_no_eof();
buf[off] = c;
++off;
if(c == q)
break;
if(c == '\\') {
buf[off] = getchar_no_eof();
++off;
}
}
} else if (strchr(delims, c) != NULL) {
buf[off] = c;
++off;
} else {
while (1) {
if (off >= alc) {
alc *= 2;
buf = xrealloc(buf, alc + 1);
}
buf[off] = c;
++off;
c = getchar_skipping_comments();
if (c == EOF)
break;
if (isspace(c) || strchr(delims, c) != NULL) {
if (c == '\n')
lineno--;
ungetc(c, stdin);
break;
}
}
}
buf[off] = '\0';
return buf;
}
示例10: initialize_current_architecture
void
initialize_current_architecture (void)
{
const char **arches = gdbarch_printable_names ();
struct gdbarch_info info;
/* determine a default architecture and byte order. */
gdbarch_info_init (&info);
/* Find a default architecture. */
if (default_bfd_arch == NULL)
{
/* Choose the architecture by taking the first one
alphabetically. */
const char *chosen = arches[0];
const char **arch;
for (arch = arches; *arch != NULL; arch++)
{
if (strcmp (*arch, chosen) < 0)
chosen = *arch;
}
if (chosen == NULL)
internal_error (__FILE__, __LINE__,
_("initialize_current_architecture: No arch"));
default_bfd_arch = bfd_scan_arch (chosen);
if (default_bfd_arch == NULL)
internal_error (__FILE__, __LINE__,
_("initialize_current_architecture: Arch not found"));
}
info.bfd_arch_info = default_bfd_arch;
/* Take several guesses at a byte order. */
if (default_byte_order == BFD_ENDIAN_UNKNOWN
&& default_bfd_vec != NULL)
{
/* Extract BFD's default vector's byte order. */
switch (default_bfd_vec->byteorder)
{
case BFD_ENDIAN_BIG:
default_byte_order = BFD_ENDIAN_BIG;
break;
case BFD_ENDIAN_LITTLE:
default_byte_order = BFD_ENDIAN_LITTLE;
break;
default:
break;
}
}
if (default_byte_order == BFD_ENDIAN_UNKNOWN)
{
/* look for ``*el-*'' in the target name. */
const char *chp;
chp = strchr (target_name, '-');
if (chp != NULL
&& chp - 2 >= target_name
&& strncmp (chp - 2, "el", 2) == 0)
default_byte_order = BFD_ENDIAN_LITTLE;
}
if (default_byte_order == BFD_ENDIAN_UNKNOWN)
{
/* Wire it to big-endian!!! */
default_byte_order = BFD_ENDIAN_BIG;
}
info.byte_order = default_byte_order;
info.byte_order_for_code = info.byte_order;
if (! gdbarch_update_p (info))
internal_error (__FILE__, __LINE__,
_("initialize_current_architecture: Selection of "
"initial architecture failed"));
/* Create the ``set architecture'' command appending ``auto'' to the
list of architectures. */
{
/* Append ``auto''. */
int nr;
for (nr = 0; arches[nr] != NULL; nr++);
arches = xrealloc (arches, sizeof (char*) * (nr + 2));
arches[nr + 0] = "auto";
arches[nr + 1] = NULL;
add_setshow_enum_cmd ("architecture", class_support,
arches, &set_architecture_string,
_("Set architecture of target."),
_("Show architecture of target."), NULL,
set_architecture, show_architecture,
&setlist, &showlist);
add_alias_cmd ("processor", "architecture", class_support, 1, &setlist);
}
}
示例11: container_p_create
extern int container_p_create(uint32_t job_id)
{
#ifdef HAVE_NATIVE_CRAY
rid_t resv_id = job_id;
int rc;
#endif
int i, empty = -1, found = -1;
DEF_TIMERS;
START_TIMER;
if (debug_flags & DEBUG_FLAG_JOB_CONT)
info("%s: creating(%u)", plugin_type, job_id);
slurm_mutex_lock(&context_lock);
for (i = 0; i < job_id_count; i++) {
if (job_id_array[i] == 0) {
empty = i;
} else if (job_id_array[i] == job_id) {
found = i;
break;
}
}
if (found == -1) {
if (empty == -1) {
empty = job_id_count;
job_id_count += 4;
job_id_array = xrealloc(job_id_array,
sizeof(uint32_t)*job_id_count);
}
job_id_array[empty] = job_id;
_save_state(state_dir);
}
slurm_mutex_unlock(&context_lock);
if (debug_flags & DEBUG_FLAG_TIME_CRAY) {
END_TIMER;
INFO_LINE("call took: %s", TIME_STR);
} else {
END_TIMER3("container_p_create: saving state took", 3000000);
}
#ifdef HAVE_NATIVE_CRAY
START_TIMER;
rc = job_create_reservation(resv_id, CREATE_FLAGS);
if (debug_flags & DEBUG_FLAG_TIME_CRAY) {
END_TIMER;
INFO_LINE("call took: %s", TIME_STR);
} else
END_TIMER3("container_p_create: job_create_reservation took",
3000000);
if ((rc == 0) || (errno == EEXIST)) {
if ((found == -1) && (rc != 0) && (errno == EEXIST)) {
error("%s: create(%u): Reservation already exists",
plugin_type, job_id);
}
if (debug_flags & DEBUG_FLAG_JOB_CONT)
_stat_reservation("create", resv_id);
return SLURM_SUCCESS;
}
error("%s: create(%u): %m", plugin_type, job_id);
return SLURM_ERROR;
#else
return SLURM_SUCCESS;
#endif
}
示例12: poptParseArgvString
int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr)
{
const char * se;
char quote = '\0';
size_t argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA;
const char ** argv = (const char**) xmalloc(sizeof(*argv) * argvAlloced);
unsigned int argc = 0;
size_t ns = strlen(s);
char * t = NULL;
char * te;
int rc = POPT_ERROR_MALLOC;
assert(argv); /* XXX can't happen */
if (argv == NULL) return rc;
te = t = (char*) xmalloc(ns + 1);
assert(te); /* XXX can't happen */
if (te == NULL) {
argv = _free(argv);
return rc;
}
*te = '\0';
argv[argc] = te;
for (se = s; *se != '\0'; se++) {
if (quote == *se) {
quote = '\0';
} else if (quote != '\0') {
if (*se == '\\') {
se++;
if (*se == '\0') {
rc = POPT_ERROR_BADQUOTE;
goto exit;
}
if (*se != quote) *te++ = '\\';
}
*te++ = *se;
} else if (_isspaceptr(se)) {
if (*argv[argc] != '\0') {
*te++ = '\0', argc++;
if (argc == argvAlloced) {
argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA;
argv = (const char**) xrealloc(argv, sizeof(*argv) * argvAlloced);
assert(argv); /* XXX can't happen */
if (argv == NULL) goto exit;
}
*te = '\0';
argv[argc] = te;
}
} else
switch (*se) {
case '"':
case '\'':
quote = *se;
/*@[email protected]*/ break;
case '\\':
se++;
if (*se == '\0') {
rc = POPT_ERROR_BADQUOTE;
goto exit;
}
/*@[email protected]*/
default:
*te++ = *se;
/*@[email protected]*/ break;
}
}
if (strlen(argv[argc])) {
argc++, *te++ = '\0';
}
rc = poptDupArgv(argc, argv, argcPtr, argvPtr);
exit:
t = _free(t);
argv = _free(argv);
return rc;
}
示例13: poptConfigFileToString
/* still in the dev stage.
* return values, perhaps 1== file erro
* 2== line to long
* 3== umm.... more?
*/
int poptConfigFileToString(FILE *fp, char ** argstrp,
/*@[email protected]*/ UNUSED(int flags))
{
size_t nline = 8192; /* XXX configurable? */
char * line = (char*) alloca(nline);
char * argstr;
char * q;
char * x;
size_t t;
size_t argvlen = 0;
size_t maxargvlen = (size_t)480;
if (argstrp)
*argstrp = NULL;
/* | this_is = our_line
* p q x
*/
if (fp == NULL)
return POPT_ERROR_NULLARG;
argstr = (char*) xmalloc(maxargvlen * sizeof(*argstr));
assert(argstr); /* XXX can't happen */
if (argstr == NULL) return POPT_ERROR_MALLOC;
argstr[0] = '\0';
while (fgets(line, (int)nline, fp) != NULL) {
char * l = line;
size_t nl;
/* loop until first non-space char or EOL */
while( *l != '\0' && _isspaceptr(l) )
l++;
nl = strlen(l);
if (nl >= nline-1) {
argstr = _free(argstr);
return POPT_ERROR_OVERFLOW; /* XXX line too long */
}
if (*l == '\0' || *l == '\n') continue; /* line is empty */
if (*l == '#') continue; /* comment line */
q = l;
while (*q != '\0' && (!_isspaceptr(q)) && *q != '=')
q++;
if (_isspaceptr(q)) {
/* a space after the name, find next non space */
*q++='\0';
while( *q != '\0' && _isspaceptr(q) ) q++;
}
if (*q == '\0') {
/* single command line option (ie, no name=val, just name) */
q[-1] = '\0'; /* kill off newline from fgets() call */
argvlen += (t = (size_t)(q - l)) + (sizeof(" --")-1);
if (argvlen >= maxargvlen) {
maxargvlen = (t > maxargvlen) ? t*2 : maxargvlen*2;
argstr = (char*) xrealloc(argstr, maxargvlen);
assert(argstr); /* XXX can't happen */
if (argstr == NULL) return POPT_ERROR_MALLOC;
}
strcat(argstr, " --"); /* XXX stpcpy */
strcat(argstr, l); /* XXX stpcpy */
continue;
}
if (*q != '=')
continue; /* XXX for now, silently ignore bogus line */
/* *q is an equal sign. */
*q++ = '\0';
/* find next non-space letter of value */
while (*q != '\0' && _isspaceptr(q))
q++;
if (*q == '\0')
continue; /* XXX silently ignore missing value */
/* now, loop and strip all ending whitespace */
x = l + nl;
while (_isspaceptr(--x))
*x = '\0'; /* null out last char if space (including fgets() NL) */
/* rest of line accept */
t = (size_t)(x - l);
argvlen += t + (sizeof("' --='")-1);
if (argvlen >= maxargvlen) {
maxargvlen = (t > maxargvlen) ? t*2 : maxargvlen*2;
argstr = (char*) xrealloc(argstr, maxargvlen);
assert(argstr); /* XXX can't happen */
if (argstr == NULL) return POPT_ERROR_MALLOC;
}
strcat(argstr, " --"); /* XXX stpcpy */
//.........这里部分代码省略.........
示例14: grid_string_cells
/* Convert cells into a string. */
char *
grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
{
const struct grid_cell *gc;
static struct grid_cell lastgc1;
struct utf8_data ud;
const char* data;
char *buf, code[128];
size_t len, off, size, codelen;
u_int xx;
const struct grid_line *gl;
GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
if (lastgc != NULL && *lastgc == NULL) {
memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
*lastgc = &lastgc1;
}
len = 128;
buf = xmalloc(len);
off = 0;
gl = grid_peek_line(gd, py);
for (xx = px; xx < px + nx; xx++) {
if (gl == NULL || xx >= gl->cellsize)
break;
gc = &gl->celldata[xx];
if (gc->flags & GRID_FLAG_PADDING)
continue;
grid_cell_get(gc, &ud);
if (with_codes) {
grid_string_cells_code(*lastgc, gc, code, sizeof code,
escape_c0);
codelen = strlen(code);
memcpy(*lastgc, gc, sizeof *gc);
} else
codelen = 0;
data = ud.data;
size = ud.size;
if (escape_c0 && size == 1 && *data == '\\') {
data = "\\\\";
size = 2;
}
while (len < off + size + codelen + 1) {
buf = xrealloc(buf, 2, len);
len *= 2;
}
if (codelen != 0) {
memcpy(buf + off, code, codelen);
off += codelen;
}
memcpy(buf + off, data, size);
off += size;
}
if (trim) {
while (off > 0 && buf[off - 1] == ' ')
off--;
}
buf[off] = '\0';
return (buf);
}
示例15: rewrite
//.........这里部分代码省略.........
/* FALLTHROUGH */
case 'a':
pr->flags = F_ADDRESS;
++p2;
if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
goto DO_BAD_CONV_CHAR;
}
*p1 = p1[2];
break;
case 'c':
pr->flags = F_C;
/* *p1 = 'c'; set in conv_c */
goto DO_BYTE_COUNT_1;
case 'p':
pr->flags = F_P;
*p1 = 'c';
goto DO_BYTE_COUNT_1;
case 'u':
pr->flags = F_U;
/* *p1 = 'c'; set in conv_u */
goto DO_BYTE_COUNT_1;
default:
goto DO_BAD_CONV_CHAR;
}
} else {
DO_BAD_CONV_CHAR:
bb_error_msg_and_die("bad conversion character %%%s", p1);
}
/*
* copy to PR format string, set conversion character
* pointer, update original.
*/
savech = *p2;
p1[1] = '\0';
pr->fmt = xstrdup(fmtp);
*p2 = savech;
//Too early! xrealloc can move pr->fmt!
//pr->cchar = pr->fmt + (p1 - fmtp);
/* DBU:[[email protected]] w/o this, trailing fmt text, space is lost.
* Skip subsequent text and up to the next % sign and tack the
* additional text onto fmt: eg. if fmt is "%x is a HEX number",
* we lose the " is a HEX number" part of fmt.
*/
for (p3 = p2; *p3 && *p3 != '%'; p3++)
continue;
if (p3 > p2) {
savech = *p3;
*p3 = '\0';
pr->fmt = xrealloc(pr->fmt, strlen(pr->fmt) + (p3-p2) + 1);
strcat(pr->fmt, p2);
*p3 = savech;
p2 = p3;
}
pr->cchar = pr->fmt + (p1 - fmtp);
fmtp = p2;
/* only one conversion character if byte count */
if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
bb_error_msg_and_die("byte count with multiple conversion characters");
}
}
/*
* if format unit byte count not specified, figure it out
* so can adjust rep count later.
*/
if (!fu->bcnt)
for (pr = fu->nextpr; pr; pr = pr->nextpr)
fu->bcnt += pr->bcnt;
}
/*
* if the format string interprets any data at all, and it's
* not the same as the blocksize, and its last format unit
* interprets any data at all, and has no iteration count,
* repeat it as necessary.
*
* if, rep count is greater than 1, no trailing whitespace
* gets output from the last iteration of the format unit.
*/
for (fu = fs->nextfu; fu; fu = fu->nextfu) {
if (!fu->nextfu && fs->bcnt < dumper->blocksize
&& !(fu->flags & F_SETREP) && fu->bcnt
) {
fu->reps += (dumper->blocksize - fs->bcnt) / fu->bcnt;
}
if (fu->reps > 1) {
for (pr = fu->nextpr;; pr = pr->nextpr)
if (!pr->nextpr)
break;
for (p1 = pr->fmt, p2 = NULL; *p1; ++p1)
p2 = isspace(*p1) ? p1 : NULL;
if (p2)
pr->nospace = p2;
}
if (!fu->nextfu)
break;
}
}