paul@158 | 1 | /* |
paul@158 | 2 | * Test directory reading. |
paul@158 | 3 | * |
paul@158 | 4 | * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> |
paul@158 | 5 | * |
paul@158 | 6 | * This program is free software; you can redistribute it and/or |
paul@158 | 7 | * modify it under the terms of the GNU General Public License as |
paul@158 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@158 | 9 | * the License, or (at your option) any later version. |
paul@158 | 10 | * |
paul@158 | 11 | * This program is distributed in the hope that it will be useful, |
paul@158 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@158 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@158 | 14 | * GNU General Public License for more details. |
paul@158 | 15 | * |
paul@158 | 16 | * You should have received a copy of the GNU General Public License |
paul@158 | 17 | * along with this program; if not, write to the Free Software |
paul@158 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@158 | 19 | * Boston, MA 02110-1301, USA |
paul@158 | 20 | */ |
paul@158 | 21 | |
paul@158 | 22 | #include <dirent.h> |
paul@158 | 23 | #include <stdio.h> |
paul@158 | 24 | |
paul@158 | 25 | |
paul@158 | 26 | |
paul@158 | 27 | int main(int argc, char *argv[]) |
paul@158 | 28 | { |
paul@158 | 29 | if (argc < 2) |
paul@158 | 30 | { |
paul@158 | 31 | printf("Need directory name.\n"); |
paul@158 | 32 | return 1; |
paul@158 | 33 | } |
paul@158 | 34 | |
paul@158 | 35 | /* Obtain directory name. */ |
paul@158 | 36 | |
paul@158 | 37 | char *filename = argv[1]; |
paul@158 | 38 | DIR *dir = opendir(filename); |
paul@158 | 39 | |
paul@158 | 40 | if (dir == NULL) |
paul@158 | 41 | { |
paul@158 | 42 | printf("Could not obtain directory: %s\n", filename); |
paul@158 | 43 | return 1; |
paul@158 | 44 | } |
paul@158 | 45 | |
paul@158 | 46 | struct dirent *dirent; |
paul@158 | 47 | |
paul@158 | 48 | while ((dirent = readdir(dir)) != NULL) |
paul@158 | 49 | printf("> %s\n", dirent->d_name); |
paul@158 | 50 | |
paul@158 | 51 | printf("Directory shown.\n"); |
paul@158 | 52 | |
paul@158 | 53 | return 0; |
paul@158 | 54 | } |
paul@158 | 55 | |
paul@158 | 56 | // vim: tabstop=2 expandtab shiftwidth=2 |