Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
¿Qué es S_IFMT en la programación del sistema UNIX?

Estoy aprendiendo llamadas al sistema y, por lo tanto, estaba escribiendo un código para implementar ls usando lenguaje C. El código funciona, pero no puedo entender el funcionamiento de

 val=(mystat.st_mode & ~S_IFMT)

en el código dado a continuación? Entiendo el resto del código.

 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <time.h> int main(int argc, char* argv[]) { DIR *mydir; struct dirent *myfile; struct stat mystat; mydir = opendir(argv[1]); char buf[512]; while((myfile = readdir(mydir)) != NULL) { struct tm *time_stamp=localtime(&mystat.st_mtime); sprintf(buf, "%s/%s", argv[1], myfile->d_name); stat(buf, &mystat); //stat(myfile->d_name, &mystat); mode_t val; val=(mystat.st_mode & ~S_IFMT); (val & S_IRUSR) ? printf("r") : printf("-"); (val & S_IWUSR) ? printf("w") : printf("-"); (val & S_IXUSR) ? printf("x") : printf("-"); (val & S_IRGRP) ? printf("r") : printf("-"); (val & S_IWGRP) ? printf("w") : printf("-"); (val & S_IXGRP) ? printf("x") : printf("-"); (val & S_IROTH) ? printf("r") : printf("-"); (val & S_IWOTH) ? printf("w") : printf("-"); (val & S_IXOTH) ? printf("x") : printf("-"); printf("\t%d",mystat.st_nlink); printf("\t%d",mystat.st_uid); printf("\t%d",mystat.st_gid); printf("\t%d",mystat.st_size); char buffer[80]; strftime(buffer,10,"%b",time_stamp); printf("\t%4d %s %2d ", time_stamp->tm_year+1900,buffer,time_stamp->tm_mday); printf(" %s\n", myfile->d_name); } closedir(mydir); }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

S_IFMT es una máscara de bits para el tipo de archivo (ver man stat )

AND bit a bit directamente con mystat.st_mode ( mystat.st_mode & S_IFMT ) significa considerar solo los bits involucrados para determinar el tipo de archivo (archivo normal, socket, bloque o dispositivo char, etc.)

hacer un AND bit a bit de mystat.st_mode con la máscara de bits negada bit a bit ( mystat.st_mode & ~S_IFMT ) significa ignorar los bits explicados anteriormente, manteniendo solo los necesarios para determinar el permiso del archivo (las 9 líneas debajo de ese comando).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!