LinuxQuestions.org
Visit Jeremy's Blog.
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 06-11-2012, 08:24 AM   #1
GNakul
Member
 
Registered: Oct 2011
Location: Bangalore,India
Posts: 59

Rep: Reputation: Disabled
username filtering


Dear all,
I m writing c code to get the username and his login time using utmp. But it prints all the users like login, reboot and all. I want only Desktop users. How can this be done?
Code:
#include <stdio.h>
#include <utmp.h>
#include <time.h>

main() {

        struct utmp *ut;
        unsigned opt;
        setutent();
        while ((ut = getutent()) != NULL) {
                if (ut->ut_user[0] && (opt || ut->ut_type == USER_PROCESS)) {
        time_t tmp;
        tmp = ut->ut_tv.tv_sec;
        printf("User %s logged in at %s\n",ut->ut_user,ctime(&tmp));
        }
        }
        endutent();
}
Output is like
Code:
User reboot logged in at Mon Jun 11 03:02:25 2012
User runlevel logged in at Mon Jun 11 03:02:25 2012
User LOGIN logged in at Mon Jun 11 03:02:25 2012
User LOGIN logged in at Mon Jun 11 03:02:25 2012
User LOGIN logged in at Mon Jun 11 03:02:25 2012
User LOGIN logged in at Mon Jun 11 03:02:25 2012
User LOGIN logged in at Mon Jun 11 03:02:25 2012
User LOGIN logged in at Mon Jun 11 03:02:27 2012
User root logged in at Mon Jun 11 03:04:37 2012
User nakul logged in at Mon Jun 11 05:22:22 2012
User root logged in at Mon Jun 11 04:15:07 2012
How can i filter the usernames only for desktop users.

Thanks in advance for your help
-Nakul Sargur
 
Old 06-11-2012, 10:01 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Quote:
Originally Posted by GNakul View Post
But it prints all the users like login, reboot and all.
It is a bug in your code: you are not limiting to USER_PROCESS entries. If you compiled it with warnings enabled (-Wall) you'd have noticed and fixed it already. Bad coder, no cookie!

Note that not all desktop environments actually maintain utmp entries. Xubuntu (XFCE4 desktop) does not, for example. On my system, the fixed code outputs nothing. To find out logged in users, I'd have to look at the process table.
 
Old 06-12-2012, 02:14 AM   #3
GNakul
Member
 
Registered: Oct 2011
Location: Bangalore,India
Posts: 59

Original Poster
Rep: Reputation: Disabled
Dear Nominal Animal,
I m a beginner in Linux System programming, i know i m a bad coder
That is the reason i m here. I want to learn from experts like you.
Any ways with only
Code:
if (ut->ut_user[0] && (ut->ut_type == USER_PROCESS))
i fixed it.
Code:
User root logged in at Mon Jun 11 21:53:56 2012

User nakul logged in at Mon Jun 11 22:51:12 2012

User root logged in at Mon Jun 11 22:00:32 2012
Regards,
Nakul

Last edited by GNakul; 06-12-2012 at 02:20 AM.
 
Old 06-12-2012, 11:04 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
Quote:
Originally Posted by GNakul View Post
Any ways with only
Code:
if (ut->ut_user[0] && (ut->ut_type == USER_PROCESS))
i fixed it.
Good! That was exactly the problem I referred to.

In your case the extra opt was uninitialized, and happened to be initialized to a nonzero value. The compiler, when using -Wall, will issue a warning about using an uninitialized variable. In your case, the nonzero value caused a shortcut, so that all ut->ut_type was never even tested -- your original if clause basically said, "if ut->user is not an empty string, and either opt is nonzero or ut->ut_type is USER_PROCESS".

The reason I wagged my finger at you was for not using the compiler to tell you about any issues it finds -- in other words, for not using the -Wall flag.

None of us write perfect code. I personally rely on the compiler to proofread for me, taking care of typos and similar details, so I can think about the overall picture instead of bogging down in the minuscule details. I always use the -Wall flag when compiling my own C code. My capacity for understanding and remembering is woefully limited, so to get the best results I can, I rely heavily on the tools I have available. For example, I don't bother trying to remember the order of parameters of even standard library functions; instead, I remember their overall functionality, weaknesses, limitations, and known issues, and use the man pages for details (using man -k term, man term, and man section term commands).
 
1 members found this post helpful.
  


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
content filtering with layer7- filtering rose1366m Linux - Networking 1 05-04-2011 12:10 PM
[SOLVED] Strip that checks if added username can be a username robertjinx Linux - Server 5 11-30-2010 02:51 AM
fedora 8 asking username but during installation i dint give any username ?? herevenku Linux - Newbie 2 08-25-2008 05:37 AM
Sendmail Spam filtering and Virus filtering MrJoshua Linux - General 2 04-03-2003 11:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration