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