我能知道 sscanf 是否修改了传递给它的字符串吗?或者有人可以指出我在哪里可以找到 sscanf 的源代码吗?
我下载了 glibc 并发现了以下内容:
extern int sscanf (__const char *__restrict __s,
__const char *__restrict __format, ...) __THROW;
extern int sscanf (__const char *__restrict __s,
__const char *__restrict __format, ...) __THROW;
Run Code Online (Sandbox Code Playgroud) 我在这里做错了什么?我希望settimeofday()更改系统时间,而不是 return EINVAL。
$ uname -a
Linux io 4.3.5-300.fc23.x86_64 #1 SMP Mon Feb 1 03:18:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
$ cat settimeofday.c
#include <sys/time.h>
#include <stdio.h>
int main()
{
struct timeval tv = {0, 0};
if (settimeofday(&tv, 0) == -1)
perror("settimeofday");
}
$ gcc settimeofday.c
$ sudo ./a.out
settimeofday: Invalid argument
Run Code Online (Sandbox Code Playgroud)
该错误来自运行 Fedora 23 的 Thinkpad T450。相同的代码在 OS X 上运行良好。
编辑
澄清一下,该命令正在以 root 身份执行:
# whoami
root
# sudo ./a.out
settimeofday: Invalid argument
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,如果我以普通用户身份运行程序,我会得到 EPERM …
我正在 CentOS 6.5 上工作。我的glibc版本是2.14,我不小心将/lib64/libc.so.6链接到了以前的glibc(libc-2.12.so)。
然后我运行,除了和 之外export LD_PRELOAD=/lib64/libc-2.14.so,大多数命令都可以执行。运行时,它给出“分段错误”。当 run 时,它给出"su: /lib64/libc.so.6: version `GLIBC_2.14' not find (required by /lib64/libcrypt.so.1)"。susudosusudo
如何设置LD_PRELOAD和su,sudo或者如何恢复 glibc?
我有一个程序getopt_get()用来解析命令行参数。我的代码是这样的:
int opt;
int optionIndex;
static struct option longOptions[] = {
{"help", no_argument, NULL, 'h'},
{"test", no_argument, NULL, 't'},
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "ht", longOptions, &optionIndex)) != -1) {
switch (opt) {
case 'h':
help();
return 0;
break;
case 't':
init();
test();
return 0;
break;
case '?':
help();
return 1;
break;
default:
printf("default.\n");
}
Run Code Online (Sandbox Code Playgroud)
当我将正确的命令行参数传递给程序时,它运行良好。但是当错误的参数传递给程序时,它会打印出像这样烦人和多余的词。
例如,我将错误的参数“q”传递给程序
$ ./program -q
./program: invalid option -- 'q'
Usage: -h -t
Run Code Online (Sandbox Code Playgroud)
当有错误的参数时,我只希望它运行我的函数help()而不打印任何单词。
./program: invalid …Run Code Online (Sandbox Code Playgroud) 我试图了解 glibc 的 malloc 在我的 64 位机器上到底是如何进行簿记的。
根据文档,它在块之前存储实际大小(malloc 值加上簿记字节)。所以我从这里获取了以下代码:
int *a = (int *) malloc(4);
int *b = (int *) malloc(7);
int *c = (int *) malloc(1);
int *d = (int *) malloc(32);
int *e = (int *) malloc(4);
printf("0x%x\n", a);
printf("0x%x\n", b);
printf("0x%x\n", c);
printf("0x%x\n", d);
printf("0x%x\n", e);
printf("a[-1] = %d, a[-2] = %d\n", a[-1], a[-2]);
printf("b[-1] = %d, b[-2] = %d\n", b[-1], b[-2]);
printf("c[-1] = %d, c[-2] = %d\n", c[-1], c[-2]);
printf("d[-1] = %d, d[-2] = …Run Code Online (Sandbox Code Playgroud) 当我遇到以下错误消息时,我正在按照本文编译最终gcc-9.2.0版本:glibc-2.31make && make install
In file included from ../../../../libsanitizer/sanitizer_common/sanitizer
_platform_limits_posix.cc:193:
../../../../libsanitizer/sanitizer_common/sanitizer_internal_defs.h:339:7
2: error: narrowing conversion of '-1' from 'int' to 'long unsigned int'
[-Wnarrowing]
339 | IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
| ^
../../../../libsanitizer/sanitizer_common/sanitizer_internal_defs.h:333:3
0: note: in expansion of macro 'IMPL_COMPILER_ASSERT'
333 | #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
| ^~~~~~~~~~~~~~~~~~~~
../../../../libsanitizer/sanitizer_common/sanitizer_platform_limits_posix
.h:1511:3: note: in expansion of macro 'COMPILER_CHECK'
1511 | COMPILER_CHECK(sizeof(((__sanitizer_##CLASS *) NULL)->MEMBER) =
= \
| ^~~~~~~~~~~~~~
../../../../libsanitizer/sanitizer_common/sanitizer_platform_limits_posix
.cc:1161:1: note: in expansion of macro 'CHECK_SIZE_AND_OFFSET'
1161 | …Run Code Online (Sandbox Code Playgroud) 我一直认为libc应该是一个独立的动态库,直到我发现这个:
$ ldd /lib/x86_64-linux-gnu/libc.so.6
/lib64/ld-linux-x86-64.so.2 (0x00007fd743c00000)
linux-vdso.so.1 (0x00007fffc75f4000)
Run Code Online (Sandbox Code Playgroud)
有人能告诉我为什么 libc 需要 ld.so 以及它使用什么功能吗?
我在以下位置glibc-2.33/ctype/ctype.c看到这段代码:
// [...]
#define __ctype_toupper \
((int32_t *) _NL_CURRENT (LC_CTYPE, _NL_CTYPE_TOUPPER) + 128)
// [...]
int
toupper (int c)
{
return c >= -128 && c < 256 ? __ctype_toupper[c] : c;
}
libc_hidden_def (toupper)
Run Code Online (Sandbox Code Playgroud)
我知道它正在检查是否c在 -128 和 256(包括)范围内,如果超出该范围则按原样返回字符,但这是什么_NL_CURRENT (LC_CTYPE, _NL_CTYPE_TOUPPER) + 128)意思以及我在哪里实际找到字母大写的源代码?这似乎是在查找当前的语言环境,我只对 感兴趣en_US.UTF-8。另外,一个角色怎么可能是负面的呢?
我不glibc具体关心,我只是想知道所有 ASCII 字符(从 NUL 到 DEL 的所有字符)在 C 中是如何大写的。
我有一个 C++ 测试程序,可以让 CPU 保持忙碌:
\n#include <cstdint>\n#include <iostream>\n\n// Linear-feedback shift register\nuint64_t lfsr1(uint64_t max_ix)\n{\n uint64_t start_state = 0xACE1u; /* Any nonzero start state will work. */\n uint64_t lfsr = start_state;\n uint64_t bit; /* Must be 16-bit to allow bit<<15 later in the code */\n\n for (uint64_t ix = 0; ix < max_ix; ++ix)\n { /* taps: 16 14 13 11; feedback polynomial: x^16 + x^14 + x^13 + x^11 + 1 */\n bit = ((lfsr >> 0) ^ (lfsr >> …Run Code Online (Sandbox Code Playgroud)