⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p4-5.c

📁 UNIX程序设计教程
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -