p4-5.c

来自「SUN Solaris8平台下进程间通信」· C语言 代码 · 共 46 行

C
46
字号
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include "err_exit.h"int main(void){    const char  *path = "testfile";    struct stat orig_buf, new_buf;    int ngroups = getgroups(0, NULL);       /* 获取附加组个数(§5.8.4)*/    /* 分配存放附加组ID的数组空间 */    gid_t *groups = (gid_t *) malloc(ngroups * sizeof(gid_t));    int val = getgroups(ngroups, groups);    /* 获取附加组ID于数组groups */    printf("create testfile\n");    creat(path, S_IRWXU | S_IRWXG | S_IRWXO);    /* 查看这个新创建文件的用户ID、组ID和进程的附加组ID */    stat(path, &orig_buf);    printf("original owner-id=%d, group-id=%d,",            orig_buf.st_uid, orig_buf.st_gid);    if (val>1)       printf(" supplementary group = %d\n",groups[1]);    /* 改变文件的用户ID和组ID 至其它用户 */    printf("set owner-id and group-id to 2 which is bin\n");    if (chown(path, 2, 2) == 0) {       printf("chown() call successful\n");       stat(path, &new_buf);       printf("new owner-id=%d, group-id=%d\n",                  new_buf.st_uid, new_buf.st_gid );    }    else        perror("chown() call failed");    /* 改变文件的用户ID和组ID 至附加组 */    printf("set group-id to supplementary group\n");     if (chown(path, orig_buf.st_uid, groups[1]) == 0 ) {       printf("chown() call successful\n");       stat (path, &new_buf);       printf("new owner-id=%d, group-id=%d\n",                  new_buf.st_uid, new_buf.st_gid );    }    else        perror("chown() call failed");    fprintf(stdout,"remove testfile\n");    remove(path);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?