1 /* 2 * Test renaming 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 /* Rename some files. */ 67 68 char source[strlen(filename) + strlen("/file-XXXX.txt") + 10]; 69 char target[strlen(filename) + strlen("/file-XXXX.txt") + 10]; 70 int filenum; 71 72 for (filenum = 1; filenum < 100; filenum++) 73 { 74 sprintf(source, "%s/file-%d.txt", filename, filenum); 75 sprintf(target, "%s/renamed-%d.txt", filename, filenum); 76 77 long err = client_rename(source, target); 78 79 if (err) 80 { 81 printf("Could not rename file: %s\n", source); 82 return 1; 83 } 84 } 85 86 /* Show the new listing. */ 87 88 reader = client_opendir(filename); 89 90 if (reader == NULL) 91 { 92 printf("Could not read from directory.\n"); 93 return 1; 94 } 95 96 printf("Reading...\n"); 97 98 while ((dirent = client_readdir(reader)) != NULL) 99 { 100 printf("> %s\n", dirent->d_name); 101 free(dirent); 102 } 103 104 printf("Directory shown.\n"); 105 106 return 0; 107 } 108 109 // vim: tabstop=2 expandtab shiftwidth=2