/* write used to log on to several hosts two users to communicate with each other directly mesg y/n Use to set whether to receive write messages format:write username terminal-line such as:write 123 tty1 write 123 /dev/tty1 at this point the user and the terminal to communicate write 123 At this time the program accesses the Utmp file, obtains the login user named 123 terminal, if the terminal is multiple, then to the last terminal access to send input EOF character, terminate the process */#include <stdio.h> #include <string.h> #include <utmp.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #define NUMNAME 30 // length of the terminal name #define nummes 50 //accepts the length of the message buffer Int main (int&nbsP;argc, char* argv[]) { char name[numname]= ""; //Store Username char writeto[NUMNAME] = "/dev/"; //Storage Terminal name struct utmp buf; int fd; int num, num_utmp, ch; char mes[NUMMES]; num = 0; if (argc == 1) { fprintf (stderr, "Usage: write user [tty]"); exit (1); } strcpy (name, argv[1]); if (argc == 3) { //compatible with/dev/tty1 and tty1 two parameter modes for compatible handling if (!strncmp (argv[2], writeto, 5)) strcat (writeto, &argv[2][5]); Else strcat (writeto, argv[2]); num++; } else { //traverse the utmp file to get the user and terminal of the login machine at this time if ((Fd = open ("/var/run/utmp", o_rdonly) == -1) { perror ("Open error"); exit (1); } num_utmp = sizeof (struct utmp); while (Read (fd, &buf, num_utmp) == num_utmp) { if (!strcmp (buf.ut_user, name) &&strcmp (buf.ut_line , ": 0")) { if (num > 0) writeto[5] = '; ' strcat (Writeto, buf.ut_line); num++; } } close (FD); } //the last WriteTo parameters are/dev/tty type if (num == 0) { fprintf (stderr, "no destination:%s\n", name); exit (0); } if (num >1) fprintf (stdout, "more then 1 tty, chose: %s\ n ", writeto); if ((fd = open (writeto, o_wronly)) == -1) { Perror ("Open error"); exit (1); } /* accepts the EOF character as appropriate, terminates the process, takes the GETC function, receives the character by Byte, Write once to the destination terminal receive ' \ n ' or EOF via the Write function. EOF simultaneously terminates the program &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; */ while (1) { for (num =0;num < nummes ;num++) { ch=getc (stdin); mes[num] = ch; if (ch == ' \ n ' | | ch == eof) break; } if (ch == eof) { if (num != 0) mes[num++] = ' \ n '; mes[num] = ' E '; mes[++num] = ' O '; mes[++num] = ' F '; mes[+ +num] = '; } if (Write (fd, mes, num+1) != (num+1)) { perror ("Write error"); exit (1); } if (ch == eof) break; } }
The Execute file requires a permission bit to be set, and the write command provided by the system will run exactly the same result. Otherwise, there is a bias in the question of whether there is write permission.
Execute command sequentially: Chown Root Write User ID is set to root
CHGRP TTY write group ID set to TTY
chmod g+s Write group permission settings Set-group-id bit
Summarize:
1, set the permission bit, otherwise the program runs up and the system wirte command result has the big discrepancy.
chmod g+s Write group permissions set Set-group-id bit, very important
2. If the string is out of bounds there will be a very strange error, and it is difficult to detect, so the character must reserve enough length. A lot of time is wasted here.
Easy implementation of the write command and the corresponding permission bit settings under Linux