Page 1 of 1

UserInformation

Posted: Sat Feb 04, 2012 4:52 pm
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

Re: UserInformation

Posted: Sun Nov 25, 2012 10:15 am
by nospam
Thank you, it's useful. One question... What library does this line load...?

Code: Select all

ImportC ""

Re: UserInformation

Posted: Sun Nov 25, 2012 11:03 am
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.

Re: UserInformation

Posted: Mon Nov 26, 2012 10:53 am
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?

Re: UserInformation

Posted: Mon Nov 26, 2012 11:37 am
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:

Re: UserInformation

Posted: Mon Nov 26, 2012 12:17 pm
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.

Re: UserInformation

Posted: Mon Nov 26, 2012 4:05 pm
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

Re: UserInformation

Posted: Tue Nov 27, 2012 12:01 pm
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!