commit: 9040d023eaf1b34eb1b5a952a9fdcb8d363ff4e3
parent 48068a36f4ee18e127a24574077ba17cbdd1eda9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Wed, 22 Jan 2025 01:17:42 +0100
cmd/uniq: Handle "-" argument
Diffstat:
M | cmd/uniq.1 | 19 | ++++++++++++++++--- |
M | cmd/uniq.c | 55 | ++++++++++++++++++++++++++++++++----------------------- |
2 files changed, 48 insertions(+), 26 deletions(-)
diff --git a/cmd/uniq.1 b/cmd/uniq.1
@@ -17,10 +17,23 @@
The
.Nm
utility reads
-.Ar file1
-and
-.Ar file2
+.Ar input_file
line-by-line and by default filters out repeating lines.
+.Pp
+If no
+.Ar input_file
+is given or if
+.Ar input_file is
+.Qq - ,
+.Nm
+reads from the standard input.
+If no
+.Ar output_file
+is given or if
+.Ar output_file is
+.Qq - ,
+.Nm
+writes to the standard output.
.Sh OPTIONS
The following options are supported:
.Bl -tag -width Ds
diff --git a/cmd/uniq.c b/cmd/uniq.c
@@ -119,38 +119,47 @@ main(int argc, char *argv[])
case 0:
break;
case 1:
- input = fopen(argv[0], "r");
- input_name = argv[0];
- if(input == NULL)
+ if(strcmp(argv[0], "-") != 0)
{
- fprintf(
- stderr, "uniq: error: Failed opening input file '%s': %s\n", argv[0], strerror(errno));
- return 1;
+ input = fopen(argv[0], "r");
+ input_name = argv[0];
+ if(input == NULL)
+ {
+ fprintf(
+ stderr, "uniq: error: Failed opening input file '%s': %s\n", argv[0], strerror(errno));
+ return 1;
+ }
}
break;
case 2:
- input = fopen(argv[0], "r");
- input_name = argv[0];
- if(input == NULL)
+ if(strcmp(argv[0], "-") != 0)
{
- fprintf(
- stderr, "uniq: error: Failed opening input file '%s': %s\n", argv[0], strerror(errno));
- return 1;
+ input = fopen(argv[0], "r");
+ input_name = argv[0];
+ if(input == NULL)
+ {
+ fprintf(
+ stderr, "uniq: error: Failed opening input file '%s': %s\n", argv[0], strerror(errno));
+ return 1;
+ }
}
- output = fopen(argv[1], "w");
- output_name = argv[1];
- if(output == NULL)
+ if(strcmp(argv[1], "-") != 0)
{
- fprintf(
- stderr, "uniq: error: Failed opening output file '%s': %s\n", argv[1], strerror(errno));
+ output = fopen(argv[1], "w");
+ output_name = argv[1];
+ if(output == NULL)
+ {
+ fprintf(
+ stderr, "uniq: error: Failed opening output file '%s': %s\n", argv[1], strerror(errno));
- if(fclose(input) != 0)
- fprintf(stderr,
- "uniq: error: Failed closing input file '%s': %s\n",
- input_name,
- strerror(errno));
+ if(fclose(input) != 0)
+ fprintf(stderr,
+ "uniq: error: Failed closing input file '%s': %s\n",
+ input_name,
+ strerror(errno));
- return 1;
+ return 1;
+ }
}
break;
default: