1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test_files/programs/ls.c Mon Jan 15 23:44:56 2024 +0100
1.3 @@ -0,0 +1,183 @@
1.4 +/*
1.5 + * List filesystem objects using the client library.
1.6 + *
1.7 + * Originally developed as fsaccess/op_list_objects.c, this should eventually
1.8 + * use C library functions instead of client library functions.
1.9 + *
1.10 + * Copyright (C) 2019, 2022, 2024 Paul Boddie <paul@boddie.org.uk>
1.11 + *
1.12 + * This program is free software; you can redistribute it and/or
1.13 + * modify it under the terms of the GNU General Public License as
1.14 + * published by the Free Software Foundation; either version 2 of
1.15 + * the License, or (at your option) any later version.
1.16 + *
1.17 + * This program is distributed in the hope that it will be useful,
1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.20 + * GNU General Public License for more details.
1.21 + *
1.22 + * You should have received a copy of the GNU General Public License
1.23 + * along with this program; if not, write to the Free Software
1.24 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.25 + * Boston, MA 02110-1301, USA
1.26 + */
1.27 +
1.28 +#include <stdio.h>
1.29 +#include <stdlib.h>
1.30 +#include <string.h>
1.31 +
1.32 +#include <sys/stat.h>
1.33 +#include <sys/types.h>
1.34 +
1.35 +#include <e2access/format.h> /* get_permission_string */
1.36 +#include <e2access/path.h>
1.37 +#include <systypes/format.h>
1.38 +
1.39 +#define FMTnlinkd "%" pFMTnlink "d"
1.40 +
1.41 +
1.42 +
1.43 +/* NOTE: For inclusion in the C library: stream acquisition and access. */
1.44 +
1.45 +#include <fsclient/client.h>
1.46 +#include <systypes/env.h>
1.47 +#include <systypes/fcntl.h>
1.48 +
1.49 +file_t *output;
1.50 +
1.51 +
1.52 +
1.53 +/* Show object details. */
1.54 +
1.55 +static void _show_object(const char *basename, struct stat *st)
1.56 +{
1.57 + char buffer[strlen(basename) + 64];
1.58 +
1.59 + sprintf(buffer, "%s%s %5d %5d %6ld " FMTnlinkd " %s\n",
1.60 + S_ISDIR(st->st_mode) ? "d" : "-",
1.61 + get_permission_string(st->st_mode),
1.62 + st->st_uid,
1.63 + st->st_gid,
1.64 + st->st_size,
1.65 + st->st_nlink,
1.66 + basename);
1.67 +
1.68 + client_write(output, buffer, strlen(buffer));
1.69 +}
1.70 +
1.71 +/* Show an object in a directory. */
1.72 +
1.73 +static int _show_dirent(const char *dirname, struct dirent *dirent)
1.74 +{
1.75 + struct stat st;
1.76 + char path[strlen(dirname) + 1 + strlen(dirent->d_name) + 1];
1.77 +
1.78 + sprintf(path, "%s/%s", dirname, dirent->d_name);
1.79 +
1.80 + if (client_stat(path, &st))
1.81 + return 1;
1.82 +
1.83 + _show_object(dirent->d_name, &st);
1.84 +
1.85 + return 0;
1.86 +}
1.87 +
1.88 +/* List a directory or file. */
1.89 +
1.90 +static int _list_object(const char *path)
1.91 +{
1.92 + struct stat st;
1.93 + file_t *reader;
1.94 + struct dirent *dirent;
1.95 +
1.96 + if (client_stat(path, &st))
1.97 + return 1;
1.98 +
1.99 + if (S_ISDIR(st.st_mode))
1.100 + {
1.101 + reader = client_opendir(path);
1.102 +
1.103 + if (reader == NULL)
1.104 + return 1;
1.105 +
1.106 + /* Show the directory entries. */
1.107 +
1.108 + while ((dirent = client_readdir(reader)) != NULL)
1.109 + {
1.110 + if (_show_dirent(path, dirent))
1.111 + {
1.112 + free(dirent);
1.113 + return 1;
1.114 + }
1.115 +
1.116 + free(dirent);
1.117 + }
1.118 +
1.119 + client_close(reader);
1.120 + }
1.121 + else
1.122 + _show_object(path_basename(path), &st);
1.123 +
1.124 + return 0;
1.125 +}
1.126 +
1.127 +/* List an object in the filesystem image. */
1.128 +
1.129 +static int list_object(const char *path)
1.130 +{
1.131 + /* Emit each object's name. */
1.132 +
1.133 + client_write(output, path, strlen(path));
1.134 + client_write(output, "\n", 1);
1.135 +
1.136 + /* List individual files or directories. */
1.137 +
1.138 + if (_list_object(path))
1.139 + {
1.140 + /* NOTE: Should write via a standard error stream provided in the same
1.141 + way as the output stream. */
1.142 +
1.143 + fprintf(stderr, "Failed to list object: %s\n", path);
1.144 + return 1;
1.145 + }
1.146 +
1.147 + return 0;
1.148 +}
1.149 +
1.150 +/* List objects in the filesystem image. */
1.151 +
1.152 +int main(int argc, char *argv[])
1.153 +{
1.154 + int i;
1.155 +
1.156 + /* NOTE: For inclusion in the C library: stream acquisition and access. */
1.157 +
1.158 + output = client_get_stream(ENV_OUTPUT_STREAM_NAME, O_WRONLY);
1.159 +
1.160 + /* List the top level without any argument. */
1.161 +
1.162 + if (argc < 2)
1.163 + {
1.164 + if (list_object(""))
1.165 + return 1;
1.166 + }
1.167 + else
1.168 + {
1.169 + /* Otherwise, list all specified objects. */
1.170 +
1.171 + for (i = 1; i < argc; i++)
1.172 + {
1.173 + if (list_object(argv[i]))
1.174 + return 1;
1.175 + }
1.176 + }
1.177 +
1.178 + /* NOTE: For inclusion in the C library: stream acquisition and access. */
1.179 +
1.180 + client_flush(output);
1.181 +
1.182 + return 0;
1.183 +}
1.184 +
1.185 +/* vim: tabstop=4 expandtab shiftwidth=4
1.186 +*/