Differenze tra le versioni di "Stat ()"

Da Gambas-it.org - Wikipedia.
 
(14 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
La funzione '''stat()''' della libreria di C ritorna alcune informazioni sul file specificato nei suoi parametri.
 
La funzione '''stat()''' della libreria di C ritorna alcune informazioni sul file specificato nei suoi parametri.
  
 +
Per utilizzare la funzione esterna "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()'''.
  
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: ''/usr/include/x86_64-linux-gnu/sys/stat.h'':
 
 
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)
 
  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'':
+
mentre il numero della versione che designa la struttura ''struct stat'' dei dati utilizzati è presente nel file header: ''/usr/include/x86_64-linux-gnu/bits/stat.h'':
 
  <FONT Color=gray>/* Versions of the `struct stat' data structure.  */</font>
 
  <FONT Color=gray>/* Versions of the `struct stat' data structure.  */</font>
 
  #ifndef __x86_64__
 
  #ifndef __x86_64__
Riga 17: Riga 16:
 
  # define _STAT_VER_LINUX      1
 
  # define _STAT_VER_LINUX      1
 
  #endif
 
  #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'', avente una dimensione di 144 byte di memoria, che si passerà al terzo paramentro della funzione "__xstat()".
  
 
+
Dunque avremo la ''Struttura'' dichiarata:
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()''.
+
  Public Struct stat_
 
 
 
 
Dunque avremo ad esempio la ''Struttura'' dichiarata:
 
  Public Struct STATUS_File
 
 
   st_dev As Long
 
   st_dev As Long
 
   st_ino As Long
 
   st_ino As Long
Riga 29: Riga 25:
 
   st_mode As Integer
 
   st_mode As Integer
 
   st_uid As Integer
 
   st_uid As Integer
   st_gid As Long
+
   st_gid As Integer
 +
  __pad0 As Integer
 
   st_rdev As Long
 
   st_rdev As Long
 
   st_size As Long
 
   st_size As Long
Riga 35: Riga 32:
 
   st_blocks As Long
 
   st_blocks As Long
 
   st_atime As Long
 
   st_atime As Long
   __unused1 As Integer
+
   st_atimensec As Long
 
   st_mtime As Long
 
   st_mtime As Long
   __unused2 As Long
+
   st_mtimensec As Long  
  st_mtime_nsec As Long  
 
 
   st_ctime As Long
 
   st_ctime As Long
 +
  st_ctimensec As Long
 
   __glibc_reserved[3] As Long
 
   __glibc_reserved[3] As Long
 
  End Struct
 
  End Struct
 +
il cui identificatore, affinché non si verifichi un grave errore all'avvio del programma, dovrà necessariamente essere <SPAN Style="text-decoration:underline">diverso</span> dalla parola ''stat'', poiché coincidente con la Classe ''Stat'' nativa di Gambas.
 +
<BR>Avremo, inoltre, la funzione "__xstat()" dichiarata:
 +
Private <FONT color=#B22222>Extern __xstat</font>(__ver As Integer, __filename As String, __stat_buf As stat_) As Integer In "<FONT color=#B22222>libc:6</font>"
  
e la funzione ''__xstat()'' dichiarata:
+
I membri ''st_mtime'', ''st_mtime'', ''st_ctime'' della precedente ''Struttura'' restituiscono dei valori numerici che rappresentano ciascuno una data ed un orario. Per convertire tali valori numerici in data e orario, dovremo fare uso delle funzioni di C: "localtime()" e "asctime()".
Private <FONT color=#B22222>Extern __xstat</font>(ver As Integer, filename As Integer, stat_buf As STATUS_File) As Integer In "<FONT color=#B22222>libc:6</font>"
 
 
 
  
  
 
Semplice esempio di uso in Gambas:
 
Semplice esempio di uso in Gambas:
  Public Struct STATUS_File
+
Library "libc:6"
 +
 +
Public Struct stat_
 
   st_dev As Long
 
   st_dev As Long
 
   st_ino As Long
 
   st_ino As Long
Riga 55: Riga 55:
 
   st_mode As Integer
 
   st_mode As Integer
 
   st_uid As Integer
 
   st_uid As Integer
   st_gid As Long
+
   st_gid As Integer
 +
  __pad0 As Integer
 
   st_rdev As Long
 
   st_rdev As Long
 
   st_size As Long
 
   st_size As Long
Riga 61: Riga 62:
 
   st_blocks As Long
 
   st_blocks As Long
 
   st_atime As Long
 
   st_atime As Long
   __unused1 As Integer
+
   st_atimensec As Long
 
   st_mtime As Long
 
   st_mtime As Long
   __unused2 As Long
+
   st_mtimensec As Long  
  st_mtime_nsec As Long  
 
 
   st_ctime As Long
 
   st_ctime As Long
 +
  st_ctimensec As Long
 
   __glibc_reserved[3] As Long
 
   __glibc_reserved[3] As Long
 
  End Struct
 
  End Struct
Riga 73: Riga 74:
 
  <FONT Color=gray>' ''int __xstat (int __ver, const char *__filename, struct stat *__stat_buf)''
 
  <FONT Color=gray>' ''int __xstat (int __ver, const char *__filename, struct stat *__stat_buf)''
 
  ' ''Get file attributes for FILE and put them in BUF.''</font>
 
  ' ''Get file attributes for FILE and put them in BUF.''</font>
  Private <FONT color=#B22222>Extern __xstat</font>(ver As Integer, filename As Integer, stat_buf As STATUS_File) As Integer In "<FONT color=#B22222>libc:6</font>"
+
  Private Extern <FONT color=#B22222>__xstat</font>(int __ver, const char *__filename, struct stat *__stat_buf) As Integer
 +
 
 +
<FONT Color=gray>' ''tm* localtime( const time_t *time )''
 +
' ''Converts given time since epoch as time_t value into calendar time, expressed in local time.''</font>
 +
Private Extern localtime(tm As Pointer) As Pointer
 +
 +
<FONT Color=gray>' ''char* asctime( const tm* time_ptr )''
 +
' ''Converts given calendar time tm to a textual representation.''</font>
 +
Private Extern asctime(time_ptr As Pointer) As String
 
   
 
   
 
   
 
   
Riga 79: Riga 88:
 
    
 
    
 
   Dim i As Integer
 
   Dim i As Integer
   Dim stS As New STATUS_File
+
   Dim st As New stat_
 +
  Dim at, mt, ct As Long
 +
 +
  i = <FONT color=#B22222>__xstat</font>(_STAT_VER_LINUX, "<FONT Color=gray>''/percorso/del/file''</font>", st)
 +
  If i < 0 Then Error.Raise("Errore alla funzione '__xstat()' !")
 
    
 
    
  i = <FONT color=#B22222>__xstat</font>(_STAT_VER_LINUX, "''/percorso/del/file''", stS)
+
   With st
  If i < 0 Then Error.Raise("Errore alla funzione '__xstat()' !")
 
 
   With stS
 
 
     Print .st_dev
 
     Print .st_dev
 
     Print .st_ino
 
     Print .st_ino
Riga 95: Riga 105:
 
     Print .st_blksize
 
     Print .st_blksize
 
     Print .st_blocks
 
     Print .st_blocks
     Print .st_atime
+
     at = .st_atime
     Print .st_mtime
+
     mt = .st_mtime
     Print .st_ctime
+
     ct = .st_ctime
 
   End With
 
   End With
 +
 
 +
  Print asctime(localtime(VarPtr(at)))
 +
  Print asctime(localtime(VarPtr(mt)))
 +
  Print asctime(localtime(VarPtr(ct)))
 +
 
 +
'''End'''
 +
 +
 +
In quest'altro esempio si verificherà se un determinato percorso è un file regolare o una Directory oppure un file-device:
 +
Library "libc:6"
 +
 +
Public Struct stat_
 +
  st_dev As Long
 +
  st_ino As Long
 +
  st_nlink As Long
 +
  st_mode As Integer
 +
  st_uid As Integer
 +
  st_gid As Integer
 +
  __pad0 As Integer
 +
  st_rdev As Long
 +
  st_size As Long
 +
  st_blksize As Long
 +
  st_blocks As Long
 +
  st_atime As Long
 +
  st_atimensec As Long
 +
  st_mtime As Long
 +
  st_mtimensec As Long
 +
  st_ctime As Long
 +
  st_ctimensec As Long
 +
  __glibc_reserved[3] As Long
 +
End Struct
 +
 +
Private Const _STAT_VER_LINUX as Integer = 1
 +
Private Const S_IFCHR As Integer = 8192
 +
Private Const S_IFDIR As Integer = 16384
 +
Private Const S_IFREG As Integer = 32768
 +
 +
<FONT Color=gray>' ''int __xstat (int __ver, const char *__filename, struct stat *__stat_buf)''
 +
' ''Get file attributes for FILE and put them in BUF.''</font>
 +
Private Extern <FONT color=#B22222>__xstat</font>(int __ver, const char *__filename, struct stat *__stat_buf) As Integer
 +
 
 +
 +
'''Public''' Sub Main()
 +
 
 +
  Dim i As Integer
 +
  Dim st As New stat_
 +
  Dim percorso, s As String
 +
 
 +
  percorso = "<FONT Color=gray>''/percorso/da/verificare''</font>"
 +
 
 +
  i = <FONT color=#B22222>__xstat</font>(_STAT_VER_LINUX, percorso, st)
 +
  If i < 0 Then Error.Raise("File o Cartella inesistente !")
 +
 +
  If st.st_mode And S_IFCHR Then s = "File Device"
 +
  If st.st_mode And S_IFDIR Then s = "Directory"
 +
  If st.st_mode And S_IFREG Then s = "File regolare"
 +
 
 +
  Print percorso; " è un "; s
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione attuale delle 11:41, 2 mar 2023

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

Per utilizzare la funzione esterna "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: /usr/include/x86_64-linux-gnu/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: /usr/include/x86_64-linux-gnu/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, avente una dimensione di 144 byte di memoria, che si passerà al terzo paramentro della funzione "__xstat()".

Dunque avremo la Struttura dichiarata:

Public Struct stat_
  st_dev As Long
  st_ino As Long
  st_nlink As Long
  st_mode As Integer
  st_uid As Integer
  st_gid As Integer
  __pad0 As Integer
  st_rdev As Long
  st_size As Long
  st_blksize As Long
  st_blocks As Long
  st_atime As Long
  st_atimensec As Long
  st_mtime As Long
  st_mtimensec As Long 
  st_ctime As Long
  st_ctimensec As Long
  __glibc_reserved[3] As Long
End Struct

il cui identificatore, affinché non si verifichi un grave errore all'avvio del programma, dovrà necessariamente essere diverso dalla parola stat, poiché coincidente con la Classe Stat nativa di Gambas.
Avremo, inoltre, la funzione "__xstat()" dichiarata:

Private Extern __xstat(__ver As Integer, __filename As String, __stat_buf As stat_) As Integer In "libc:6"

I membri st_mtime, st_mtime, st_ctime della precedente Struttura restituiscono dei valori numerici che rappresentano ciascuno una data ed un orario. Per convertire tali valori numerici in data e orario, dovremo fare uso delle funzioni di C: "localtime()" e "asctime()".


Semplice esempio di uso in Gambas:

Library "libc:6"

Public Struct stat_
  st_dev As Long
  st_ino As Long
  st_nlink As Long
  st_mode As Integer
  st_uid As Integer
  st_gid As Integer
  __pad0 As Integer
  st_rdev As Long
  st_size As Long
  st_blksize As Long
  st_blocks As Long
  st_atime As Long
  st_atimensec As Long
  st_mtime As Long
  st_mtimensec As Long 
  st_ctime As Long
  st_ctimensec 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(int __ver, const char *__filename, struct stat *__stat_buf) As Integer
 
' tm* localtime( const time_t *time )
' Converts given time since epoch as time_t value into calendar time, expressed in local time.
Private Extern localtime(tm As Pointer) As Pointer

' char* asctime( const tm* time_ptr )
' Converts given calendar time tm to a textual representation.
Private Extern asctime(time_ptr As Pointer) As String


Public Sub Main()
 
 Dim i As Integer
 Dim st As New stat_
 Dim at, mt, ct As Long

 i = __xstat(_STAT_VER_LINUX, "/percorso/del/file", st)
 If i < 0 Then Error.Raise("Errore alla funzione '__xstat()' !")
 
 With st
   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
   at = .st_atime
   mt = .st_mtime
   ct = .st_ctime
 End With
 
 Print asctime(localtime(VarPtr(at)))
 Print asctime(localtime(VarPtr(mt)))
 Print asctime(localtime(VarPtr(ct)))
 
End


In quest'altro esempio si verificherà se un determinato percorso è un file regolare o una Directory oppure un file-device:

Library "libc:6"

Public Struct stat_
  st_dev As Long
  st_ino As Long
  st_nlink As Long
  st_mode As Integer
  st_uid As Integer
  st_gid As Integer
  __pad0 As Integer
  st_rdev As Long
  st_size As Long
  st_blksize As Long
  st_blocks As Long
  st_atime As Long
  st_atimensec As Long
  st_mtime As Long
  st_mtimensec As Long 
  st_ctime As Long
  st_ctimensec As Long
  __glibc_reserved[3] As Long
End Struct

Private Const _STAT_VER_LINUX as Integer = 1
Private Const S_IFCHR As Integer = 8192
Private Const S_IFDIR As Integer = 16384
Private Const S_IFREG As Integer = 32768

' int __xstat (int __ver, const char *__filename, struct stat *__stat_buf)
' Get file attributes for FILE and put them in BUF.
Private Extern __xstat(int __ver, const char *__filename, struct stat *__stat_buf) As Integer
 

Public Sub Main()
 
 Dim i As Integer
 Dim st As New stat_
 Dim percorso, s As String
 
 percorso = "/percorso/da/verificare"
 
 i = __xstat(_STAT_VER_LINUX, percorso, st)
 If i < 0 Then Error.Raise("File o Cartella inesistente !")

 If st.st_mode And S_IFCHR Then s = "File Device"
 If st.st_mode And S_IFDIR Then s = "Directory"
 If st.st_mode And S_IFREG Then s = "File regolare"
 
 Print percorso; " è un "; s

End