Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-01-2005, 05:56 PM
|
#1
|
|
Member
Registered: Sep 2004
Posts: 127
Rep:
|
Sending Data Bytes to a terminal
I am reading data from a socket and I want to send this to a terminal (A user Logged into the System via Xterm)
In C, how can I find a particular Terminal # (e.g. pts/1) and send a msg to this particular terminal
Help appreciated....
|
|
|
|
03-02-2005, 12:12 AM
|
#2
|
|
Member
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740
Rep:
|
Ok, here is a quick function I wrote. There is a file (/var/run/utmp on my system) that holds all the information about the users currently logged on. Each of these is stored in the file as a structure (struct utmp). So you just open it up and read each entry in. Check out /usr/include/utmp.h and /usr/include/bits/utmp.h for more information about the internals of the structure and what not (UTMP_FILE is a macro for the actual name of the file, since it changes from system to system). In this structure are 2 fields that you want. ut_user and ut_line. ut_user is the username of the user. ut_line is the device, but without the /dev part. i.e. pts/1 or tty1 or similar. so you have to append ut_line onto /dev/  Then you just open it up, and write your message to it.
Code:
#include <utmp.h>
#include <stdio.h>
/* other crud here */
void writeUserMessage(const char *user, const char *msg) {
FILE *f;
struct utmp u;
char dev[12] = "/dev/";
f = fopen(UTMP_FILE,"r");
while(fread(&u, sizeof(struct utmp), 1, f) > 0) {
if(u.ut_type == USER_PROCESS) {
if(strcmp(user, u.ut_user) == 0) {
FILE *f2;
f2 = fopen(strcat(dev,u.ut_line),"w");
fputs(msg,f2);
fclose(f2);
}
}
}
fclose(f);
}
Hope this is what you want 
Last edited by 95se; 03-02-2005 at 12:15 AM.
|
|
|
|
03-07-2005, 05:18 PM
|
#3
|
|
Member
Registered: Sep 2004
Posts: 127
Original Poster
Rep:
|
Here is my version:
I get a SIGSEGV on line 15...string compare...I am looking for a Particular Terminal say "pts/1" However, I get a Seg Fault everytime on LINE 15....
f = fopen(UTMP_FILE,"r");
11
12 while(fread(&u, sizeof(struct utmp), 1, f) > 0) {
13 if(u.ut_type == USER_PROCESS) {
14 //if((strcmp(user, u.ut_user == 0)) && (strcmp("pts/1", u.ut_line==0))) {
15 if(strcmp("pts/1", u.ut_line==0)) {
16 FILE *f2;
17 f2 = fopen(strcat(dev,u.ut_line),"w");
18 return(f2);
19 flose(f2);
|
|
|
|
03-07-2005, 05:26 PM
|
#4
|
|
Member
Registered: Sep 2004
Posts: 127
Original Poster
Rep:
|
BTW, I am returning a POINTer toa FILE * from this funtion to another Function which then uses FPUTS to write to that pts/1 that was determined in thsi function.
Guess this should work, I see that this funtion does have teh proper values in all variables till line 15, strcmp should not fail....but it does.
Help appreciated!
|
|
|
|
03-08-2005, 03:23 PM
|
#5
|
|
Member
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740
Rep:
|
Yours:
if(strcmp("pts/1", u.ut_line==0)) {
What it should be:
if(strcmp("pts/1",u.ut_line) == 0) {
I think you just misplaced the ')'. strcmp takes 2 pointers to char arrays. Unfortunately, your typo is still techincally correct in C. In C, (u.ut_line==0), a comparison, will return an integer (either 0 or not 0). And this integer can be implicitly cast into a pointer, so it sees (u.ut_line==0) as the address to the string which your comparing "pts/1" to, which is not what you want 
Last edited by 95se; 03-08-2005 at 03:25 PM.
|
|
|
|
03-16-2005, 01:34 PM
|
#6
|
|
Member
Registered: Sep 2004
Posts: 127
Original Poster
Rep:
|
pardon my ignorance ....but /var/run/utmp is a Binary file ..how can we access this file as a Structure of type utmp.
Is this an Object?
|
|
|
|
03-16-2005, 01:53 PM
|
#7
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
You could use getutent() to get the utmp structure
man 3 getutent
|
|
|
|
03-16-2005, 03:08 PM
|
#8
|
|
Member
Registered: Sep 2004
Posts: 127
Original Poster
Rep:
|
Hi ,
I am using UTMP_FILE macro to access /var/run/utmp.
I use fopen to open it..and then fread to read from the file and populate my utmp structure (u above is the code snippet).
I am sure I can do the same using getutent() func.
However, what I do not understand is that this /var/run/utmp file looks like a Binary file. It must be build using some source.
How can I open it (using UTMP_FILE macro in my case) and point to structures that it internally holds.....I am looping through the various Logins that are stored in this Binary file. I followed the directions from 95se and was able to do it. But I still have nto fully understood this.
I would guess this is a basic C Programming Question..
|
|
|
|
03-28-2005, 07:55 PM
|
#9
|
|
Member
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740
Rep:
|
Sorry for the super delayed response. fread just reads a specified number of bytes from a file and writes it at a specified address in memory. The utmp file IS a binary file. It contains a list of structures. Just one utmp structure after another, written directly, byte for byte, into the file. So I just read in a number of bytes equal to the utmp struct, and this represents some structure perviously written to the file. Think of it like this, if you have an integer, 4000000000, are you going to write it into a file as a string, "4000000000" (10 bytes), or as it's binary representation, 4 bytes. You can do this to, using fwrite.
fwrite(&some_int, sizeof(int), 1, fp);
Which would write sizeof(int) (4 bytes usually) from the address of some_int to the file fp. In this case, whatever integer is at some_int would be written as it is in memory to a file. You can then read this again using:
fread(&some_int, sizeof(int), 1, fp);
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:53 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|