~za08gywo

Übung 06 - Dateisystem

Published at 10.07.2025

Dateisystem

Beispiel Programme aus der Präsenzübung

Alle Einträge aus einem Verzeichnis ausgeben

#include <dirent.h>
#include <fnmatch.h>
#include <stdio.h>
#include <stdlib.h>

int patternMatch(const struct dirent *ent) {
  if (0 == fnmatch("*", ent->d_name, FNM_PERIOD)) {
    return 1;
  }
  return 0;
}

int main(int argc, char *argv[]) {
  if (argc < 2) {
    fprintf(stderr, "Usage: %s [DIR]\n", argv[0]);
    return -1;
  }

  char *dir = argv[1];

  struct dirent **namelist;

  int num_entries;
  num_entries = scandir(dir, &namelist, patternMatch, alphasort);
  if (num_entries == -1) {
    perror("scandir");
    exit(EXIT_FAILURE);
  }

  for (int i = 0; i < num_entries; i++) {
    printf("%s\n", namelist[i]->d_name);
  }
}