自己动手写shell命令之write

Linux下write命令允许用户跟其他终端上的用户对话。用c语言实现shell命令write,代码如下:

#include	<stdio.h>
#include	<fcntl.h>
#include	<unistd.h>
#include	<utmp.h>
#include	<pwd.h>
#include	<sys/types.h>
#include	<stdlib.h>
#include	<sys/stat.h>
#include	<time.h>

char * get_terminal_name(char *);
char * get_terminal_name_by_user_name(char *);
void hello();

int main(int argc, char * argv[]) {
	int fd;
	char buffer[1024];
	char * terminal_name;
	if (argc != 2) {
		printf("write usage: write [ttyname|username]");
		return 1;
	}
	terminal_name = get_terminal_name(argv[1]);
	if(terminal_name == NULL)
	{
		printf("get terminal name error\n");
		return 1;
	}
	//printf("terminate_name:%s", terminal_name);

	fd = open(terminal_name,O_WRONLY);
	hello(fd);

	while(fgets(buffer,1024,stdin) != EOF)
	{
		write(fd,buffer,strlen(buffer));
	}
	close(fd);
	return 0;
}

void hello(int fd) {
	char greeting[1024];
	struct passwd * passwd_pointer;
	time_t now;
	char host[255];
	gethostname(host, 255);
	time(&now);
	passwd_pointer = getpwuid(getuid());
	sprintf(greeting, "Message from %[email protected]%s on %s at %5.5s ...\n",
			passwd_pointer->pw_name, host, ttyname(0), ctime(&now) + 11);
	write(fd,greeting,strlen(greeting));
}

char * get_terminal_name(char * user_input) {
	char terminal_name[255];
	struct stat stat_buffer;
	if (lstat(user_input, &stat_buffer) != -1) {
		if (S_ISCHR(stat_buffer.st_mode)) {
			return user_input;
		}
	}
	return get_terminal_name_by_user_name(user_input);
}

char * get_terminal_name_by_user_name(char * user_name) {
	struct utmp * utmp_pointer;
	setutent();
	int number = 0;
	char * result = (char *) malloc(sizeof(char) * 255);
	char *mytty = ttyname(0); /* begins "/dev/" */
	char *ttydev = mytty + strlen("/dev/");
	printf("%s\n", mytty);

	while ((utmp_pointer = getutent()) != NULL) {
		if (strcmp(user_name, utmp_pointer->ut_user) == 0
				&& utmp_pointer->ut_type == USER_PROCESS
				&& strcmp(utmp_pointer->ut_line, ttydev) != 0
				&& utmp_pointer->ut_line[0] != ':') {
			number++;
			//printf("%s\n", utmp_pointer->ut_line);
			if (number == 1) {
				strcpy(result, "/dev/");
				strcat(result, utmp_pointer->ut_line);
			}
		}
	}
	endutent();
	if (number > 1)
		printf("warning:%s is logged in %d different terminals,using %s\n",
				user_name, number, result);
	if(number == 0)
		return NULL;
	return result;
}
时间: 2024-08-11 21:20:41

自己动手写shell命令之write的相关文章

自己动手写shell命令之du

du命令可以查看指定文件夹占用的磁块数,以下为linux下c语言实现shell du指令的代码(支持-k选项): #include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <string.h> int disk_usage(char *); int k = 0; int main(int argc,char * argv

自己动手写shell命令之ls -R1fF

ls命令的R参数代表递归的列出所有子文件夹中的所有文件,1表示每一行只显示一个文件或文件夹,f表示不排序即输出,F表示在每项的输出的最后根据其文件类型相应的加上*/=>@|字符.通过c语言实现ls -R1fF命令的效果,其源代码如下: #include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <pwd.h> #in

自己动手写shell命令之who

思路:在unix系统中有一个名为utmp的文件,里面存着当前登录到系统中用户的信息.utmp这个文件里面保存的是结构数组,数组元素是utmp类型的结构.实现who命令,只要依次从utmp文件中读出utmp类型的结构体,然后通过合适的方式将它显示出来即可.如果每一次只从文件里面读一个结构体数据,那么每一个结构体都要进行依次系统调用.而又因为系统调用比较耗时(涉及到用户态到核心态的切换),所以这样会造成程序低效.我们使用缓冲技术,每次从文件中读若干结构体数据到内存中,当这些数据都已经显示在终端后,我

自己动手写shell命令之ls

linux下ls命令(支持-R參数)的c语言实现: #include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> #include <pwd.h> #include <grp.h> #include <string.h> void do_ls(char *); void do_stat(char *,char *);

自己动手写shell命令之more

unix下more命令的简单实现: #include <stdio.h> #define PAGELEN 24 #define LINELEN 512 int do_more(FILE * file); int see_more(FILE * file); int main(int argc,char * argv[]) { FILE * fp; if(argc == 1) do_more(stdin); else { int argv_index = 1; while(argc > 1

自己动手写shell命令之pwd

思路:(1)得到"."的i节点号,称其为n(使用stat) (2)chdir ..(使用chdir) (3)找到inode号为n的节点,得到其文件名. 重复上述操作直到当前目录"."的inode值等于".."的inode值 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> ino_t

自己动手写shell之chgrp,chown,chmod

1.chgrp实现 #include <grp.h> #include <unistd.h> void chgrp(char * groupname,char * filename) { struct group * groupinfo = NULL; if((groupinfo = getgrnam(groupname)) == NULL) { printf("groupname does not exist\n"); return; } if(access(

自己写shell命令pwd

思维:(1)得到"."的i节点号,叫n(使用stat) (2)chdir ..(使用chdir) (3)找到inode号为n的节点,得到其文件名称. 反复上述操作直到当前文件夹"."的inode值等于".."的inode值 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> ino_t

写了一个Mac快速设置、打开和关闭Web代理的Shell命令

缘由(痛点) 每次在Mac上设置Web代理,都需要点开"系统偏好设置 -- 网络 -- 高级 -- 代理",然后分别设置Web代理(HTTP)和安全Web代理(HTTPS),设置完后,还要点"好 -- 应用".如果要关闭Web代理也是如此,只是点到代理设置面板后,只需要取消勾选Web代理和安全Web代理. 个人觉得这种操作很繁琐,对于一个经常需要改Web代理的人来说,这简直是灾难性的重复操作.所以这段时间学了些Linux Shell脚本编程的知识,写了两个Shell