当前位置: 技术问答>linux和unix
问一个很常见的问题
来源: 互联网 发布时间:2016-08-11
本文导语: 我的嵌入式系统中没有write命令也没有talk命令,我想找个源码包交叉编译了,到死活找不到。 有哪位大侠知道,write、talk等可执行文件到底怎么来的? | http://src.opensolaris.org/source/xref/onnv/onnv-...
我的嵌入式系统中没有write命令也没有talk命令,我想找个源码包交叉编译了,到死活找不到。
有哪位大侠知道,write、talk等可执行文件到底怎么来的?
有哪位大侠知道,write、talk等可执行文件到底怎么来的?
|
http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/cmd/cmd-inet/usr.bin/talk/
|
先看看该命令属于哪个包 应该能找到
#which write
/usr/bin/write
#rpm -qf /usr/bin/write
util-linux-2.13-0.52.el5_4.1
write的源码应该在util-linux源码里
#which write
/usr/bin/write
#rpm -qf /usr/bin/write
util-linux-2.13-0.52.el5_4.1
write的源码应该在util-linux源码里
|
我在下面这里下载了util-linux 找到了write的源码util-linux-2.12j/misc-utils/write.c
http://www.kernel.org/pub/linux/utils/util-linux/
http://www.kernel.org/pub/linux/utils/util-linux/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "pathnames.h"
#include "carefulputc.h"
#include "nls.h"
void search_utmp(char *, char *, char *, uid_t);
void do_write(char *, char *, uid_t);
void wr_fputs(char *);
static void done(int);
int term_chk(char *, int *, time_t *, int);
int utmp_chk(char *, char *);
int
main(int argc, char **argv) {
time_t atime;
uid_t myuid;
int msgsok, myttyfd;
char tty[MAXPATHLEN], *mytty;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
/* check that sender has write enabled */
if (isatty(fileno(stdin)))
myttyfd = fileno(stdin);
else if (isatty(fileno(stdout)))
myttyfd = fileno(stdout);
else if (isatty(fileno(stderr)))
myttyfd = fileno(stderr);
else {
myttyfd = -1;
}
if (myttyfd != -1) {
if (!(mytty = ttyname(myttyfd))) {
(void)fprintf(stderr, _("write: can't find your tty's namen"));
exit(1);
}
/* We may have /dev/ttyN but also /dev/pts/xx.
Below, term_chk() will put "/dev/" in front, so remove that part. */
if (!strncmp(mytty, "/dev/", 5))
mytty += 5;
if (term_chk(mytty, &msgsok, &atime, 1))
exit(1);
if (!msgsok) {
(void)fprintf(stderr,
_("write: you have write permission turned off.n"));
exit(1);
}
} else {
mytty = "";
}
myuid = getuid();
/* check args */
switch (argc) {
case 2:
search_utmp(argv[1], tty, mytty, myuid);
do_write(tty, mytty, myuid);
break;
case 3:
if (!strncmp(argv[2], "/dev/", 5))
argv[2] += 5;
if (utmp_chk(argv[1], argv[2])) {
(void)fprintf(stderr,
_("write: %s is not logged in on %s.n"),
argv[1], argv[2]);
exit(1);
}
if (term_chk(argv[2], &msgsok, &atime, 1))
exit(1);
if (myuid && !msgsok) {
(void)fprintf(stderr,
_("write: %s has messages disabled on %sn"),
argv[1], argv[2]);
exit(1);
}
do_write(argv[2], mytty, myuid);
break;
default:
(void)fprintf(stderr, _("usage: write user [tty]n"));
exit(1);
}
done(0);
/* NOTREACHED */
return 0;
}
/*
* utmp_chk - checks that the given user is actually logged in on
* the given tty
*/
int utmp_chk(char *user, char *tty)
{
struct utmp u;
struct utmp *uptr;
int res = 1;
utmpname(_PATH_UTMP);
setutent();
while ((uptr = getutent())) {
memcpy(&u, uptr, sizeof(u));
if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
res = 0;
break;
}
}
endutent();
return(res);
}
void search_utmp(char *user, char *tty, char *mytty, uid_t myuid)
{
struct utmp u;
struct utmp *uptr;
time_t bestatime, atime;
int nloggedttys, nttys, msgsok, user_is_me;
char atty[sizeof(u.ut_line) + 1];
utmpname(_PATH_UTMP);
setutent();
nloggedttys = nttys = 0;
bestatime = 0;
user_is_me = 0;
while ((uptr = getutent())) {
memcpy(&u, uptr, sizeof(u));
if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0) {
++nloggedttys;
(void)strncpy(atty, u.ut_line, sizeof(u.ut_line));
atty[sizeof(u.ut_line)] = '';
if (term_chk(atty, &msgsok, &atime, 0))
continue; /* bad term? skip */
if (myuid && !msgsok)
continue; /* skip ttys with msgs off */
if (strcmp(atty, mytty) == 0) {
user_is_me = 1;
continue; /* don't write to yourself */
}
if (u.ut_type != USER_PROCESS)
continue; /* it's not a valid entry */
++nttys;
if (atime > bestatime) {
bestatime = atime;
(void)strcpy(tty, atty);
}
}
}
endutent();
if (nloggedttys == 0) {
(void)fprintf(stderr, _("write: %s is not logged inn"), user);
exit(1);
}
if (nttys == 0) {
if (user_is_me) { /* ok, so write to yourself! */
(void)strcpy(tty, mytty);
return;
}
(void)fprintf(stderr,
_("write: %s has messages disabledn"), user);
exit(1);
} else if (nttys > 1) {
(void)fprintf(stderr,
_("write: %s is logged in more than once; writing to %sn"),
user, tty);
}
}
/*
* term_chk - check that a terminal exists, and get the message bit
* and the access time
*/
int term_chk(char *tty, int *msgsokP, time_t *atimeP, int showerror)
{
struct stat s;
char path[MAXPATHLEN];
if (strlen(tty) + 6 > sizeof(path))
return(1);
(void)sprintf(path, "/dev/%s", tty);
if (stat(path, &s) > 3)) != 0; /* group write bit */
*atimeP = s.st_atime;
return(0);
}
/*
* do_write - actually make the connection
*/
void do_write(char *tty, char *mytty, uid_t myuid) {
char *login, *pwuid, *nows;
struct passwd *pwd;
time_t now;
char path[MAXPATHLEN], host[MAXHOSTNAMELEN], line[512];
/* Determine our login name(s) before the we reopen() stdout */
if ((pwd = getpwuid(myuid)) != NULL)
pwuid = pwd->pw_name;
else
pwuid = "???";
if ((login = getlogin()) == NULL)
login = pwuid;
if (strlen(tty) + 6 > sizeof(path))
exit(1);
(void)sprintf(path, "/dev/%s", tty);
if ((freopen(path, "w", stdout)) == NULL) {
(void)fprintf(stderr, "write: %s: %sn",
path, strerror(errno));
exit(1);
}
(void)signal(SIGINT, done);
(void)signal(SIGHUP, done);
/* print greeting */
if (gethostname(host, sizeof(host))