[1_Dev_Note] LDD Char_Device_权限字
2016-12-21 本文已影响316人
Quasars
权限字
2个头文件:
- _UAPI_LINUX_STAT_H
#define S_IRWXU 00700//所有者权限
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100
#define S_IRWXG 00070//组权限
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010
#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001
- _LINUX_STAT_H
#define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
#define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
#define S_IRUGO (S_IRUSR|S_IRGRP|S_IROTH)
#define S_IWUGO (S_IWUSR|S_IWGRP|S_IWOTH)
#define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
写个小程序测试一下此权限字是否就是我们ls -l拿到的权限字.
work@vm1:TestKit$ ./test
0111000000,0100000000,0010000000,0001000000
0000111000,0000100000,0000010000,0000001000
0000000111,0000000100,0000000010,0000000001
work@vm1:TestKit$ ls -l test
-rwxrwxr-x 1 work work 9092 Dec 21 17:36 test
work@vm1:TestKit$ chmod g-r test
work@vm1:TestKit$ chmod g-x test
work@vm1:TestKit$ chmod g-w test
work@vm1:TestKit$ ls -l test
-rwx---r-x 1 work work 9092 Dec 21 17:36 test
附:mode_t.cc
#include <stdio.h>
#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100
#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010
#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001
#define S_IRWXUGO (S_IRWXU|S_IRWXG|S_IRWXO)
#define S_IALLUGO (S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
#define S_IRUGO (S_IRUSR|S_IRGRP|S_IROTH)
#define S_IWUGO (S_IWUSR|S_IWGRP|S_IWOTH)
#define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
#define Sym(rd, who) S_I##rd##who
#define Syms(who,f) Sym(R,WX##f),Sym(R,who),Sym(W,who),Sym(X,who)
#define PRUSR do{ printf_bin("%B,%B,%B,%B\n",Syms(USR,U)); }while(0)
#define PRGRP do{ printf_bin("%B,%B,%B,%B\n",Syms(GRP,G)); }while(0)
#define PROTH do{ printf_bin("%B,%B,%B,%B\n",Syms(OTH,O)); }while(0)
#define PRALL PRUSR;PRGRP;PROTH;
void _pb_clean_plain(char *start, char *end)
{
if(*start){
char tmp = *end;
*end = '\0';
printf("%s", start);
*end = tmp;
}
}
void _pb_pr_byte(unsigned char *p, char *str)
{
int i = 0;
while(i < 8){
str[7 - i] = '0' + ((*p) & (1U << i) ? 1 : 0);
i++;
}
}
#include <malloc.h>
void _pb_clean_num(int num)
{
// printf("%d", num);
int b = sizeof(num), i;
char *str = (char *)malloc( b * 8 * sizeof(char));
unsigned char *p = (unsigned char *)#
for(i = 0; i < b; i++){
_pb_pr_byte(p + i, str + 8 * (b - 1 - i));
//from low to high
}
// printf("\n------------\n");
// for(i = 0; i < b * 8; i++){
// printf("%d-", *(str + i));
// }
printf("%s", str + 22);
free(str);
}
#include <stdarg.h>
#include <assert.h>
#include <string.h>
#define FORMAT_MAX 4096
void printf_bin(const char *fm, ...)
{
assert(strlen(fm) < FORMAT_MAX - 1);
char fm_str[FORMAT_MAX];
strcpy(fm_str, fm);
va_list args;
va_start(args,fm);
char *i = fm_str, *before = fm_str;
int num;
while(*i){
if(*i == '%' && *(i+1) == 'B'){
_pb_clean_plain(before, i);
_pb_clean_num(va_arg(args, int));
i++;
before = i + 1;
}
i++;
}
_pb_clean_plain(before, i);
}
int main()
{
PRALL;
return 0;
}