某模块作为前台进程独立运行时,运行命令携带命令行参数;作为某平台下守护进程子进程运行时,需要将命令行参数固化在代码里。类似如下写法:

char *argv[] = {"./DslDriver", "-t", "/bin/VdslModemSco.bin"};

int argc = sizeof(argv) / sizeof(argv[0]);

     随后,调用basename函数(头文件为libgen.h)解析argv[0],即"./DslDriver"。实测发现,在Linux原生系统中解析正常,在某平台下解析时则会发生段错误。

     合理的想法自然是怀疑两种环境下basename函数的实现不同。Linux原生函数源码未找到,但某平台uclibc源码中可以找到basename函数的实现:

1 /* Return final component of PATH.
2    This is the weird XPG version of this function.  It sometimes will
3    modify its argument.  Therefore we normally use the GNU version (in
4    <string.h>) and only if this header is included make the XPG
5    version available under the real name.  */
6 extern char *__xpg_basename (char *__path) __THROW;
7 #define basename    __xpg_basename
Libgen.h

相关文章: