本文整理汇总了C++中safe_strdup函数的典型用法代码示例。如果您正苦于以下问题:C++ safe_strdup函数的具体用法?C++ safe_strdup怎么用?C++ safe_strdup使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了safe_strdup函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: StrArrayAdd
void StrArrayAdd(StrArray* arr, const char* str)
{
char** old_table;
if ((arr == NULL) || (arr->Table == NULL))
return;
if (arr->Index == arr->Max) {
arr->Max *= 2;
old_table = arr->Table;
arr->Table = (char**)realloc(arr->Table, arr->Max*sizeof(char*));
if (arr->Table == NULL) {
free(old_table);
uprintf("Could not reallocate string array\n");
return;
}
}
arr->Table[arr->Index] = safe_strdup(str);
if (arr->Table[arr->Index++] == NULL) {
uprintf("Could not store string in array\n");
}
}
示例2: fatal_error
/* You must free() this yourself. */
const vm_char *factor_vm::default_image_path()
{
vm_char full_path[MAX_UNICODE_PATH];
vm_char *ptr;
vm_char temp_path[MAX_UNICODE_PATH];
if(!GetModuleFileName(NULL, full_path, MAX_UNICODE_PATH))
fatal_error("GetModuleFileName() failed", 0);
if((ptr = wcsrchr(full_path, '.')))
*ptr = 0;
wcsncpy(temp_path, full_path, MAX_UNICODE_PATH - 1);
size_t full_path_len = wcslen(full_path);
if (full_path_len < MAX_UNICODE_PATH - 1)
wcsncat(temp_path, L".image", MAX_UNICODE_PATH - full_path_len - 1);
temp_path[MAX_UNICODE_PATH - 1] = 0;
return safe_strdup(temp_path);
}
示例3: lirc_channel_join
void lirc_channel_join(struct LIRCServer_struct* server,
struct LIRCClientData_struct* client,
char *channel)
{
char temp[MAX_IRC_MESSAGE_SIZE];
dlnode_t* client_node;
LIRCChannelData* c =
(LIRCChannelData*)find_element(server->channel_list,
channel, lirc_channel_cmp);
if (c == NULL)
{
/* The channel doesn't exist in the list so we
* will need to create one */
c = lirc_channel_new_metadata();
c->name = safe_strdup(channel);
insert_at_front(server->channel_list, c, sizeof(c));
}
/* Add this client to the list */
insert_at_front(c->client_list, client,
sizeof(client));
/* Add a weak reference to the joined channels list of the client */
insert_at_front(client->channel_list, c, sizeof(c));
/* Send a response message to all connected clients inside the
* channel indicating that a join was made */
client_node = c->client_list->head;
sprintf(temp, ":%s!~%[email protected] JOIN %s\r\n", client->nick, client->user,
channel);
while (client_node != NULL)
{
struct LIRCClientData_struct *client_node_data =
(struct LIRCClientData_struct *)client_node->data;
send(client_node_data->socket, temp, strlen(temp), 0);
client_node = client_node->next;
}
}
示例4: iptables_fw_find_mention
// add by lijg, 2013-05-30, Find keyword @mention in firewall rule table = @table and chain = @chain
// return : 0 - not found, 1 - found it .
int iptables_fw_find_mention(
const char * table,
const char * chain,
const char * mention
) {
FILE *p = NULL;
char *command = NULL;
char line[MAX_BUF];
char *victim = safe_strdup(mention);
int found = 0;
// 1.1 @victim="WiFiDog_br-lan_Trusted"
iptables_insert_gateway_id(&victim);
debug(LOG_DEBUG, "Attempting to find all mention of %s from %s.%s", victim, table, chain);
safe_asprintf(&command, "iptables -t %s -L %s -n --line-numbers -v", table, chain);
iptables_insert_gateway_id(&command);
//1.2 执行命令 iptables -t mangle -L PREROUTING -n --line-numbers -v, 逐行输出链中规则
if ((p = popen(command, "r"))) {
/* Skip first 2 lines */
while (!feof(p) && fgetc(p) != '\n');
while (!feof(p) && fgetc(p) != '\n');
/* Loop over entries */
while (fgets(line, sizeof(line), p)) { // 匹配链中每条规则
/* Look for victim */
if (strstr(line, victim)) { // 根据客户端IP地址进行匹配
found = 1;
break;
}
}
pclose(p);
}
free(command);
free(victim);
return (found);
}
示例5: w32g_add_playlist1
static int w32g_add_playlist1(char *filename, int uniq, int refine)
{
PlayListEntry *entry;
char *title;
struct midi_file_info *info;
if(uniq)
{
int i;
for(i = 0; i < playlist.nfiles; i++)
if(pathcmp(filename, playlist.list[i].filename, 0) == 0)
return 0;
}
title = get_midi_title(filename);
info = get_midi_file_info(filename, 1);
if(refine && info->format < 0)
return 0;
if(playlist.allocated == 0)
{
playlist.allocated = 32;
playlist.list = (PlayListEntry *)safe_malloc(playlist.allocated *
sizeof(PlayListEntry));
}
else if(playlist.nfiles == playlist.allocated)
{
playlist.allocated *= 2;
playlist.list = (PlayListEntry *)safe_realloc(playlist.list,
playlist.allocated *
sizeof(PlayListEntry));
}
entry = &playlist.list[playlist.nfiles];
entry->filename = safe_strdup(filename);
entry->title = title;
entry->info = info;
playlist.nfiles++;
w32g_shuffle_playlist_reset(1);
return 1;
}
示例6: get_iface_ip
char *
get_iface_ip(const char ifname[])
{
char addrbuf[INET6_ADDRSTRLEN+1];
const struct ifaddrs *cur;
struct ifaddrs *addrs;
s_config *config;
if(getifaddrs(&addrs) < 0) {
debug(LOG_ERR, "getifaddrs(): %s", strerror(errno));
return NULL;
}
config = config_get_config();
/* Set default address */
sprintf(addrbuf, config->ip6 ? "::" : "0.0.0.0");
/* Iterate all interfaces */
cur = addrs;
while(cur != NULL) {
if( (cur->ifa_addr != NULL) && (strcmp( cur->ifa_name, ifname ) == 0) ) {
if(config->ip6 && cur->ifa_addr->sa_family == AF_INET6) {
inet_ntop(AF_INET6, &((struct sockaddr_in6 *)cur->ifa_addr)->sin6_addr, addrbuf, sizeof(addrbuf));
break;
}
if(!config->ip6 && cur->ifa_addr->sa_family == AF_INET) {
inet_ntop(AF_INET, &((struct sockaddr_in *)cur->ifa_addr)->sin_addr, addrbuf, sizeof(addrbuf));
break;
}
}
cur = cur->ifa_next;
}
freeifaddrs(addrs);
return safe_strdup(addrbuf);
}
示例7: sym_bi_gets
/*---------------------------------------------------------------------------
* Purpose: Return the string value of a builtin symbol.
*
* Return: Copy of the string value or NULL
*
* Programmer: Robb Matzke
* Friday, June 2, 2000
*
* Modifications:
*---------------------------------------------------------------------------
*/
char *
sym_bi_gets(const char *name)
{
char fullname[1024], *retval;
obj_t var, val;
/* Add built-in prefix */
if (*name!='$') {
fullname[0] = '$';
strcpy(fullname+1, name);
name = fullname;
}
var = obj_new(C_SYM, name);
val = sym_vboundp(var);
var = obj_dest(var);
retval = safe_strdup(obj_name(val));
obj_dest(val);
return retval;
}
示例8: guestfs_impl_get_sockdir
/* Note this actually calculates the sockdir, so it never returns NULL. */
char *
guestfs_impl_get_sockdir (guestfs_h *g)
{
const char *str;
uid_t euid = geteuid ();
if (euid == 0) {
/* Use /tmp exclusively for root, as otherwise qemu (running as
* qemu.qemu when launched by libvirt) will not be able to access
* the directory.
*/
str = "/tmp";
} else {
if (g->env_runtimedir)
str = g->env_runtimedir;
else
str = "/tmp";
}
return safe_strdup (g, str);
}
示例9: guestfs__inspect_get_type
char *
guestfs__inspect_get_type (guestfs_h *g, const char *root)
{
struct inspect_fs *fs = guestfs___search_for_root (g, root);
if (!fs)
return NULL;
char *ret;
switch (fs->type) {
case OS_TYPE_DOS:
ret = safe_strdup (g, "dos");
break;
case OS_TYPE_FREEBSD:
ret = safe_strdup (g, "freebsd");
break;
case OS_TYPE_HURD:
ret = safe_strdup (g, "hurd");
break;
case OS_TYPE_LINUX:
ret = safe_strdup (g, "linux");
break;
case OS_TYPE_NETBSD:
ret = safe_strdup (g, "netbsd");
break;
case OS_TYPE_OPENBSD:
ret = safe_strdup (g, "openbsd");
break;
case OS_TYPE_WINDOWS:
ret = safe_strdup (g, "windows");
break;
case OS_TYPE_UNKNOWN:
default:
ret = safe_strdup (g, "unknown");
break;
}
return ret;
}
示例10: c_locale_vsnprintf
int
c_locale_vsnprintf (char *str, size_t size, const char *format, va_list ap)
{
#ifdef _MSC_VER
# define vsnprintf _vsnprintf
#endif
int result;
char *locale;
locale = safe_strdup(setlocale(LC_ALL, NULL));
setlocale(LC_ALL, "C");
result = vsnprintf(str, size, format, ap);
setlocale(LC_ALL, locale);
free(locale);
return result;
}
示例11: iptables_compile
/**
* @internal
* Compiles a struct definition of a firewall rule into a valid iptables
* command.
* @arg table Table containing the chain.
* @arg chain Chain that the command will be (-A)ppended to.
* @arg rule Definition of a rule into a struct, from conf.c.
*/
static char *
iptables_compile(const char * table, const char *chain, const t_firewall_rule *rule)
{
char command[MAX_BUF],
*mode;
memset(command, 0, MAX_BUF);
switch (rule->target){
case TARGET_DROP:
mode = safe_strdup("DROP");
break;
case TARGET_REJECT:
mode = safe_strdup("REJECT");
break;
case TARGET_ACCEPT:
mode = safe_strdup("ACCEPT");
break;
case TARGET_LOG:
mode = safe_strdup("LOG");
break;
case TARGET_ULOG:
mode = safe_strdup("ULOG");
break;
}
snprintf(command, sizeof(command), "-t %s -A %s ",table, chain);
if (rule->mask != NULL) {
snprintf((command + strlen(command)), (sizeof(command) -
strlen(command)), "-d %s ", rule->mask);
}
if (rule->protocol != NULL) {
snprintf((command + strlen(command)), (sizeof(command) -
strlen(command)), "-p %s ", rule->protocol);
}
if (rule->port != NULL) {
snprintf((command + strlen(command)), (sizeof(command) -
strlen(command)), "--dport %s ", rule->port);
}
snprintf((command + strlen(command)), (sizeof(command) -
strlen(command)), "-j %s", mode);
free(mode);
/* XXX The buffer command, an automatic variable, will get cleaned
* off of the stack when we return, so we strdup() it. */
return(safe_strdup(command));
}
示例12: guestfs__set_attach_method
int
guestfs__set_attach_method (guestfs_h *g, const char *method)
{
if (STREQ (method, "appliance")) {
g->attach_method = ATTACH_METHOD_APPLIANCE;
free (g->attach_method_arg);
g->attach_method_arg = NULL;
}
else if (STRPREFIX (method, "unix:") && strlen (method) > 5) {
g->attach_method = ATTACH_METHOD_UNIX;
free (g->attach_method_arg);
g->attach_method_arg = safe_strdup (g, method + 5);
/* Note that we don't check the path exists until launch is called. */
}
else {
error (g, "invalid attach method: %s", method);
return -1;
}
return 0;
}
示例13: guestfs_set_private
void
guestfs_set_private (guestfs_h *g, const char *key, void *data)
{
if (g->pda == NULL) {
g->pda = hash_initialize (16, NULL, hasher, comparator, freer);
if (g->pda == NULL)
g->abort_cb ();
}
struct pda_entry *new_entry = safe_malloc (g, sizeof *new_entry);
new_entry->key = safe_strdup (g, key);
new_entry->data = data;
struct pda_entry *old_entry = hash_delete (g->pda, new_entry);
freer (old_entry);
struct pda_entry *entry = hash_insert (g->pda, new_entry);
if (entry == NULL)
g->abort_cb ();
assert (entry == new_entry);
}
示例14: guestfs_impl_set_identifier
int
guestfs_impl_set_identifier (guestfs_h *g, const char *identifier)
{
size_t i, len;
/* Check the identifier contains only permitted characters. */
len = strlen (identifier);
for (i = 0; i < len; ++i) {
char c = identifier[i];
if (!c_isalnum (c) && c != '_' && c != '-') {
error (g, _("identifier must contain only alphanumeric characters, underscore or minus sign"));
return -1;
}
}
free (g->identifier);
g->identifier = safe_strdup (g, identifier);
return 0;
}
示例15: update_tags
static int update_tags(notmuch_message_t *msg, const char *tags)
{
char *tag = NULL, *end = NULL, *p;
char *buf = safe_strdup(tags);
if (!buf)
return -1;
notmuch_message_freeze(msg);
for (p = buf; p && *p; p++) {
if (!tag && isspace(*p))
continue;
if (!tag)
tag = p; /* begin of the tag */
if (*p == ',' || *p == ' ')
end = p; /* terminate the tag */
else if (*(p + 1) == '\0')
end = p + 1; /* end of optstr */
if (!tag || !end)
continue;
if (tag >= end)
break;
*end = '\0';
if (*tag == '-') {
dprint(1, (debugfile, "nm: remove tag: '%s'\n", tag + 1));
notmuch_message_remove_tag(msg, tag + 1);
} else {
dprint(1, (debugfile, "nm: add tag: '%s'\n", *tag == '+' ? tag + 1 : tag));
notmuch_message_add_tag(msg, *tag == '+' ? tag + 1 : tag);
}
end = tag = NULL;
}
notmuch_message_thaw(msg);
FREE(&buf);
return 0;
}