linux系统函数
1 源码中常出现的字符串函数
1.1 strrchr、strch 函数-返回字符在字符串中指针地址
1.2 getopt、getopt_long、getopt_long_only
1.3 字符串转换为整形的函数strtoull
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(int argc ,char ** argv)
{
char *memsuffix, *addrsuffix, *loopsuffix;
off_t physaddrbase = 0;
int opt;
while ((opt = getopt(argc, argv, "p:d:")) != -1){
switch (opt) {
case 'p':
errno = 0;
physaddrbase = (off_t) strtoull(optarg, &addrsuffix, 16);
if (errno != 0) {
fprintf(stderr,
"failed to parse physaddrbase arg; should be hex "
"address (0x123...)\n");
}
printf("optarg:%s physaddrbase:%ld ,addrsuffix:%s \n",optarg, physaddrbase, addrsuffix);
case 'd':
fprintf(stdout, "optarg: %s \n", optarg);
break;
default: /* '?' */
fprintf(stdout, "Nothing is done.");
}
}
return 0;
}
2 linux系统相关函数
2.1 getuid / geteuid 函数
geteuid() 函数 返回有效用户。
2.2 日志函数
3 字节序转换
3.1 小端字节序转换为网络字节序
float2big
/** * Small endian order to big endian order */ void float2big(float *data_p,uint8_t *dest) { dest[0] = ((uint8_t *)data_p)[3]; dest[1] = ((uint8_t *)data_p)[2]; dest[2] = ((uint8_t *)data_p)[1]; dest[3] = ((uint8_t *)data_p)[0]; }
3.2 网络字节序转换本地小端字节序
float2small
int float2small(uint8_t *buf, float * dest) { ((uint8_t*)dest)[0] = buf[3]; ((uint8_t*)dest)[1] = buf[2]; ((uint8_t*)dest)[2] = buf[1]; ((uint8_t*)dest)[3] = buf[0]; return 0; }