我知道 unix 用户 ID (UID) 通常是 16 位或 32 位无符号整数,但是我怎样才能找到任何给定系统(在 shell 中)?
小智 14
您需要查看<limits.h>(或它包含的文件之一,例如sys/syslimits.h在 OS X 上)#define的UID_MAX.
最新的操作系统(Solaris 2.x、OS X、BSD、Linux、HP-UX 11i、AIX 6)最多可以处理 20 亿 ( 2^31-2) '不。
glibc 为所有这些系统类型提供了定义。
您可以检查/usr/include/bits/typesizes.h:
% grep UID_T /usr/include/bits/typesizes.h
#define __UID_T_TYPE __U32_TYPE
Run Code Online (Sandbox Code Playgroud)
接下来你看看/usr/include/bits/types.h:
% grep '#define __U32_TYPE' /usr/include/bits/types.h
#define __U32_TYPE unsigned int
Run Code Online (Sandbox Code Playgroud)
这可以让您找出 C 类型。由于您需要以字节为单位的大小,因此您最好的选择是根据以下规范解析 typedef 名称types.h:
We define __S<SIZE>_TYPE and __U<SIZE>_TYPE for the signed and unsigned
variants of each of the following integer types on this machine.
16 -- "natural" 16-bit type (always short)
32 -- "natural" 32-bit type (always int)
64 -- "natural" 64-bit type (long or long long)
LONG32 -- 32-bit type, traditionally long
QUAD -- 64-bit type, always long long
WORD -- natural type of __WORDSIZE bits (int or long)
LONGWORD -- type of __WORDSIZE bits, traditionally long
Run Code Online (Sandbox Code Playgroud)
所以,这是一个单行:
% grep '#define __UID_T_TYPE' /usr/include/bits/typesizes.h | cut -f 3 | sed -r 's/__([US])([^_]*)_.*/\1 \2/'
U 32
Run Code Online (Sandbox Code Playgroud)
这里的U意思是unsigned(这也可以S用于signed)和32大小(在上面的列表中查找;我认为,大多数时候你可以假设它已经以字节为单位的大小,但如果你希望你的脚本完全可移植它case打开这个值可能会更好)。