File Owner and Group

Mac OSX specific forum
User avatar
Airr
User
User
Posts: 49
Joined: Tue Oct 04, 2005 4:29 am
Contact:

Post by Airr »

I know this is an old post, but I thought this might be useful info anyway:

Code: Select all

Structure stat64
  st_dev.w            ; /* ID of device containing file */
  st_mode.i           ; /* Mode of file  */
  st_nlink.i          ; /* Number of hard links */
  st_ino.w            ; /* File serial number */
  st_uid.i            ; /* User ID of the file */
  st_gid.i            ; /* Group ID of the file */
  st_rdev.i           ; /* Device ID */
  st_atimespec.q      ; /* time of last access */
  st_mtimespec.q      ; /* time of last data modification */
  st_ctimespec.q      ; /* time of last status change */
  st_birthtimespec.q  ; /* time of file creation(birth) */
  st_size.i           ; /* file size, in bytes */
  st_blocks.i         ; /* blocks allocated for file */
  st_blksize.i        ; /* optimal blocksize for I/O */
  st_flags.i          ; /* user defined flags for file */
  st_gen.i            ; /* file generation number */
  st_lspare.i         ; /* RESERVED: DO NOT USE! */
  st_qspare.q[2]      ; /* RESERVED: DO NOT USE! */
EndStructure

Structure passwd
   pw_name.s          ;       /* user name */
   pw_passwd.s        ;       /* encrypted password */
   pw_uid.i           ;       /* user uid */
   pw_gid.i           ;       /* user gid */
   pw_change.i        ;       /* password change time */
   pw_class.s         ;       /* user access class */
   pw_gecos.s         ;       /* Honeywell login info */
   pw_dir.s           ;       /* home directory */
   pw_shell.s         ;       /* default shell */
   pw_expire.i        ;       /* account expiration */
   pw_fields.i        ;       /* internal: fields filled in */
EndStructure

;ImportC "/usr/lib/libc.dylib"
;  stat.l(s.s, *buf)
;  getpwuid(*uid)
;EndImport

buf.stat64
*pwd.passwd

; place your own path to file/folder here
err.l=stat_("/Users/riveraa/Library",@buf)

Debug buf\st_uid    ; USER ID of file's owner
Debug buf\st_gid    ; GROUP ID with rights to file

*pwd=getpwuid_(buf\st_uid) ; pass uid to function

Debug *pwd\pw_name  ; user's shortname
Debug *pwd\pw_uid   ; confirms correct uid
I think the struct conversions are right, but could use another set of eyes...

AIR.

Edit: Realized that the ImportC block wasn't needed because the api functions are accessible without them. Calls have been modified accordingly...
"Programming is like Poetry. Sometimes the words just escape you..." -me, to my manager.
jamirokwai
Enthusiast
Enthusiast
Posts: 796
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Post by jamirokwai »

Airr wrote:I know this is an old post, but I thought this might be useful info anyway:
Sorry for the late response... I was busy and forgot about my question.
I will have a look. Thank you very much!
Regards,
JamiroKwai
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: File Owner and Group

Post by wilbert »

I know this is a very old thread but I verified the structure for both 32 and 64 bits.

Code: Select all

ImportC ""
  stat64(path.p-ascii, *buf)
  getpwuid(uid)
  getgrgid(gid)
EndImport

Structure timespec
  tv_sec.i
  tv_nsec.i
EndStructure

Structure stat64
  st_dev.l;                   /* ID of device containing file */
  st_mode.w;                  /* Mode of file */
  st_nlink.w;                 /* Number of hard links */
  st_ino.q;                   /* File serial number */
  st_uid.l;                   /* User ID of the file */
  st_gid.l;                   /* Group ID of the file */
  st_rdev.i;                  /* Device ID */
  st_atimespec.timespec;      /* time of last access */
  st_mtimespec.timespec;      /* time of last data modification */
  st_ctimespec.timespec;      /* time of last status change */
  st_birthtimespec.timespec;  /* time of file creation(birth) */
  st_size.q;                  /* file size, in bytes */
  st_blocks.q;                /* blocks allocated for file */
  st_blksize.l;               /* optimal blocksize for I/O */
  st_flags.l;                 /* user defined flags for file */
  st_gen.l;                   /* file generation number */
  st_lspare.l;                /* RESERVED: DO NOT USE! */
  st_qspare.q[2];             /* RESERVED: DO NOT USE! */
EndStructure

stat64("MyFile", @stats.stat64)

Debug "File size : " + Str(stats\st_size)
Debug "User name : " + PeekS(PeekI(getpwuid(stats\st_uid)), -1, #PB_Ascii)
Debug "Group name : " + PeekS(PeekI(getgrgid(stats\st_gid)), -1, #PB_Ascii)
Debug "Last access : " + FormatDate("%yyyy/%mm/%dd", stats\st_atimespec\tv_sec)
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply