UserInformation

Linux specific forum
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

UserInformation

Post by Guimauve »

Hello everyone,

A little code snippet to get a Linux User Informations.

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : User Informations
; File Name : UserInformations.pb
; File version: 1.0.0
; Programmation : OK
; Programmed by : remi_meier
; Additional programming by : Guimauve
; Date : 04-02-2012
; Last Update : 04-02-2012
; PureBasic code : 4.61
; Plateform : Linux
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Structure passwd 
  *pw_name.ascii;       /* username */
  *pw_passwd.ascii;     /* user password */
  pw_uid.l;        /* user ID */
  pw_gid.l;        /* group ID */
  *pw_gecos.ascii;      /* real name */
  *pw_dir.ascii;        /* home directory */
  *pw_shell.ascii;      /* shell program */
EndStructure

ImportC ""
  getuid.l()
  getpwuid.i(uid.l)
EndImport


Procedure.s UserName()
  
  Protected *login.passwd
  
  *login = getpwuid(getuid())
  
  If *login
    ProcedureReturn PeekS(*login\pw_name, -1, #PB_Ascii)
  EndIf
  
EndProcedure

Procedure.s RealUserName()
  
  Protected *login.passwd
  
  *login = getpwuid(getuid())
  
  If *login
    ProcedureReturn PeekS(*login\pw_gecos, -1, #PB_Ascii)
  EndIf
  
EndProcedure

Procedure UserID()
  
  Protected *login.passwd
  
  *login = getpwuid(getuid())
  
  If *login
    ProcedureReturn *login\pw_uid
  EndIf
  
EndProcedure

Procedure GroupID()
  
  Protected *login.passwd
  
  *login = getpwuid(getuid())
  
  If *login
    ProcedureReturn *login\pw_gid
  EndIf
  
EndProcedure

Procedure.s ShellProgram()
  
  Protected *login.passwd
  
  *login = getpwuid(getuid())
  
  If *login
    ProcedureReturn PeekS(*login\pw_shell, -1, #PB_Ascii)
  EndIf
  
EndProcedure

Procedure.s HomeDirectory() ; Useless, since we have GetHomeDirectory()
  
  Protected *login.passwd, HomeDirectory.s
  
  *login = getpwuid(getuid())
  
  If *login
    
    HomeDirectory = PeekS(*login\pw_dir, -1, #PB_Ascii)
    
    If Right(HomeDirectory, 1) <> "/"
      HomeDirectory + "/"
    EndIf
    
    ProcedureReturn HomeDirectory
    
  EndIf
  
EndProcedure

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
; <<<<< !!! WARNING - YOU ARE NOW IN A TESTING ZONE - WARNING !!! <<<<< 
; <<<<< !!! WARNING - THIS CODE SHOULD BE COMMENTED - WARNING !!! <<<<< 
; <<<<< !!! WARNING - BEFORE THE FINAL COMPILATION. - WARNING !!! <<<<< 
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 

Debug UserName()
Debug RealUserName()
Debug UserID()
Debug GroupID()
Debug ShellProgram()
Debug HomeDirectory()

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards.
Guimauve
nospam
Enthusiast
Enthusiast
Posts: 130
Joined: Mon Nov 12, 2012 9:15 am

Re: UserInformation

Post by nospam »

Thank you, it's useful. One question... What library does this line load...?

Code: Select all

ImportC ""
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: UserInformation

Post by Shardik »

nospam wrote:What library does this line load...?

Code: Select all

ImportC ""
http://www.purebasic.fr/english/viewtop ... 83&start=1
freak wrote:The required libraries for these commands are already linked to the executable because the Gadget commands themselves use them too, so you can just leave that part blank.
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: UserInformation

Post by Poshu »

Still quite new to linux, so please pardon any stupid question.

To get some of those informations, I'm using environment variables (this code work on ubuntu 12.10 x64) :

Code: Select all

*memory = AllocateMemory(5)
PokeS(*memory,"LANG",-1,#PB_Ascii)
Debug  PeekS(getenv_(*memory),-1,#PB_Ascii)
PokeS(*memory,"HOME",-1,#PB_Ascii)
Debug  PeekS(getenv_(*memory),-1,#PB_Ascii)
PokeS(*memory,"USER",-1,#PB_Ascii)
Debug PeekS(getenv_(*memory),-1,#PB_Ascii)
FreeMemory(*memory)
Is it wrong to do so?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: UserInformation

Post by ts-soft »

Poshu wrote:Is it wrong to do so?
It is easier this way:

Code: Select all

Debug GetEnvironmentVariable("LANG")
Debug GetEnvironmentVariable("HOME")
Debug GetEnvironmentVariable("USER")
:wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
nospam
Enthusiast
Enthusiast
Posts: 130
Joined: Mon Nov 12, 2012 9:15 am

Re: UserInformation

Post by nospam »

Shardik wrote:http://www.purebasic.fr/english/viewtop ... 83&start=1
freak wrote:The required libraries for these commands are already linked to the executable because the Gadget commands themselves use them too, so you can just leave that part blank.
Thanks for that. I'd seen it elsewhere and had wondered about it.
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Re: UserInformation

Post by Poshu »

ts-soft wrote:
Poshu wrote:Is it wrong to do so?
It is easier this way:

Code: Select all

Debug GetEnvironmentVariable("LANG")
Debug GetEnvironmentVariable("HOME")
Debug GetEnvironmentVariable("USER")
:wink:
I tend to forget that purebasic comes with a set of built-in functions, I'm only using it for its ability to compile really fast and its sweet syntax :p
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Re: UserInformation

Post by remi_meier »

Poshu wrote:Is it wrong to do so?
It's fine as long as those env variables are set. It's even
better if you want to allow a program's HOME, USER, etc.
to be overwritten when starting a program. Like:

Code: Select all

HOME=/tmp/tmp_home ./myprogram
The best way would be to first check the env var and,
if it is empty, use the procedures based on getuid().

cheers!
Athlon64 3700+, 1024MB Ram, Radeon X1600
Post Reply