mesg程序实际上就是用来查看和设置终端的组的写权限,而biff用来查看和设置终端的用户的执行权限。这两个位分别为S_IXUSR,S_IWGRP,我的实现是用stat获取mode,然后调用chmod修改mode,网站空间,我实现的这两个程序的代码基本一样,只有一处不一样,mesg中#define MC_BITS_IWGRP而biff中#defineMC_BITS_IXUSR。另外,biff和mail一组,mesg和write一组,另外本人的write并没有检验S_IWGRP位,虚拟主机,但本人也不想改写了。另外由于代码极其简单,所以注释就没写(本人一直就坚持一眼能看清的代码不写注释,可是似乎写了博客后就去写冗杂的注释了),最后吐槽一下,用代码来学习系统的办法似乎博客园没人喜欢的说,既然大家不喜欢我也不再去写了,毕竟去实现已经实现很好的程序不是什么好的做法(除非为了学习和兴趣)。
程序作者:莫尘/mc_nns; 程序开源,网站空间,可任意使用和修改
两个命令的代码是一样的,把mesg中的define MC_BITS_IWGRP替换为#defineMC_BITS_IXUSR就成了biff命令了
mesg的代码如下:
1 #include <unistd.h> 2 #include <sys/stat.h> 3 #include <stdio.h> 4 #include <errno.h> 5 #include <string.h> 6 #include <stdlib.h>MC_BIT S_IWGRP*mc_prog_name;mc_usage(void);main(int argc, char *argv[])16 {17struct stat st;18char *p;19char *tty;)) ? ++p : *argv;22tty = ttyname(STDERR_FILENO);23if(tty == NULL){,25 mc_prog_name);26exit(1);27 }28if(stat(tty, &st) == -1){,30 mc_prog_name, tty, strerror(errno));31exit(1);32 }33if(argc == 1){34if(st.st_mode & MC_BIT));printf();;39 }40switch(argv[1][0]){:42if(chmod(tty, st.st_mode | MC_BIT) == -1),44 mc_prog_name, tty, strerror(errno));45break;:47if(chmod(tty, st.st_mode & ~MC_BIT) == -1),49 mc_prog_name, tty, strerror(errno));50break;51default:52 mc_usage();53 };55 }mc_usage(void)59 {, mc_prog_name);61exit(1);62 }爱的力量大到可以使人忘记一切,