Advertisement
Guest User

fstat

a guest
Feb 12th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <sys/types.h>
  4. #include <time.h>
  5.  
  6. #include <fcntl.h> //Inclus les flags pour open
  7.  
  8. char* getType(struct stat f){
  9.     switch (f.st_mode & S_IFMT) {
  10.            case S_IFBLK:  return "périphérique bloc";       break;
  11.            case S_IFCHR:  return "périphérique caractère";  break;
  12.            case S_IFDIR:  return "répertoire";              break;
  13.            case S_IFIFO:  return "FIFO/tube";               break;
  14.            case S_IFLNK:  return "lien symbolique";         break;
  15.            case S_IFREG:  return "fichier ordinaire";       break;
  16.            case S_IFSOCK: return "socket";                  break;
  17.            default:       return "inconnu ?";               break;
  18.            }
  19. }
  20.  
  21. int main(int argc, char* argv[]){
  22.     if (argc != 2){
  23.         printf("Usage %s [file]\n", argv[0]);
  24.         return -1;
  25.     }
  26.     else{
  27.         int ld = open(argv[1], O_RDONLY);
  28.         struct stat retour;
  29.         if (fstat(ld, &retour) == 0){
  30.  
  31.             printf(" inode n° %i\n UID du propiétaire: %i \n GID du propriétaire: %i\n Taille (octets): %i\n Date dernier accès: %s Type : %s\n Droits : %c%c%c%c%c%c%c%c%c\n",
  32. retour.st_ino,
  33. retour.st_uid,
  34. retour.st_gid,
  35. retour.st_size,
  36. ctime(&retour.st_atime),
  37. getType(retour),
  38. (retour.st_mode & S_IRUSR) ? 'r' : '-', (retour.st_mode & S_IWUSR) ? 'w' : '-', (retour.st_mode & S_IXUSR) ? 'x' : '-',
  39. (retour.st_mode & S_IRGRP) ? 'r' : '-', (retour.st_mode & S_IWGRP) ? 'w' : '-', (retour.st_mode & S_IXGRP) ? 'x' : '-',
  40. (retour.st_mode & S_IROTH) ? 'r' : '-', (retour.st_mode & S_IWOTH) ? 'w' : '-', (retour.st_mode & S_IXOTH) ? 'x' : '-');
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement