1 /* 2 * Test directory reading operations. 3 * 4 * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 #include <l4/sys/err.h> 24 25 #include <stdio.h> 26 #include <string.h> 27 #include <stdlib.h> 28 29 #include <fsclient/client.h> 30 #include <systypes/fcntl.h> 31 32 33 34 int main(int argc, char *argv[]) 35 { 36 if (argc < 2) 37 { 38 printf("Need a directory name.\n"); 39 return 1; 40 } 41 42 char *filename = argv[1]; 43 44 printf("Opening %s...\n", filename); 45 46 file_t *reader = client_opendir(filename); 47 48 if (reader == NULL) 49 { 50 printf("Could not read from directory.\n"); 51 return 1; 52 } 53 54 printf("Reading...\n"); 55 56 struct dirent *dirent; 57 58 while ((dirent = client_readdir(reader)) != NULL) 59 { 60 printf("> %s\n", dirent->d_name); 61 free(dirent); 62 } 63 64 printf("Directory shown.\n"); 65 66 /* Open again, reading a single entry only. */ 67 68 reader = client_opendir(filename); 69 70 if (reader == NULL) 71 { 72 printf("Could not read from directory.\n"); 73 return 1; 74 } 75 76 dirent = client_readdir(reader); 77 78 if (dirent != NULL) 79 { 80 printf("> %s\n", dirent->d_name); 81 free(dirent); 82 } 83 84 printf("Entry shown.\n"); 85 86 return 0; 87 } 88 89 // vim: tabstop=2 expandtab shiftwidth=2