1 /* 2 * Test file access. 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 directory names.\n"); 39 return 1; 40 } 41 42 for (int i = 1; i < argc; i++) 43 { 44 char *dirname = argv[i]; 45 46 printf("Opening %s...\n", dirname); 47 48 file_t *reader = client_opendir(dirname); 49 50 if (!client_opened(reader)) 51 { 52 client_close(reader); 53 printf("Could not read from directory: %s\n", dirname); 54 continue; 55 } 56 57 printf(" Reading...\n"); 58 59 struct dirent *dirent; 60 61 while ((dirent = client_readdir(reader)) != NULL) 62 { 63 char *filename = dirent->d_name; 64 if (!strlen(filename) || (filename[0] == '.')) 65 continue; 66 67 char path[strlen(filename) + strlen(dirname) + 2]; 68 69 sprintf(path, "%s/%s", dirname, filename); 70 71 printf(" Attempt to read %s...\n", filename); 72 73 file_t *file = client_open(path, O_RDONLY); 74 75 if (!client_opened(file)) 76 printf(" Could not open file: %s\n", filename); 77 else 78 { 79 char buffer[32]; 80 offset_t nread = client_read(file, buffer, 32); 81 82 fputs(" Start of file:\n----\n", stdout); 83 fwrite(buffer, sizeof(char), nread, stdout); 84 fputs("\n----\n", stdout); 85 } 86 87 client_close(file); 88 free(dirent); 89 } 90 91 client_close(reader); 92 printf("Closing directory %s.\n\n", dirname); 93 } 94 95 printf("End of test.\n"); 96 return 0; 97 } 98 99 // vim: tabstop=2 expandtab shiftwidth=2