Differenze tra le versioni di "Ottenere alcune informazioni generali sui file"

Da Gambas-it.org - Wikipedia.
 
(3 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
Per ottenere alcune informazioni generali sui file, è possibile operare con almeno due risorse.
+
Per ottenere alcune informazioni generali sui file, è possibile operare alcune risorse.
  
 +
===Usando la funzione "Stat()" nativa di Gambas===
 +
Le modalità di utilizzo della funzione di Gambas "Stat()" è descritta [[Stat|nella seguente pagina]].
  
===Usando la funzione ''Stat()'' propria di Gambas===
+
===Usando la funzione "stat()" di C===
Le modalità di utilizzo della funzione di Gambas ''Stat()'' è descritta [[Stat|nella seguente pagina]].
+
Per utilizzare la funzione "stat()" di C, bisognerà richiamare la funzione "[[Stat_()|__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()".
 
 
===Usando la funzione ''stat()'' di C===
 
Per utilizzare la funzione ''stat()'' di C, bisognerà richiamare la funzione ''[[Stat_()|__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()''.
 
 
 
  
 
Il codice Gambas potrà essere il seguente:
 
Il codice Gambas potrà essere il seguente:
 +
Library "libc:6"
 +
 
  Public Struct STATUS_File
 
  Public Struct STATUS_File
 
   st_dev As Long
 
   st_dev As Long
Riga 33: Riga 32:
 
   
 
   
 
  Private Const _STAT_VER_LINUX as Integer = 1
 
  Private Const _STAT_VER_LINUX as Integer = 1
 
Library "libc:6"
 
 
   
 
   
 
  <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)''
Riga 54: Riga 51:
 
   Dim i As Integer
 
   Dim i As Integer
 
   Dim at, mt, ct As Long
 
   Dim at, mt, ct As Long
 +
  Dim s As String = "<FONT Color=gray>''/percorso/del/file''</font>"
 +
 +
  i = __xstat(_STAT_VER_LINUX, s, stS)
 +
  If i <> 0 Then Error.Raise("Errore alla funzione '__xstat()' !")
 
    
 
    
  i = __xstat(_STAT_VER_LINUX, "''/percorso/del/file''", stS)
+
  Print "File controllato: "; s
  If i <> 0 Then Error.Raise("Errore alla funzione '__xstat()' !")
+
  Print
 
+
  With stS
+
  With stS
    Print .st_dev
+
    Print .st_dev
    Print .st_ino
+
    Print .st_ino
    Print .st_nlink
+
    Print .st_nlink
    Print .st_mode
+
    Print .st_mode
    Print .st_uid
+
    Print .st_uid
    Print .st_gid
+
    Print .st_gid
    Print .st_rdev
+
    Print .st_rdev
    Print .st_size; " byte"
+
    Print .st_size; " byte"
    Print .st_blksize
+
    Print .st_blksize
    Print .st_blocks
+
    Print .st_blocks
    at = .st_atime
+
    at = .st_atime
    mt = .st_mtime
+
    mt = .st_mtime
    ct = .st_ctime
+
    ct = .st_ctime
  End With
+
  End With
 
    
 
    
  Print asctime(localtime(VarPtr(at)))
+
  Print asctime(localtime(VarPtr(at)))
  Print asctime(localtime(VarPtr(mt)))
+
  Print asctime(localtime(VarPtr(mt)))
  Print asctime(localtime(VarPtr(ct)))
+
  Print asctime(localtime(VarPtr(ct)))
 
   
 
   
 
  '''End'''
 
  '''End'''
  
 
+
===Usando la funzione "stat()" di C mediante una libreria esterna ''ad hoc''===
===Usando la funzione ''stat()'' di C mediante una libreria esterna ''ad hoc''===
+
La funzione esterna "stat()" di C potrà essere utilizzata anche mediante una libreria esterna condivisa .so, da noi realizzata, nella quale verrà posta la funzione.
La funzione ''stat()'' di C potrà essere utilizzata anche mediante una libreria esterna dinamica e condivisa .so, da noi realizzata, nella quale verrà posta la funzione.
+
<BR>Il codice Gambas, contenente anche una procedura per generare il file sorgente in linguaggio C della libreria esterna condivisa, nonché la libreria medesima .so, sarà il seguente:
Il codice Gambas, contenente anche una procedura per generare il file sorgente in linguaggio C della libreria esterna condivisa, nonché la libreria medesima .so, sarà il seguente:
+
Library "libc:6"
 +
 
  Public Struct STATUS_File
 
  Public Struct STATUS_File
 
   st_dev As Long
 
   st_dev As Long
Riga 102: Riga 104:
 
   __glibc_reserved[3] As Long
 
   __glibc_reserved[3] As Long
 
  End Struct
 
  End Struct
 
 
 
Library "libc:6"
 
 
   
 
   
 
  <FONT Color=gray>' ''tm* localtime( const time_t *time )''
 
  <FONT Color=gray>' ''tm* localtime( const time_t *time )''
Riga 114: Riga 113:
 
  Private Extern asctime(time_ptr As Pointer) As String
 
  Private Extern asctime(time_ptr As Pointer) As String
 
   
 
   
  Private Extern Chiama_stat(nomefile As String, stFile As STATUS_File) As Integer In "/tmp/libSTATUS"
+
  Private Extern Chiama_stat(nomefile As String, stFile As STATUS_File) As Integer In "/tmp/libadhoc"
 
   
 
   
 
   
 
   
Riga 123: Riga 122:
 
   Dim at, mt, ct As Long
 
   Dim at, mt, ct As Long
 
   Dim s As String = "<FONT Color=gray>''/percorso/del/file''</font>"
 
   Dim s As String = "<FONT Color=gray>''/percorso/del/file''</font>"
 
 
  CreaSo()
 
 
   
 
   
  i = Chiama_stat(s, stS)
+
  CreaSo()
  If i <> 0 Then Error.Raise("Errore nella chiamata della funzione 'Chiama_stat()' !")
+
   
+
  i = Chiama_stat(s, stS)
  Print "File controllato: "; s
+
  If i <> 0 Then Error.Raise("Errore nella chiamata della funzione 'Chiama_stat()' !")
  Print
+
 
 +
  Print "File controllato: "; s
 +
  Print
 +
 +
  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
 +
    at = .st_atime
 +
    mt = .st_mtime
 +
    ct = .st_ctime
 +
  End With
 
   
 
   
  With stS
+
   Print asctime(localtime(VarPtr(at)))
    Print .st_dev
+
  Print asctime(localtime(VarPtr(mt)))
    Print .st_ino
+
  Print asctime(localtime(VarPtr(ct)))
    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'''
 
  '''End'''
 
 
   
 
   
 
  '''Private''' Procedure CreaSo()
 
  '''Private''' Procedure CreaSo()
 
   
 
   
  <FONT Color=gray>' ''Scriviamo e salviamo il file sorgente in C della libreria condivisa ad hoc:''</font>
+
  <FONT Color=gray>' ''Scrive e salva il file sorgente in C della libreria condivisa ad hoc:''</font>
  File.Save("/tmp/libadhoc.c", "#include <sys/stat.h>" &
+
  File.Save("/tmp/libadhoc.c", "#include <sys/stat.h>" &
            "\n\nstruct stat status;" &
+
                              "\n\nstruct stat status;" &
            "\n\nint Chiama_stat(const char *s, struct stat *st) {" &
+
                              "\n\nint Chiama_stat(const char *s, struct stat *st) {" &
            "\nint r;" &
+
                              "\nint r;" &
            "\nr = stat(s, &status);" &
+
                              "\nr = stat(s, &status);" &
            "\n*st = status;" &
+
                              "\n*st = status;" &
            "\nreturn r;" &
+
                              "\nreturn r;" &
            "\n}")
+
                              "\n}")
 
   
 
   
  <FONT Color=gray>' ''Generiamo la libreria condivisa .so ad hoc:''</font>
+
  <FONT Color=gray>' ''Genera la libreria condivisa .so ad hoc:''</font>
  Shell "gcc -o /tmp/libadhoc.so /tmp/libadhoc.c -shared -fPIC" Wait
+
  Shell "gcc -o /tmp/libadhoc.so /tmp/libadhoc.c -shared -fPIC" Wait
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione attuale delle 16:14, 11 set 2022

Per ottenere alcune informazioni generali sui file, è possibile operare alcune risorse.

Usando la funzione "Stat()" nativa di Gambas

Le modalità di utilizzo della funzione di Gambas "Stat()" è descritta nella seguente pagina.

Usando la funzione "stat()" di C

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()".

Il codice Gambas potrà essere il seguente:

Library "libc:6"

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
  __unused2 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, s As String, st As STATUS_File) 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 stS As New STATUS_File
 Dim i As Integer
 Dim at, mt, ct As Long
 Dim s As String = "/percorso/del/file"

 i = __xstat(_STAT_VER_LINUX, s, stS)
 If i <> 0 Then Error.Raise("Errore alla funzione '__xstat()' !")
 
 Print "File controllato: "; s
 Print

 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
   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

Usando la funzione "stat()" di C mediante una libreria esterna ad hoc

La funzione esterna "stat()" di C potrà essere utilizzata anche mediante una libreria esterna condivisa .so, da noi realizzata, nella quale verrà posta la funzione.
Il codice Gambas, contenente anche una procedura per generare il file sorgente in linguaggio C della libreria esterna condivisa, nonché la libreria medesima .so, sarà il seguente:

Library "libc:6"

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
  unused2 As Long
  st_ctime As Long
  __glibc_reserved[3] As Long
End Struct

' 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

Private Extern Chiama_stat(nomefile As String, stFile As STATUS_File) As Integer In "/tmp/libadhoc"


Public Sub Main()

 Dim i As Integer
 Dim stS As New STATUS_File
 Dim at, mt, ct As Long
 Dim s As String = "/percorso/del/file"

 CreaSo()

 i = Chiama_stat(s, stS)
 If i <> 0 Then Error.Raise("Errore nella chiamata della funzione 'Chiama_stat()' !")
  
 Print "File controllato: "; s
 Print

 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
   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

Private Procedure CreaSo()

' Scrive e salva il file sorgente in C della libreria condivisa ad hoc:
 File.Save("/tmp/libadhoc.c", "#include <sys/stat.h>" &
                              "\n\nstruct stat status;" &
                              "\n\nint Chiama_stat(const char *s, struct stat *st) {" &
                              "\nint r;" &
                              "\nr = stat(s, &status);" &
                              "\n*st = status;" &
                              "\nreturn r;" &
                              "\n}")

' Genera la libreria condivisa .so ad hoc:
 Shell "gcc -o /tmp/libadhoc.so /tmp/libadhoc.c -shared -fPIC" Wait

End