我知道,UNIX用户ID(UID)通常是16或32位无符号整数,但我怎么能找出任何给定的系统(在一个shell)?
您需要查看<limits.h> (或其中包含的一个文件,例如OS X上的sys/syslimits.h ),以查找UID_MAX的#define 。
最新的操作系统(Solaris 2.x,OS X,BSD,Linux,HP-UX 11i,AIX 6)可以处理多达20亿( 2^31-2 ),所以我会假设并为更不明显的系统。
glibc提供了所有这些系统types的定义。
你可以检查/usr/include/bits/typesizes.h :
% grep UID_T /usr/include/bits/typesizes.h #define __UID_T_TYPE __U32_TYPE
接下来你看看/usr/include/bits/types.h :
% grep '#define __U32_TYPE' /usr/include/bits/types.h #define __U32_TYPE unsigned int
这可以让你找出C型。 由于您需要以字节为单位的大小,所以最好的select是根据types.h的规范parsingtypedef名称:
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
所以,这里是一个单行的:
% grep '#define __UID_T_TYPE' /usr/include/bits/typesizes.h | cut -f 3 | sed -r 's/__([US])([^_]*)_.*/\1 \2/' U 32
这里的U表示unsigned (这也可以是S表示signed ), 32是大小(在上面的列表中查看;我想大多数情况下,你可以假设这已经是以字节为单位的大小了,但是如果你想要脚本要完全移植,这个值可能会更好)。
这是一个有趣的问题。 如果有一个标准的,便携式的方法来确定这一点,我会感到惊讶。
我没有Linux的方便,但在FreeBSD 8.0上的id命令回到零:
# id 4294967296 uid=0(root) gid=0(wheel) groups=0(wheel),5(operator)
我相信这是未定义的行为,但我敢打赌,大多数版本的id要么包装为零, 65'536 (如果16位的UID)和4'294'967'296或错误,如果你超越系统限制。
在这个链接中 ,问题被询问,响应者使用一个试验和错误的方法来确定有问题的系统使用一个带符号的long int,剩下31位来存储值,最大值为2,147,483,647。
# groupadd -g 42949672950 testgrp # more /etc/group testgrp:*:2147483647: