本文整理汇总了C++中safe_strncpy函数的典型用法代码示例。如果您正苦于以下问题:C++ safe_strncpy函数的具体用法?C++ safe_strncpy怎么用?C++ safe_strncpy使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了safe_strncpy函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char **argv)
{
struct sockaddr sa;
char host[128];
struct aftype *ap;
struct hwtype *hw;
struct ifreq ifr;
int goterr = 0, didnetmask = 0;
char **spp;
int fd;
#if HAVE_AFINET6
extern struct aftype inet6_aftype;
struct sockaddr_in6 sa6;
struct in6_ifreq ifr6;
unsigned long prefix_len;
char *cp;
#endif
#if I18N
bindtextdomain("net-tools", "/usr/share/locale");
textdomain("net-tools");
#endif
/* Create a channel to the NET kernel. */
if ((skfd = sockets_open(0)) < 0) {
perror("socket");
exit(1);
}
/* Find any options. */
argc--;
argv++;
while (argc && *argv[0] == '-') {
if (!strcmp(*argv, "-a"))
opt_a = 1;
if (!strcmp(*argv, "-v"))
opt_v = 1;
if (!strcmp(*argv, "-V") || !strcmp(*argv, "-version") ||
!strcmp(*argv, "--version"))
version();
if (!strcmp(*argv, "-?") || !strcmp(*argv, "-h") ||
!strcmp(*argv, "-help") || !strcmp(*argv, "--help"))
usage();
argv++;
argc--;
}
/* Do we have to show the current setup? */
if (argc == 0) {
int err = if_print((char *) NULL);
(void) close(skfd);
exit(err < 0);
}
/* No. Fetch the interface name. */
spp = argv;
safe_strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
if (*spp == (char *) NULL) {
int err = if_print(ifr.ifr_name);
(void) close(skfd);
exit(err < 0);
}
/* The next argument is either an address family name, or an option. */
if ((ap = get_aftype(*spp)) == NULL)
ap = get_aftype(DFLT_AF);
else {
/* XXX: should print the current setup if no args left, but only
for this family */
spp++;
addr_family = ap->af;
}
if (sockets_open(addr_family) < 0) {
perror("family socket");
exit(1);
}
/* Process the remaining arguments. */
while (*spp != (char *) NULL) {
if (!strcmp(*spp, "arp")) {
goterr |= clr_flag(ifr.ifr_name, IFF_NOARP);
spp++;
continue;
}
if (!strcmp(*spp, "-arp")) {
goterr |= set_flag(ifr.ifr_name, IFF_NOARP);
spp++;
continue;
}
#ifdef IFF_PORTSEL
if (!strcmp(*spp, "media") || !strcmp(*spp, "port")) {
if (*++spp == NULL)
usage();
if (!strcasecmp(*spp, "auto")) {
goterr |= set_flag(ifr.ifr_name, IFF_AUTOMEDIA);
} else {
int i, j, newport;
char *endp;
//.........这里部分代码省略.........
示例2: load_font_textures
int load_font_textures ()
{
#ifndef NEW_TEXTURES
int poor_man_save=poor_man;
int use_mipmaps_save=use_mipmaps;
#endif /* NEW_TEXTURES */
size_t i = 0;
char *glob_pattern;
#ifdef WINDOWS
struct _finddata_t c_file;
long hFile;
#else //WINDOWS
int ret;
glob_t glob_res;
size_t j;
#endif //WINDOWS
char file[60] = "";
char str[60] = "";
if (fonts[0] == NULL || fonts[1] == NULL || fonts[2] == NULL || fonts[3]==NULL )
{
for (i = 0; i < FONTS_ARRAY_SIZE; i++) {
if (fonts[i] != NULL)
free (fonts[i]);
fonts[i] = NULL;
}
if ( !init_fonts () ) return 0;
}
#ifndef NEW_TEXTURES
poor_man=0;
use_mipmaps=0;
#endif /* NEW_TEXTURES */
#ifdef NEW_TEXTURES
fonts[0]->texture_id = load_texture_cached("textures/font.dds", tt_font);
#else /* NEW_TEXTURES */
fonts[0]->texture_id = load_texture_cache("./textures/font.bmp", 0);
#endif /* NEW_TEXTURES */
i = 1;
// Force the selection of the base font.
add_multi_option("chat_font", "Type 1");
add_multi_option("name_font", "Type 1");
// Find what font's exist and load them
glob_pattern = malloc(strlen(datadir)+sizeof(texture_dir)+10+1); //+10 = font*.bmp*
#ifdef NEW_TEXTURES
sprintf(glob_pattern, "%s%sfont*.dds", datadir, texture_dir);
#else /* NEW_TEXTURES */
sprintf(glob_pattern, "%s%sfont*.bmp*", datadir, texture_dir);
#endif /* NEW_TEXTURES */
#ifdef WINDOWS
if( (hFile = _findfirst( glob_pattern, &c_file )) == -1L ){
free(glob_pattern);
return 0;
}
do {
int len;
safe_strncpy(file, c_file.name, sizeof(file));
#else //!WINDOWS
ret = glob(glob_pattern, 0, NULL, &glob_res);
if(ret != 0) {
LOG_ERROR("Unable to find any font textures\n");
free(glob_pattern);
return 0;
}
j = 0;
while (j < glob_res.gl_pathc && i < FONTS_ARRAY_SIZE) {
int len;
safe_strncpy(file, glob_res.gl_pathv[j]+sizeof(texture_dir)-1+strlen(datadir), sizeof(file));
#endif //WINDOWS
len= strlen(file);
#ifdef NEW_TEXTURES
if (((len + sizeof(texture_dir) - 1) < sizeof(str)) && !strncasecmp(file, "font", 4)
&& has_suffix(file, len, ".dds", 4))
{
safe_snprintf(str, sizeof(str), "./textures/%s", file); //Use a relative path here, load_texture_cache_deferred() is using the path wrappers.
file[len - 4] = 0;
fonts[i]->texture_id = load_texture_cached(str, tt_font);
#else /* NEW_TEXTURES */
if (len+sizeof(texture_dir)-1 < sizeof(str) && !strncasecmp(file, "font", 4)
&& (has_suffix(file, len, ".bmp", 4) || has_suffix(file, len, ".bmp.gz", 7))
&& (!has_suffix(file, len, "_alpha.bmp", 10)) && (!has_suffix(file, len, "_alpha.bmp.gz", 13))) {
// Get the filename, remove the .bmp and add _alpha.bmp to a copy, then replace the .bmp
safe_snprintf(str, sizeof(str), "./textures/%s", file); //Use a relative path here, load_texture_cache_deferred() is using the path wrappers.
if(has_suffix(file, len, ".bmp.gz", 7)){
file[len - 7]= 0;
} else {
file[len - 4]= 0;
}
fonts[i]->texture_id = load_texture_cache_deferred(str, 0);
#endif /* NEW_TEXTURES */
safe_snprintf(font_names[i], sizeof(font_names[i]), "Type %i - %s", i + 1, file);
add_multi_option("chat_font", font_names[i]);
add_multi_option("name_font", font_names[i]);
i++;
}
#ifndef WINDOWS
j++;
//.........这里部分代码省略.........
示例3: add_to_netq
void add_to_netq(unsigned long ip, unsigned short port, int rating, int replace)
{
if (rating > 100) rating=100;
if (rating < 0) rating=0;
if (ip == ((unsigned long) -1) || ip == 0L) {
return;
}
struct in_addr in;
in.s_addr=ip;
char *t=inet_ntoa(in);
if (!port) port=CONFIG_port_DEFAULT;
if (t && ip != ((unsigned long) -1) && ip) {
char host[256];
safe_strncpy(host,t,sizeof(host));
if (is_accessable_addr(ip) && allowIP(ip)) {
sprintf(host+strlen(host),":%d",port);
int x;
int nh=g_lvnetcons.GetCount();
int bestpos=-1;
int lastrat=-1;
for (x = 0; x < nh; x ++) {
char text[256];
text[0]=0;
g_lvnetcons.GetText(x,2,text,sizeof(text));
int rat=atoi(text);
if (rating > rat && bestpos < 0) bestpos=x;
text[0]=0;
g_lvnetcons.GetText(x,1,text,sizeof(text));
if (!stricmp(text,host)) {
if (g_lvnetcons.GetParam(x)) return;
char rat[32];
g_lvnetcons.GetText(x,2,rat,sizeof(rat));
int irat=atoi(rat);
if (irat < rating) {
if (irat < 50)
rating = irat+1;
else rating=irat;
};
if (!replace && rating < lastrat) {
sprintf(rat,"%d",rating);
g_lvnetcons.SetItemText(x,2,rat);
return;
};
g_lvnetcons.DeleteItem(x--);
nh--;
break;
};
lastrat=rat;
};
if (bestpos < 0) bestpos=nh;
char buf[32];
sprintf(buf,"%d",rating);
g_lvnetcons.InsertItem(bestpos,"",0);
g_lvnetcons.SetItemText(bestpos,1,host);
g_lvnetcons.SetItemText(bestpos,2,buf);
};
};
}
示例4: ExecuteMacro
int
ExecuteMacro(char *argv, int namelength)
{
RXSTRING rxRc;
RXSTRING rxArg[2];
int rxArgCount = 0;
char pszName[CCHMAXPATH];
char *rxArgStr;
short sRc;
long rc;
if (namelength >= sizeof(pszName))
return 1;
/* FIXME HBB 20010121: 3rd argument doesn't make sense. Either
* this should be sizeof(pszName), or it shouldn't use
* safe_strncpy(), here */
safe_strncpy(pszName, argv, namelength + 1);
rxArgStr = &argv[namelength];
RXSTRPTR(rxRc) = NULL;
#if 0
/*
C-like calling of function: program name is first
parameter.
In REXX you would have to use
Parse Arg param0, param1
to get the program name in param0 and the arguments in param1.
Some versions before gnuplot 3.7pl1 used a similar approach but
passed program name and arguments in a single string:
(==> Parse Arg param0 param1)
*/
MAKERXSTRING(rxArg[0], pszName, strlen(pszName));
rxArgCount++;
if (*rxArgStr) {
MAKERXSTRING(rxArg[1], rxArgStr, strlen(rxArgStr));
rxArgCount++;
}
#else
/*
REXX standard calling (gnuplot 3.7pl1 and above):
The program name is not supplied and so all actual arguments
are in a single string:
Parse Arg param
We even handle blanks like cmd.exe when calling REXX programs.
*/
if (*rxArgStr) {
MAKERXSTRING(rxArg[0], rxArgStr, strlen(rxArgStr));
rxArgCount++;
}
#endif
CallFromRexx = TRUE;
rc = RexxStart(
rxArgCount,
rxArg,
pszName,
NULL,
"GNUPLOT",
RXCOMMAND,
NULL,
&sRc,
&rxRc);
CallFromRexx = FALSE;
/* am: a word WRT errors codes:
the negative ones don't seem to have symbolic names, you can get
them from the OREXX reference, they're not in REXX Programming Guide -
no idea where to retrieve them from a Warp 3 reference ??
The positive ones are somehow referenced in REXXPG
*/
if (rc < 0) {
/* REXX error */
} else if (rc > 0) {
/* Interpreter couldn't be started */
if (rc == -4)
/* run was cancelled, but don't give error message */
rc = 0;
} else if (rc==0) {
/* all was fine */
}
/* We don't we try to use rxRc ?
BTW, don't use free() instead since it's allocated inside RexxStart()
and not in our executable using the EMX libraries */
if (RXSTRPTR(rxRc))
/* I guess it's NULL if something major went wrong,
NULL strings are usually not part of the REXX language ... */
DosFreeMem(rxRc.strptr);
return rc;
}
示例5: func_label
static void func_label(char *buf, int size, const procps_status_t *ps)
{
safe_strncpy(buf, ps->context ? ps->context : "unknown", size+1);
}
示例6: rtmon_check_updates
void rtmon_check_updates(struct rtmon_t *rtmon) {
int i, j;
for (i=0; i < rtmon->_route_sz; i++) {
if (rtmon->_routes[i].has_data) {
if (rtmon->_routes[i].destination.s_addr == 0) {
log_dbg("Default Route %s", inet_ntoa(rtmon->_routes[i].gateway));
for (j=0; j < rtmon->_iface_sz; j++) {
if (rtmon->_ifaces[j].has_data) {
if (rtmon->_routes[i].if_index == rtmon->_ifaces[j].index) {
struct arpreq areq;
struct sockaddr_in *sin;
int s, attempt=0, retries=3;
log_dbg("Route Interface %s", rtmon->_ifaces[j].devname);
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
return;
}
memset(&areq, 0, sizeof(areq));
sin = (struct sockaddr_in *) &areq.arp_pa;
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = rtmon->_routes[i].gateway.s_addr;
safe_strncpy(areq.arp_dev, rtmon->_ifaces[j].devname, sizeof(areq.arp_dev));
while (attempt < retries) {
struct sockaddr_in addr;
char b[1]={0};
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr = sin->sin_addr;
addr.sin_port = htons(10000);
if (sendto(s, b, sizeof(b), 0,
(struct sockaddr *) &addr,
sizeof(addr)) < 0)
perror("sendto");
if (ioctl(s, SIOCGARP, (caddr_t) &areq) == -1) {
if (errno == ENXIO) {
log_dbg("%s -- no entry\n", inet_ntoa(sin->sin_addr));
attempt++;
sleep(1);
continue;
}
else { perror("SIOCGARP"); break; }
} else {
log_dbg("MAC %s", mactoa((uint8_t *)&areq.arp_ha.sa_data));
memcpy(rtmon->_routes[i].gwaddr, &areq.arp_ha.sa_data, sizeof(rtmon->_routes[i].gwaddr));
if (rtmon->cb(rtmon, &rtmon->_ifaces[j], &rtmon->_routes[i]))
log_err(errno, "callback failed");
break;
}
}
close(s);
return;
}
}
}
}
}
}
}
示例7: rtmon_discover_ifaces
void rtmon_discover_ifaces(struct rtmon_t *rtmon) {
struct rtmon_iface ri;
struct ifconf ic;
int fd, len, i;
for (i=0; i < rtmon->_iface_sz; i++)
if (rtmon->_ifaces[i].has_data)
rtmon->_ifaces[i].has_data |= RTMON_REMOVE;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
return;
}
ic.ifc_buf=0;
ic.ifc_len=0;
if (ioctl(fd, SIOCGIFCONF, &ic) < 0) {
close(fd);
return;
}
ic.ifc_buf = calloc((size_t)ic.ifc_len, 1);
if (ioctl(fd, SIOCGIFCONF, &ic) < 0) {
close(fd);
free(ic.ifc_buf);
return;
}
len = (ic.ifc_len/sizeof(struct ifreq));
for (i=0; i < len; ++i) {
struct ifreq *ifr = (struct ifreq *)&ic.ifc_req[i];
memset(&ri, 0, sizeof(ri));
/* device name and address */
safe_strncpy(ri.devname, ifr->ifr_name, sizeof(ri.devname));
ri.address = inaddr(ifr_addr);
/* index */
if (-1 < ioctl(fd, SIOCGIFINDEX, (caddr_t) ifr)) {
ri.index = ifr->ifr_ifindex;
}
/* netmask */
if (-1 < ioctl(fd, SIOCGIFNETMASK, (caddr_t)ifr)) {
ri.netmask = inaddr(ifr_addr);
}
ri.network.s_addr = ri.address.s_addr & ri.netmask.s_addr;
/* hardware address */
#ifdef SIOCGIFHWADDR
if (-1 < ioctl(fd, SIOCGIFHWADDR, (caddr_t)ifr)) {
switch (ifr->ifr_hwaddr.sa_family) {
case ARPHRD_PPP:
break;
case ARPHRD_NETROM:
case ARPHRD_ETHER:
case ARPHRD_EETHER:
case ARPHRD_IEEE802:
{
unsigned char *u = (unsigned char *)&ifr->ifr_addr.sa_data;
memcpy(ri.hwaddr, u, 6);
}
break;
}
}
#else
#ifdef SIOCGENADDR
if (-1 < ioctl(fd, SIOCGENADDR, (caddr_t)ifr)) {
unsigned char *u = (unsigned char *)&ifr->ifr_enaddr;
memcpy(ri.hwaddr, u, 6);
}
#else
#warning Do not know how to find interface hardware address
#endif /* SIOCGENADDR */
#endif /* SIOCGIFHWADDR */
/* flags */
if (-1 < ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr)) {
ri.devflags = ifr->ifr_flags;
}
/* point-to-point gateway */
if (ri.devflags & IFF_POINTOPOINT) {
if (-1 < ioctl(fd, SIOCGIFDSTADDR, (caddr_t)ifr)) {
ri.gateway = inaddr(ifr_addr);
}
}
/* broadcast address */
if (ri.devflags & IFF_BROADCAST) {
if (-1 < ioctl(fd, SIOCGIFBRDADDR, (caddr_t)ifr)) {
ri.broadcast = inaddr(ifr_addr);
}
}
if (-1 < ioctl(fd, SIOCGIFMTU, (caddr_t)ifr)) {
//.........这里部分代码省略.........
示例8: load_instance_struct
ncInstance * load_instance_struct (const char * instanceId)
{
const int meta_size = sizeof (struct ncInstance_t);
ncInstance * instance = calloc (1, meta_size);
if (instance==NULL) {
logprintfl (EUCADEBUG, "load_instance_struct: out of memory for instance struct\n");
return NULL;
}
safe_strncpy (instance->instanceId, instanceId, sizeof (instance->instanceId));
// we don't know userId, so we'll look for instanceId in every user's
// directory (we're assuming that instanceIds are unique in the system)
char user_paths [MAX_PATH];
set_path (user_paths, sizeof (user_paths), NULL, NULL);
DIR * insts_dir = opendir(user_paths);
if (insts_dir == NULL) {
logprintfl (EUCADEBUG, "load_instance_struct: failed to open %s\n", user_paths);
goto free;
}
struct dirent * dir_entry;
while ((dir_entry = readdir (insts_dir)) != NULL) {
char tmp_path [MAX_PATH];
struct stat mystat;
snprintf(tmp_path, sizeof (tmp_path), "%s/%s/%s", user_paths, dir_entry->d_name, instance->instanceId);
if (stat(tmp_path, &mystat)==0) {
safe_strncpy (instance->userId, dir_entry->d_name, sizeof (instance->userId));
break; // found it
}
}
closedir (insts_dir);
if (strlen(instance->userId)<1) {
logprintfl (EUCADEBUG, "load_instance_struct: didn't find instance %s\n", instance->instanceId);
goto free;
}
int fd;
char checkpoint_path [MAX_PATH];
set_path (checkpoint_path, sizeof (checkpoint_path), instance, "instance.checkpoint");
if ((fd = open(checkpoint_path, O_RDONLY)) < 0
|| read (fd, instance, meta_size) < meta_size) {
logprintfl(EUCADEBUG, "load_instance_struct: failed to load metadata for %s from %s: %s\n", instance->instanceId, checkpoint_path, strerror (errno));
if(fd >= 0)
close (fd);
goto free;
}
close (fd);
instance->stateCode = NO_STATE;
// clear out pointers, since they are now wrong
instance->params.root = NULL;
instance->params.kernel = NULL;
instance->params.ramdisk = NULL;
instance->params.swap = NULL;
instance->params.ephemeral0 = NULL;
vbr_parse (&(instance->params), NULL); // fix up the pointers
return instance;
free:
if (instance) free (instance);
return NULL;
}
示例9: strncat
bool DOS_Shell::Execute(char * name,char * args) {
/* return true => don't check for hardware changes in do_command
* return false => check for hardware changes in do_command */
char fullname[DOS_PATHLENGTH+4]; //stores results from Which
char* p_fullname;
char line[CMD_MAXLINE];
if(strlen(args)!= 0){
if(*args != ' '){ //put a space in front
line[0]=' ';line[1]=0;
strncat(line,args,CMD_MAXLINE-2);
line[CMD_MAXLINE-1]=0;
}
else
{
safe_strncpy(line,args,CMD_MAXLINE);
}
}else{
line[0]=0;
};
/* check for a drive change */
if (((strcmp(name + 1, ":") == 0) || (strcmp(name + 1, ":\\") == 0)) && isalpha(*name))
{
if (!DOS_SetDrive(toupper(name[0])-'A')) {
WriteOut(MSG_Get("SHELL_EXECUTE_DRIVE_NOT_FOUND"),toupper(name[0]));
}
return true;
}
/* Check for a full name */
p_fullname = Which(name);
if (!p_fullname) return false;
strcpy(fullname,p_fullname);
const char* extension = strrchr(fullname,'.');
__android_log_print(ANDROID_LOG_INFO, "dosbox", "command fullname:%s", fullname);
/*always disallow files without extension from being executed. */
/*only internal commands can be run this way and they never get in this handler */
if(extension == 0)
{
//Check if the result will fit in the parameters. Else abort
if(strlen(fullname) >( DOS_PATHLENGTH - 1) ) return false;
char temp_name[DOS_PATHLENGTH+4],* temp_fullname;
//try to add .com, .exe and .bat extensions to filename
strcpy(temp_name,fullname);
strcat(temp_name,".COM");
temp_fullname=Which(temp_name);
if (temp_fullname) { extension=".com";strcpy(fullname,temp_fullname); }
else
{
strcpy(temp_name,fullname);
strcat(temp_name,".EXE");
temp_fullname=Which(temp_name);
if (temp_fullname) { extension=".exe";strcpy(fullname,temp_fullname);}
else
{
strcpy(temp_name,fullname);
strcat(temp_name,".BAT");
temp_fullname=Which(temp_name);
if (temp_fullname) { extension=".bat";strcpy(fullname,temp_fullname);}
else
{
return false;
}
}
}
}
if (strcasecmp(extension, ".bat") == 0)
{ /* Run the .bat file */
/* delete old batch file if call is not active*/
bool temp_echo=echo; /*keep the current echostate (as delete bf might change it )*/
if(bf && !call) delete bf;
bf=new BatchFile(this,fullname,name,line);
echo=temp_echo; //restore it.
}
else
{ /* only .bat .exe .com extensions maybe be executed by the shell */
if(strcasecmp(extension, ".com") !=0)
{
if(strcasecmp(extension, ".exe") !=0) return false;
}
/* Run the .exe or .com file from the shell */
/* Allocate some stack space for tables in physical memory */
reg_sp-=0x200;
//Add Parameter block
DOS_ParamBlock block(SegPhys(ss)+reg_sp);
block.Clear();
//Add a filename
RealPt file_name=RealMakeSeg(ss,reg_sp+0x20);
MEM_BlockWrite(Real2Phys(file_name),fullname,(Bitu)(strlen(fullname)+1));
/* HACK: Store full commandline for mount and imgmount */
full_arguments.assign(line);
/* Fill the command line */
//.........这里部分代码省略.........
示例10: main
int main(int argc, char **argv)
{
struct sockaddr sa;
struct sockaddr samask;
struct sockaddr_in sin;
char host[128];
struct aftype *ap;
struct hwtype *hw;
struct ifreq ifr;
int goterr = 0, didnetmask = 0, neednetmask=0;
char **spp;
int fd;
#if HAVE_AFINET6
extern struct aftype inet6_aftype;
struct sockaddr_in6 sa6;
struct in6_ifreq ifr6;
unsigned long prefix_len;
char *cp;
#endif
#if HAVE_AFINET
extern struct aftype inet_aftype;
#endif
#if I18N
setlocale (LC_ALL, "");
bindtextdomain("net-tools", "/usr/share/locale");
textdomain("net-tools");
#endif
/* Find any options. */
argc--;
argv++;
while (argc && *argv[0] == '-') {
if (!strcmp(*argv, "-a"))
opt_a = 1;
else if (!strcmp(*argv, "-s"))
ife_short = 1;
else if (!strcmp(*argv, "-v"))
opt_v = 1;
else if (!strcmp(*argv, "-V") || !strcmp(*argv, "-version") ||
!strcmp(*argv, "--version"))
version();
else if (!strcmp(*argv, "-?") || !strcmp(*argv, "-h") ||
!strcmp(*argv, "-help") || !strcmp(*argv, "--help"))
usage();
else {
fprintf(stderr, _("ifconfig: option `%s' not recognised.\n"),
argv[0]);
fprintf(stderr, _("ifconfig: `--help' gives usage information.\n"));
exit(1);
}
argv++;
argc--;
}
/* Create a channel to the NET kernel. */
if ((skfd = sockets_open(0)) < 0) {
perror("socket");
exit(1);
}
/* Do we have to show the current setup? */
if (argc == 0) {
int err = if_print((char *) NULL);
(void) close(skfd);
exit(err < 0);
}
/* No. Fetch the interface name. */
spp = argv;
safe_strncpy(ifr.ifr_name, *spp++, IFNAMSIZ);
if (*spp == (char *) NULL) {
int err = if_print(ifr.ifr_name);
(void) close(skfd);
exit(err < 0);
}
/* The next argument is either an address family name, or an option. */
if ((ap = get_aftype(*spp)) != NULL)
spp++; /* it was a AF name */
else
ap = get_aftype(DFLT_AF);
if (ap) {
addr_family = ap->af;
skfd = ap->fd;
}
/* Process the remaining arguments. */
while (*spp != (char *) NULL) {
if (!strcmp(*spp, "arp")) {
goterr |= clr_flag(ifr.ifr_name, IFF_NOARP);
spp++;
continue;
}
//.........这里部分代码省略.........
示例11: arpping
/* Returns 1 if no reply received */
int FAST_FUNC arpping(uint32_t test_nip,
const uint8_t *safe_mac,
uint32_t from_ip,
uint8_t *from_mac,
const char *interface)
{
int timeout_ms;
struct pollfd pfd[1];
#define s (pfd[0].fd) /* socket */
int rv = 1; /* "no reply received" yet */
struct sockaddr addr; /* for interface name */
struct arpMsg arp;
s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP));
if (s == -1) {
bb_perror_msg("%s", bb_msg_can_not_create_raw_socket);
return -1;
}
if (setsockopt_broadcast(s) == -1) {
bb_perror_msg("can't enable bcast on raw socket");
goto ret;
}
/* send arp request */
memset(&arp, 0, sizeof(arp));
memset(arp.h_dest, 0xff, 6); /* MAC DA */
memcpy(arp.h_source, from_mac, 6); /* MAC SA */
arp.h_proto = htons(ETH_P_ARP); /* protocol type (Ethernet) */
arp.htype = htons(ARPHRD_ETHER); /* hardware type */
arp.ptype = htons(ETH_P_IP); /* protocol type (ARP message) */
arp.hlen = 6; /* hardware address length */
arp.plen = 4; /* protocol address length */
arp.operation = htons(ARPOP_REQUEST); /* ARP op code */
memcpy(arp.sHaddr, from_mac, 6); /* source hardware address */
memcpy(arp.sInaddr, &from_ip, sizeof(from_ip)); /* source IP address */
/* tHaddr is zero-filled */ /* target hardware address */
memcpy(arp.tInaddr, &test_nip, sizeof(test_nip));/* target IP address */
memset(&addr, 0, sizeof(addr));
safe_strncpy(addr.sa_data, interface, sizeof(addr.sa_data));
if (sendto(s, &arp, sizeof(arp), 0, &addr, sizeof(addr)) < 0) {
// TODO: error message? caller didn't expect us to fail,
// just returning 1 "no reply received" misleads it.
goto ret;
}
/* wait for arp reply, and check it */
timeout_ms = 2000;
do {
typedef uint32_t aliased_uint32_t FIX_ALIASING;
int r;
unsigned prevTime = monotonic_ms();
pfd[0].events = POLLIN;
r = safe_poll(pfd, 1, timeout_ms);
if (r < 0)
break;
if (r) {
r = safe_read(s, &arp, sizeof(arp));
if (r < 0)
break;
//log3("sHaddr %02x:%02x:%02x:%02x:%02x:%02x",
// arp.sHaddr[0], arp.sHaddr[1], arp.sHaddr[2],
// arp.sHaddr[3], arp.sHaddr[4], arp.sHaddr[5]);
if (r >= ARP_MSG_SIZE
&& arp.operation == htons(ARPOP_REPLY)
/* don't check it: Linux doesn't return proper tHaddr (fixed in 2.6.24?) */
/* && memcmp(arp.tHaddr, from_mac, 6) == 0 */
&& *(aliased_uint32_t*)arp.sInaddr == test_nip
) {
/* if ARP source MAC matches safe_mac
* (which is client's MAC), then it's not a conflict
* (client simply already has this IP and replies to ARPs!)
*/
if (!safe_mac || memcmp(safe_mac, arp.sHaddr, 6) != 0)
rv = 0;
//else log2("sHaddr == safe_mac");
break;
}
}
timeout_ms -= (unsigned)monotonic_ms() - prevTime;
} while (timeout_ms > 0);
ret:
close(s);
log1("%srp reply received for this address", rv ? "No a" : "A");
return rv;
}
示例12: while
struct ax_routes *read_ax_routes(void)
{
FILE *fp;
char buffer[256], *cp, *cmd;
struct ax_routes *p=NULL, *list = NULL, *new_el;
int i = 0, k;
errno = 0;
if ((fp = fopen(AX_ROUTES_FILE, "r")) == NULL) return NULL;
while (fgets(buffer, 256, fp) != NULL) {
if (i++<1) continue;
if(*buffer=='#' || *buffer==' ' ) continue; /* commented line */
cp=strchr(buffer, '#'); /* ignore comments */
if (cp) *cp='\0';
cmd=strtok(buffer, " \t\n\r");
if(cmd==NULL) continue; /* empty line */
if (strcasecmp(cmd,"route")==0) { /* add route */
if ((new_el = calloc(1, sizeof(struct ax_routes))) == NULL) break;
safe_strncpy(new_el->dest_call, strupr(strtok(NULL, " \t\n\r")), 9);
safe_strncpy(new_el->alias, strupr(strtok(NULL, " \t\n\r")), 9);
safe_strncpy(new_el->dev, strtok(NULL, " \t\n\r"), 13);
safe_strncpy(new_el->conn_type, strupr(strtok(NULL, " \t\n\r")), 1);
safe_strncpy(new_el->description, strtok(NULL, "'\t\n\r"), 50);
if (new_el->description==NULL) strcpy(new_el->description," ");
switch(*new_el->conn_type) {
case CONN_TYPE_DIRECT:
{
break;
}
case CONN_TYPE_NODE:
{
safe_strncpy(new_el->digis[0], strupr(strtok(NULL, " \t\n\r")), 9);
break;
}
case CONN_TYPE_DIGI:
{
k=0;
while((cp=strtok(NULL, " \t\n\r"))!=NULL&&k<AX25_MAX_DIGIS)
safe_strncpy(new_el->digis[k++],strupr(cp),9);
while(k<AX25_MAX_DIGIS) strcpy(new_el->digis[k++],"\0");
break;
}
default:
{
return NULL;
break;
}
}
if(list==NULL) {
list=new_el;
p=list;
} else {
p->next = new_el;
p=p->next;
}
}
}
fclose(fp);
return list;
}
示例13: func_rgroup
static void func_rgroup(char *buf, int size, const procps_status_t *ps)
{
safe_strncpy(buf, get_cached_groupname(ps->rgid), size+1);
}
示例14: waste_srvmain_start
int waste_srvmain_start(int argc, char **argv)
{
SetProgramDirectory(argv[0]);
g_log_level=ds_Console;
_logfile=stderr;
{
bool dohelp=false;
if (argc<2) {
dohelp=true;
}
else {
if (!strcmp(argv[1],"-i")) {
log_printf(ds_Console,"Interactive!");
}
else if (!strcmp(argv[1],"-L")) {
char *szLI2;
szLI2=(char*)malloc(sK0[3]);
safe_strncpy(szLI2,(char*)sK1[3],sK0[3]);
dpi(szLI2,4);
RelpaceCr(szLI2);
fprintf(_logfile,"\n\n%s\n",szLI2);
memset(szLI2,0,sK0[3]);free(szLI2);
return 1;
}
else if (!strcmp(argv[1],"-d")) {
if (argc==3) {
log_printf(ds_Console,"Forking DAEMON!");
log_UpdatePath(argv[2],true);
#ifndef _WIN32
daemon(1,0);
#endif
log_printf(ds_Console,"DAEMON!");
}
else {
dohelp=true;
};
}
else {
dohelp=true;
};
};
if (dohelp) {
char *szCR2;
szCR2=(char*)malloc(sK0[1]);
safe_strncpy(szCR2,(char*)sK1[1],sK0[1]);
dpi(szCR2,2);
RelpaceCr(szCR2);
log_printf(ds_Console,
"%s\n"
"%s\n"
"\n"
"Usage: wastesrv <-i> | -d <logfile>\n"
"\t -L print license\n"
"\t -i interactive mode\n"
"\t -d daemon mode (on linux this will put wastesrv in the background)\n"
"\n"
"\twastesrv.ini must to be present on Windows\n"
"\tthe config is default.pr0 to default.pr4\n",
g_nameverstr,
szCR2
);
memset(szCR2,0,sK0[1]);free(szCR2);
return 1;
};
};
installsighandler();
log_printf(ds_Console,"%s starting up...",g_nameverstr);
MYSRAND();
if (!g_exit) //emergency break!
{
strcat(g_config_prefix,"default");
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
memset(&g_key,0,sizeof(g_key));
MessageBox(NULL,"Error initializing Winsock\n",APP_NAME " Error",0);
return 1;
}
#endif
UnifiedReadConfig();
InitialLoadDb();
PrepareDownloadDirectory();
if (!g_key.bits) {
reloadKey(
g_config->ReadInt(CONFIG_storepass,CONFIG_storepass_DEFAULT)?
g_config->ReadString(CONFIG_keypass,CONFIG_keypass_DEFAULT):
NULL
);
//.........这里部分代码省略.........
示例15: func_ruser
static void func_ruser(char *buf, int size, const procps_status_t *ps)
{
safe_strncpy(buf, get_cached_username(ps->ruid), size+1);
}