1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test_files/programs/clip.c Sun Mar 19 01:18:23 2023 +0100
1.3 @@ -0,0 +1,157 @@
1.4 +/*
1.5 + * Show lines from a file.
1.6 + *
1.7 + * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/sys/err.h>
1.26 +
1.27 +#include <stdio.h>
1.28 +#include <stdlib.h>
1.29 +#include <string.h>
1.30 +
1.31 +#include <fsclient/client.h>
1.32 +#include <systypes/env.h>
1.33 +#include <systypes/fcntl.h>
1.34 +
1.35 +
1.36 +
1.37 +/* Read a line from the file. */
1.38 +
1.39 +static int readline(file_t *file, int *lineno, char **start, char **end)
1.40 +{
1.41 + static char buf[256];
1.42 + static offset_t current = 0, limit = 0;
1.43 + char *newline = NULL;
1.44 +
1.45 + do
1.46 + {
1.47 + /* Obtain file content. */
1.48 +
1.49 + if (!limit)
1.50 + {
1.51 + limit = client_read(file, buf, 256);
1.52 +
1.53 + if (!limit)
1.54 + {
1.55 + *start = NULL;
1.56 + *end = NULL;
1.57 + return 0;
1.58 + }
1.59 +
1.60 + current = 0;
1.61 +
1.62 + /* Start of line at start of buffer. */
1.63 +
1.64 + if (newline != NULL)
1.65 + {
1.66 + current += 1;
1.67 + *start = buf;
1.68 + *end = (char *) memchr(buf + current, (int) '\n', limit - current);
1.69 +
1.70 + if (*end == NULL)
1.71 + *end = buf + limit;
1.72 +
1.73 + return 1;
1.74 + }
1.75 + }
1.76 +
1.77 + /* Find newline. */
1.78 +
1.79 + newline = (char *) memchr(buf + current, (int) '\n', limit - current);
1.80 +
1.81 + if (newline != NULL)
1.82 + {
1.83 + (*lineno)++;
1.84 + current = newline - (char *) buf + 1;
1.85 +
1.86 + /* Start of line before end of buffer. */
1.87 +
1.88 + if (current < limit)
1.89 + {
1.90 + *start = newline + 1;
1.91 + *end = (char *) memchr(buf + current, (int) '\n', limit - current);
1.92 +
1.93 + if (*end == NULL)
1.94 + *end = buf + limit;
1.95 +
1.96 + return 1;
1.97 + }
1.98 +
1.99 + /* Otherwise, reset the buffer to read the start of line. */
1.100 +
1.101 + else
1.102 + limit = 0;
1.103 + }
1.104 +
1.105 + /* No newline: read more data. */
1.106 +
1.107 + else
1.108 + limit = 0;
1.109 + }
1.110 + while (1);
1.111 +}
1.112 +
1.113 +
1.114 +
1.115 +int main(int argc, char *argv[])
1.116 +{
1.117 + file_t *file;
1.118 + int i, startline, numlines;
1.119 + char *start, *end;
1.120 +
1.121 + if (argc < 4)
1.122 + return 1;
1.123 +
1.124 + file = client_open(argv[1], O_RDONLY);
1.125 +
1.126 + if (!client_opened(file))
1.127 + {
1.128 + if (file != NULL)
1.129 + printf("Error: %s\n", l4sys_errtostr(file->error));
1.130 + else
1.131 + printf("Could not open file.\n");
1.132 +
1.133 + while (1);
1.134 + }
1.135 +
1.136 + startline = atoi(argv[2]);
1.137 + numlines = atoi(argv[3]);
1.138 +
1.139 + i = 1;
1.140 + while (i < startline)
1.141 + if (!readline(file, &i, &start, &end))
1.142 + break;
1.143 +
1.144 + while (i < startline + numlines)
1.145 + {
1.146 + fwrite(start, sizeof(char), end - start, stdout);
1.147 + if (!readline(file, &i, &start, &end))
1.148 + break;
1.149 + }
1.150 +
1.151 + fputs("\n\n", stdout);
1.152 +
1.153 + client_close(file);
1.154 + client_notifier_close(client_notifier_task());
1.155 +
1.156 + return 0;
1.157 +}
1.158 +
1.159 +/* vim: tabstop=2 expandtab shiftwidth=2
1.160 +*/