2 Star 6 Fork 1

FreeAC/FreeAC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
util.c 3.16 KB
一键复制 编辑 原始数据 按行查看 历史
FreeAC 提交于 2015-12-28 00:58 . start
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
* 版权所有 2014-2015 成都星锐蓝海网络科技有限公司
* 商业许可请联系 +86-18682011860 QQ:66442834
*
*/
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include "xyz_log.h"
#include "util.h"
int utilSetNonblock(int fd)
{
int flags = 0;
if (fd < 0) {
xyz_log_error("argument is error");
return -1;
}
if ((flags = fcntl(fd, F_GETFL, 0)) == -1) {
xyz_log_error("fcntl : %s", strerror(errno));
return -1;
} else if (fcntl(fd, F_SETFL, flags | O_NONBLOCK | O_ASYNC) != 0) {
xyz_log_error("fcntl : %s", strerror(errno));
return -1;
}
return 0;
}
int utilDoCMD(const char *cmd)
{
int rc = 0;
FILE *fp = NULL;
if (!cmd) {
xyz_log_error("Arg error");
return -1;
}
fp = popen(cmd, "r");
if (!fp) {
xyz_log_error("popen : %s", strerror(errno));
return -1;
}
rc = pclose(fp);
rc = WEXITSTATUS(rc);
if (0 == rc) {
return 0;
} else {
xyz_log_error("Do \"%s\" result failed, code %d", cmd, rc);
return -1;
}
return 0;
}
int utilByteMAC(const char *s, uint8_t *m)
{
int rc = 0;
char p = 0;
static char *template = "%02X%[:-]%02X%[:-]%02X%[:-]%02X%[:-]%02X%[:-]%02X";
if (!s || !m) {
xyz_log_error("Args error");
return -1;
}
if (strlen(s) != 17) {
xyz_log_error("String MAC format is error");
return -1;
}
rc = sscanf(s, template,
&(m[0]), &p,
&(m[1]), &p,
&(m[2]), &p,
&(m[3]), &p,
&(m[4]), &p,
&(m[5]));
if (rc != 11) {
xyz_log_error("sscanf : %s", strerror(errno));
rc = -1;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
#if 0
int main(int argc, char **argv)
{
char *s1 = "00-01-02-03-04-05";
char *s2 = "00:01:02:03:04:05";
char *s3 = "AA-01-02-03-04-05";
char *s4 = "00-01-02-aa-04-05";
uint8_t mac[6] = {0};
logOpen("stdout", 7);
if (utilByteMAC(s1, mac)) {
xyz_log_error("MAC parse error");
return -1;
}
logDebug("%02X:%02X:%02X:%02X:%02X:%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
memset(mac, 0, sizeof(mac));
if (utilByteMAC(s2, mac)) {
xyz_log_error("MAC parse error");
return -1;
}
logDebug("%02X:%02X:%02X:%02X:%02X:%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
memset(mac, 0, sizeof(mac));
if (utilByteMAC(s3, mac)) {
xyz_log_error("MAC parse error");
return -1;
}
logDebug("%02X:%02X:%02X:%02X:%02X:%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
memset(mac, 0, sizeof(mac));
if (utilByteMAC(s4, mac)) {
xyz_log_error("MAC parse error");
return -1;
}
logDebug("%02X:%02X:%02X:%02X:%02X:%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
memset(mac, 0, sizeof(mac));
return 0;
}
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/freeac/FreeAC.git
git@gitee.com:freeac/FreeAC.git
freeac
FreeAC
FreeAC
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385