Differenze tra le versioni di "Stat ()"

Da Gambas-it.org - Wikipedia.
Riga 37: Riga 37:
 
   __unused1 As Integer
 
   __unused1 As Integer
 
   st_mtime As Long
 
   st_mtime As Long
  __unused2 As Long
 
 
   st_mtime_nsec As Long  
 
   st_mtime_nsec As Long  
 
   st_ctime As Long
 
   st_ctime As Long
Riga 63: Riga 62:
 
   __unused1 As Integer
 
   __unused1 As Integer
 
   st_mtime As Long
 
   st_mtime As Long
  __unused2 As Long
 
 
   st_mtime_nsec As Long  
 
   st_mtime_nsec As Long  
 
   st_ctime As Long
 
   st_ctime As Long

Versione delle 04:07, 7 nov 2014

La funzione stat() della libreria di C ritorna alcune informazioni sul file specificato nei suoi parametri.


Per utilizzare la funzione stat() di C, bisognerà richiamare la funzione __xstat(). in quanto, per consentire alla Struttura "struct stat" di variare senza cambiare numero di versione della libreria condivisa .so, il gruppo di funzioni 'stat' sono traslate nelle chiamate alle funzioni 'xstat', 'fxstat', 'lxstat', alle quali è necessario passare come primo argomento il numero-versione che designa la struttura dei dati e i bit utilizzati. Dunque, in fase di linking la chiamata a stat() viene convertita in __xstat().

La definizione e dichiarazione della funzione __xstat() è presente nel file header: sys.stat.h:

int __xstat (int __ver, const char *__filename, struct stat *__stat_buf)

mentre il numero della versione che designa la struttura struct stat dei dati utilizzati è presente nel file header: bits/stat.h:

/* Versions of the `struct stat' data structure.  */
#ifndef __x86_64__
# define _STAT_VER_LINUX_OLD   1
# define _STAT_VER_KERNEL      1
# define _STAT_VER_SVR4        2
# define _STAT_VER_LINUX       3
#else
# define _STAT_VER_KERNEL      0
# define _STAT_VER_LINUX       1
#endif


Volendola utilizzare in Gambas, bisognerà dichiararla con Extern, nonché dichiarare la libreria di C: libc.so.6, nella quale la funzione __xstat() è contenuta. Da sottolineare che bisognerà definire e dichiarare anche la Struttura che si passerà al terzo paramentro della funzione __xstat().


Dunque avremo ad esempio la Struttura dichiarata:

Public Struct STATUS_File
  st_dev As Long
  st_ino As Long
  st_nlink As Long
  st_mode As Integer
  st_uid As Integer
  st_gid As Long
  st_rdev As Long
  st_size As Long
  st_blksize As Long
  st_blocks As Long
  st_atime As Long
  __unused1 As Integer
  st_mtime As Long
  st_mtime_nsec As Long 
  st_ctime As Long
  __glibc_reserved[3] As Long
End Struct

e la funzione __xstat() dichiarata:

Private Extern __xstat(ver As Integer, filename As String, stat_buf As STATUS_File) As Integer In "libc:6"


Semplice esempio di uso in Gambas:

 Public Struct STATUS_File
  st_dev As Long
  st_ino As Long
  st_nlink As Long
  st_mode As Integer
  st_uid As Integer
  st_gid As Long
  st_rdev As Long
  st_size As Long
  st_blksize As Long
  st_blocks As Long
  st_atime As Long
  __unused1 As Integer
  st_mtime As Long
  st_mtime_nsec As Long 
  st_ctime As Long
  __glibc_reserved[3] As Long
End Struct

Private Const _STAT_VER_LINUX as Integer = 1

' int __xstat (int __ver, const char *__filename, struct stat *__stat_buf)
' Get file attributes for FILE and put them in BUF.
Private Extern __xstat(ver As Integer, filename As String, stat_buf As STATUS_File) As Integer In "libc:6"


Public Sub Main()
 
 Dim i As Integer
 Dim stS As New STATUS_File
 
 i = __xstat(_STAT_VER_LINUX, "/percorso/del/file", stS)
 If i < 0 Then Error.Raise("Errore alla funzione '__xstat()' !")

 With stS
   Print .st_dev
   Print .st_ino
   Print .st_nlink
   Print .st_mode
   Print .st_uid
   Print .st_gid
   Print .st_rdev
   Print .st_size; " byte"
   Print .st_blksize
   Print .st_blocks
   Print .st_atime
   Print .st_mtime
   Print .st_ctime
 End With

End