Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Thu Apr 02, 2020 2:45 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Linux OS API Call for User Name
PostPosted: Thu Apr 10, 2008 9:01 am 
Offline
User avatar

Joined: Thu Aug 10, 2006 10:56 am
Posts: 152
Location: Annapolis, Maryland
Does anyone have the syntax, or sample RB code, to acquire the user name with an API call in Linux? I've found and coded the appropriate API calls for Windows and OS X, but am at a loss to find what I need to do the same in Linux. To be very clear, I am not looking for an environment variable with the user name. I want to get it via an OS API call. Thanks.


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux OS API Call for User Name
PostPosted: Thu Apr 10, 2008 10:26 am 
Offline

Joined: Thu Jan 26, 2006 12:55 am
Posts: 31
This is not strictly an OS API call, but you could just use the whoami command to determine the username of the owner of the running process.


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux OS API Call for User Name
PostPosted: Thu Apr 10, 2008 4:56 pm 
Offline
User avatar

Joined: Thu Aug 10, 2006 10:56 am
Posts: 152
Location: Annapolis, Maryland
quangdog wrote:
This is not strictly an OS API call, but you could just use the whoami command to determine the username of the owner of the running process.

Thanks, but as I said, I would prefer to determine the user name with a Declare/API call. This seems to be difficult to find. Hopefully someone has the answer and comes across this post.


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux OS API Call for User Name
PostPosted: Thu Apr 10, 2008 7:38 pm 
Offline
User avatar

Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN
I think you can use the getlogin API, or getlogin_r. At the very least, you can use getuid to get the user's id, and then convert that into a user name via getpwuid.

http://opengroup.org/onlinepubs/0079087 ... login.html
http://www.opengroup.org/onlinepubs/009 ... etuid.html
http://opengroup.org/onlinepubs/0079087 ... pwuid.html


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux OS API Call for User Name
PostPosted: Fri Apr 11, 2008 9:42 am 
Offline
User avatar

Joined: Thu Aug 10, 2006 10:56 am
Posts: 152
Location: Annapolis, Maryland
Aaron Ballman wrote:
I think you can use the getlogin API, or getlogin_r. At the very least, you can use getuid to get the user's id, and then convert that into a user name via getpwuid.

http://opengroup.org/onlinepubs/0079087 ... login.html
http://www.opengroup.org/onlinepubs/009 ... etuid.html
http://opengroup.org/onlinepubs/0079087 ... pwuid.html

Since in your suggestion there are two ways to acquire the user name, is one method preferred over the other? It seems that the first is more direct but there may be some benefit to the second that I've overlooked.

I am unfortunately not an expert at converting what's documented at each of the links to REALbasic code. Can you point me in the right direction in terms of a tutorial that would get me through this and any future requirements to write Declares and do the necessary conversions on the retrieved data? As you know, my background is in Visual Basic, not C, so some education is in order.

Thanks!


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux OS API Call for User Name
PostPosted: Fri Apr 11, 2008 10:22 am 
Offline
User avatar

Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN
Personally -- I'd use getlogin. The reason is because (1) it's direct, and (2) it is easier to clean up than getlogin_r, because of this comment:
Quote:
Upon successful completion, getlogin() returns a pointer to the login name or a null pointer if the user's login name cannot be found. Otherwise it returns a null pointer and sets errno to indicate the error.

The return value may point to static data whose content is overwritten by each call.

So basically, we can call it and just work with the results.

The call is very simple -- it takes nothing, and returns a pointer to an ANSI string, which is a CString in REALbasic. So the converted call would look like this:

Soft Declare Function getlogin Lib "LibC" () as CString

dim username as String = getlogin


That's all there is to it! I don't have a good example of converting Linux declares, but here's a bit of an (old) primer on Windows declares that may help out.

http://aaronballman.com/programming/REA ... RbDecl.php


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux OS API Call for User Name
PostPosted: Fri Apr 11, 2008 2:24 pm 
Offline
User avatar

Joined: Thu Aug 10, 2006 10:56 am
Posts: 152
Location: Annapolis, Maryland
Aaron Ballman wrote:
Personally -- I'd use getlogin. The reason is because (1) it's direct, and (2) it is easier to clean up than getlogin_r, because of this comment:
Quote:
Upon successful completion, getlogin() returns a pointer to the login name or a null pointer if the user's login name cannot be found. Otherwise it returns a null pointer and sets errno to indicate the error.

The return value may point to static data whose content is overwritten by each call.

So basically, we can call it and just work with the results.

The call is very simple -- it takes nothing, and returns a pointer to an ANSI string, which is a CString in REALbasic. So the converted call would look like this:

Soft Declare Function getlogin Lib "LibC" () as CString

dim username as String = getlogin


That's all there is to it! I don't have a good example of converting Linux declares, but here's a bit of an (old) primer on Windows declares that may help out.

http://aaronballman.com/programming/REA ... RbDecl.php

Thanks. So how did you determine which type of string to use, or know that it is an ANSI string, and how did you know to use the LibC library versus another library?

I just tried it and username turns out to be empty. I also had to change LibC to libc in Ubuntu 7.10. When I execute WhoAmI from within a terminal session I get my user name. Any ideas why the API call returns nothing?


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux OS API Call for User Name
PostPosted: Fri Apr 11, 2008 2:58 pm 
Offline
User avatar

Joined: Wed Sep 28, 2005 8:39 am
Posts: 9341
Location: St Augusta, MN
fwRoller wrote:
Thanks. So how did you determine which type of string to use, or know that it is an ANSI string, and how did you know to use the LibC library versus another library?


If it's a char *, then it's a CString. If it's a wchar_t *, then it's a WString. If it's an unsigned char *, then generally it's a PString (but you won't see this one often). As for LibC versus something else -- that was a guess, truth be told. ;-) There are some tricks for determining the library, mostly involving man pages and the likes. But unfortunately, I don't have much better advice than that.

Quote:
I just tried it and username turns out to be empty. I also had to change LibC to libc in Ubuntu 7.10. When I execute WhoAmI from within a terminal session I get my user name. Any ideas why the API call returns nothing?


Hmm... well, the trouble is, you cannot check errno from REALbasic -- so you have no way to find out what could be going wrong. You could try the other approaches with getlogin_r, or getuid to see if you can get different results. Or perhaps someone more knowledgeable can step in with some advice...


Top
 Profile  
Reply with quote  
 Post subject: Re: Linux OS API Call for User Name
PostPosted: Mon Apr 21, 2008 9:31 pm 
Offline
User avatar

Joined: Thu Aug 10, 2006 10:56 am
Posts: 152
Location: Annapolis, Maryland
Thanks Aaron!
Soft Declare Function getlogin Lib "libc" () As CString
App.UserName_String = getlogin

Any Linux gurus out there that might have an idea why the 'getlogin' API call is returning an empty string? I'm running Ubuntu 7.10 and REALbasic 2008r1.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group