forked from ThomasMertes/seed7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.sd7
81 lines (76 loc) · 2.81 KB
/
cat.sd7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
(********************************************************************)
(* *)
(* cat.sd7 Concatenate and print files *)
(* Copyright (C) 2015 Brian Callahan *)
(* *)
(* This program is free software; you can redistribute it and/or *)
(* modify it under the terms of the GNU General Public License as *)
(* published by the Free Software Foundation; either version 2 of *)
(* the License, or (at your option) any later version. *)
(* *)
(* This program is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
(* GNU General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU General Public *)
(* License along with this program; if not, write to the *)
(* Free Software Foundation, Inc., 51 Franklin Street, *)
(* Fifth Floor, Boston, MA 02110-1301, USA. *)
(* *)
(********************************************************************)
$ include "seed7_05.s7i";
include "stdio.s7i";
(**
* Open the file and print its contents. Handle "-" as stdin.
*)
const proc: cat (in string: fileName) is func
local
var file: inFile is STD_NULL;
var char: ch is ' ';
begin
if fileName = "-" then
inFile := IN;
else
inFile := open(fileName, "r");
end if;
if inFile <> STD_NULL then
repeat
ch := getc(inFile);
if ch <> EOF then
write(OUT, ch);
end if;
until ch = EOF;
if fileName <> "-" then
close(inFile);
end if;
else
writeln("cat: " <& fileName <& ": Not found or not readable");
end if;
end func;
(**
* This is a POSIX compatible cat.
*)
const proc: main is func
local
var integer: index is 0;
begin
if length(argv(PROGRAM)) < 1 then
cat("-");
elsif length(argv(PROGRAM)) = 1 and argv(PROGRAM)[1] = "-u" then
cat("-");
else
for index range 1 to length(argv(PROGRAM)) do
if index = 1 and argv(PROGRAM)[index][1] = '-' then
if argv(PROGRAM)[index] = "-" then
cat(argv(PROGRAM)[index]);
elsif argv(PROGRAM)[index] <> "-u" then
writeln("usage: cat [-u] [file ...]");
exit(1);
end if;
else
cat(argv(PROGRAM)[index]);
end if;
end for;
end if;
end func;