LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 03-01-2005, 05:56 PM   #1
jonty_11
Member
 
Registered: Sep 2004
Posts: 127

Rep: Reputation: 15
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....
 
Old 03-02-2005, 12:12 AM   #2
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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.
 
Old 03-07-2005, 05:18 PM   #3
jonty_11
Member
 
Registered: Sep 2004
Posts: 127

Original Poster
Rep: Reputation: 15
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);
 
Old 03-07-2005, 05:26 PM   #4
jonty_11
Member
 
Registered: Sep 2004
Posts: 127

Original Poster
Rep: Reputation: 15
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!
 
Old 03-08-2005, 03:23 PM   #5
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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.
 
Old 03-16-2005, 01:34 PM   #6
jonty_11
Member
 
Registered: Sep 2004
Posts: 127

Original Poster
Rep: Reputation: 15
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?
 
Old 03-16-2005, 01:53 PM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,578

Rep: Reputation: 127Reputation: 127
You could use getutent() to get the utmp structure

man 3 getutent
 
Old 03-16-2005, 03:08 PM   #8
jonty_11
Member
 
Registered: Sep 2004
Posts: 127

Original Poster
Rep: Reputation: 15
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..
 
Old 03-28-2005, 07:55 PM   #9
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
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);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Privacy: sending data to Internet fiomba Linux - Software 1 09-12-2005 05:10 PM
Laptop keeps sending data every second? mikegorb Linux - Newbie 6 11-16-2004 05:54 AM
Sending data from one server to the other ganninu Linux - General 6 12-10-2003 06:06 AM
Sending data after establishing PPP connection ruchika Linux - Software 0 09-05-2003 07:59 PM
Sending data over ppp shibdas Linux - Networking 0 06-26-2003 12:40 PM


All times are GMT -5. The time now is 05:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration