Access ()

Da Gambas-it.org - Wikipedia.

La funzione access() dichiarata nella libreria /usr/include/unistd.h

int access (const char *__name, int __type)

verifica se il file, specificato nel prima parametro, sia accessibile con una o più modalità indicate nel secondo parametro __type .

Le modalità sono specificate nel predetto file di sistema "unistd.h" dalle seguenti costanti:

/* Values for the second argument to access.
  These may be OR'd together.  */
  #define      R_OK    4               /* Test for read permission.  */
  #define      W_OK    2               /* Test for write permission.  */
  #define      X_OK    1               /* Test for execute permission.  */
  #define      F_OK    0               /* Test for existence.  */

Se la funzione access( ) restituisce zero, allora il file è accessibile per la modalità (o le modalità) specificata nel secondo parametro della funzione medesima.
Come già accennato, è possibile testare l'accessibilità del file anche con riferimeno a più modalità fra quelle sopra descritte, utilizzando l'operatore OR .


Volendola utilizzare in Gambas, bisognerà dichiararla con Extern, nonché dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta. Da sottolineare che, poiché questa funzione esterna "access()" è omonima alla funzione di Gambas "Access()", bisognerà assegnarle un nome a piacere, ma si dovrà anche richiamare il suo vero nome con il comando Exec.
Dunque avremo ad esempio:

Private Extern access_C(__name As String, __type As Integer) As Integer In "libc:6" Exec "access"


Semplice esempio di uso in Gambas:

Library "libc:6"

Private Enum F_OK = 0, X_OK, W_OK, R_OK = 4

' int access (const char *__name, int __type)
' Test for access to NAME using the real UID and real GID.
Private Extern access_C(__name As String, __type As Integer) As Integer Exec "access"


Public Sub Main()

 Dim i As Integer
 
' Verifica se il file è contemporaneamente eseguibile e accessibile in lettura:
  i = access_C("/percorso/del/file", X_OK Or R_OK)
  If i < 0 Then Error.Raise("Errore alla funzione 'access()' !")
 
End



Riferimenti