切换其他人写入终端的权限的过程[Unix] [C]

时间:2015-03-09 21:01:16

标签: c unix terminal tty

我正在制作允许用户允许或禁止其他人写入终端的流程。以下是我所拥有但它似乎没有工作,我不知道为什么。如果有人能指出我正确的方向,我会很感激。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

main( int ac, char *av[] ){
  struct stat settings;
  int perms;

  if ( fstat(0, &settings) == -1 ){
    perror("Cannot stat stdin");
    exit(0);
  }

  perms = (settings.st_mode & 07777);

  if ( ac == 1 ){
    printf("Other's write is %s\n", (perms & S_IWOTH)?"On":"Off");
    exit(0);
  }

  if ( ac == 2 && av[1][0] == 'n' )
    perms &= ~S_IWOTH;
  else
    perms &= S_IWOTH;
  fchmod(0, perms);
  return 0;
}

1 个答案:

答案 0 :(得分:1)

你可能打算做

perms |= S_IWOTH;

而不是

perms &= S_IWOTH;
使用&的{​​p> S_IWOTH将清除所有不是S_IWOTH的位,并且如果尚未设置,则将该位保留为零。

您可以运行tty(1)获取终端的文件名并在其上运行ls -l以查看权限,以防您不知道。不要忘记组权限也可能很重要。

检查fchmod(2)的返回值也是个好主意。

相关问题