ITT: make your best, most elegant implementation of cat(1) in C. Doesn't have to be POSIX compliant; for example, mine takes no option arguments because that's how I think it should be, whereas I think that POSIX specifies a few options.
#include <stdio.h>
int main(int argc, char **argv) {
char b[4096];
FILE *f = stdin;
int i;
size_t s;
if (argc == 1)
goto noargs;
for (i = 1; i < argc; i++) {
f = fopen(argv[i], "rb");
if (!f) {
fprintf(stderr, "%s: failed to open %s\n", argv[0], argv[i]);
continue;
}
noargs:
while ((s = fread(b, 1, 4096, f)))
fwrite(b, 1, s, stdout);
}
return 0;
}
Why is this the best in my opinion? It is short, simple, fast, serves just the purpose of concatenating files and no more, and even has a clever and elegant trick to handle the special case of no arguments without code duplication.
Mine isn't really POSIX compliant either, but it correctly returns something other than EXIT_SUCCESS when it fails to concatenate the files, so I feel it's still adequate.
Name:
Anonymous2012-02-16 3:47
>>2
Well, I could argue that while yours is adequate, mine is superior because yours has a provably 100% failure rate, while my failure rate is <= 100% depending on external factors.
This is perfect example of why declaring functions within functions with lexical scoping for the variables is a good language feature.
Name:
Anonymous2012-02-16 3:48
>>4
I don't see how this is an example of the necessity of nested functions with lexical scoping. Please elaborate.
Name:
Anonymous2012-02-16 4:05
>>5
I can't do it using standard C, but:
while ((s = fread(b, 1, 4096, f)))
fwrite(b, 1, s, stdout);
could be wrapped in a function, and then this function can be called from within the loop, and instead of using a goto to jump into the middle of the loop, you can just call the function.
Or is that correct? When you jump into the middle of the loop there, you expect the loop to terminate right? IE, the catting code is only run once? I don't think i is necessarily initialized when the comparison, i < argc is made.
Name:
Anonymous2012-02-16 4:22
>>6
Thanks for the catch; I can probably move the initialisation of i to the declaration, and leave the first 'trimester' of the for statement empty, so that i is always initialised. I suppose a function could be used, though in a program as trivial as this, there isn't any benefit that I can see.
Name:
Anonymous2012-02-16 4:25
>>6
Why should C need nested lexical scoping when the function could just be passed the FILE* as an argument? Using scoping captures instead of function arguments introduces the same problems as using global variables. void writeout(FILE *f) {
char b[4096];
size_t s;
while ((s = fread(b, 1, 4096, f)))
fwrite(b, 1, s, stdout);
}
>>16
Can you enumerate the possible inputs it doesn't fail on?
Name:
Anonymous2012-02-24 4:33
>>7
The program may be small, but even in small programs code duplication can come up, and factoring the duplicated code in a function and then calling it from each duplication point would be the safest way to factor it. And variable initialization is very explicit in function calls. Using gotos to jump into the middle of loops creates many implicit situations amongst the local variables, and very quickly, the only method for finding out is happening becomes manually tracing through it. When this happens, it means the concept of the program is destroyed, and all that remains is code to compute. The only way to recover the conceptual methods employed in the program is to become the computer and then realize what you are doing.
Name:
cat.hs2012-02-24 5:59
main = do
args <- getArgs
map (\x -> readFile x >>= putStr) args
. . . Did someone say elegant?
Name:
Anonymous2012-02-24 6:24
You can always just use redirects ...
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
return 0;
}
>>20
Except that's neither elegant, nor efficient.
Name:
Anonymous2012-02-24 7:34
>>23
It achieves cat function in very little code. I suppose if you wanted to include the error message, it'll be a bit longer. I'm not sure how one could achieve a denser solution than in any other practical language.
>>20 haskell
elegant (Show a, Show b, Show c) => Show (a, b, c)
(Show a, Show b, Show c, Show d) => Show (a, b, c, d)
(Show a, Show b, Show c, Show d, Show e) => Show (a, b, c, d, e)
(Show a, Show b, Show c, Show d, Show e, Show f) => Show (a, b, c, d, e, f)
(Show a, Show b, Show c, Show d, Show e, Show f, Show g) => Show (a, b, c, d, e, f, g)
(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h) => Show (a, b, c, d, e, f, g, h)
(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i) => Show (a, b, c, d, e, f, g, h, i)
(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j) => Show (a, b, c, d, e, f, g, h, i, j)
(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k) => Show (a, b, c, d, e, f, g, h, i, j, k)
(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l) => Show (a, b, c, d, e, f, g, h, i, j, k, l)
(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l, Show m) => Show (a, b, c, d, e, f, g, h, i, j, k, l, m)
(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l, Show m, Show n) => Show (a, b, c, d, e, f, g, h, i, j, k, l, m, n)
(Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k, Show l, Show m, Show n, Show o) => Show (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)
>>29
You think with all the academics pouting over haskell night and day that it's just some silly implementation problem that can be fixed in a new version? It's a design issue that clashes with the entire foundation of the language -- short of significant theoretical advances it won't ever be fixed.
Exactly. However, as the problem is framed, one can never hope to expect a rational number being output from such a generator in practice, albeit every number (even rational ones) have the same probability to occur. Despite, the practicality of an experiment of this sort is indeed rather dubious.
Name:
Anonymous2012-02-24 15:25
>>36,37
It never happens, the probability is exactly zero.
This has to do with the cardinality of the rational numbers versus the cardinality of the irrational numbers.
Try doing the (lebesgue) integral of the probability distribution over any real interval that extends over more than a single real number.
Name:
Anonymous2012-02-24 15:30
>>38 the probability is exactly zero.
Read the definition of the term in >>36. Its probability is zero, but it CAN happen, the same way P(x = sqrt(2)) = 0 but it obviously can occur.
>>38
Probability will be zero for any specific number you choose or any enumerable set - such as computable reals or algebraic numbers and so on. Reals themselves are not enumerable, hence why it's 0. However, that doesn't mean it doesn't "happen" (given the right set theoretic axioms).
Name:
Anonymous2012-02-24 16:11
>>39
No it's exactly zero, it can't happen.
Learn mathematics.
>>47
The 8008's A register became AX which then became EAX which AMD turned into RAX. The 8008 is a single-chip version of the Datapoint 2200 terminal processor. Intel's own Jew designs are all shit, so they're having better luck extending a 70's 8-bit embedded Goyish terminal chip. It's like if Ford kept adding more and more crap onto the Model T in the year 2012 even though all other car companies are selling modern cars.
/* cat -- concatenate files and print on the standard output.
Copyright (C) 1988, 1990-1991, 1995-2011 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>;. */
/* Differences from the Unix cat:
* Always unbuffered, -u is ignored.
* Usually much faster than other versions of cat, the difference
is especially apparent when using the -v option.
By tege@sics.se, Torbjorn Granlund, advised by rms, Richard Stallman. */
/* Name of input file. May be "-". */
static char const *infile;
/* Descriptor on which input file is open. */
static int input_desc;
/* Buffer for line numbers.
An 11 digit counter may overflow within an hour on a P2/466,
an 18 digit counter needs about 1000y */
#define LINE_COUNTER_BUF_LEN 20
static char line_buf[LINE_COUNTER_BUF_LEN] =
{
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0',
'\t', '\0'
};
/* Position in `line_buf' where printing starts. This will not change
unless the number of lines is larger than 999999. */
static char *line_num_print = line_buf + LINE_COUNTER_BUF_LEN - 8;
/* Position of the first digit in `line_buf'. */
static char *line_num_start = line_buf + LINE_COUNTER_BUF_LEN - 3;
/* Position of the last digit in `line_buf'. */
static char *line_num_end = line_buf + LINE_COUNTER_BUF_LEN - 3;
/* Preserves the `cat' function's local `newlines' between invocations. */
static int newlines2 = 0;
void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("\
Usage: %s [OPTION]... [FILE]...\n\
"),
program_name);
fputs (_("\
Concatenate FILE(s), or standard input, to standard output.\n\
\n\
-A, --show-all equivalent to -vET\n\
-b, --number-nonblank number nonempty output lines, overrides -n\n\
-e equivalent to -vE\n\
-E, --show-ends display $ at end of each line\n\
-n, --number number all output lines\n\
-s, --squeeze-blank suppress repeated empty output lines\n\
"), stdout);
fputs (_("\
-t equivalent to -vT\n\
-T, --show-tabs display TAB characters as ^I\n\
-u (ignored)\n\
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("\
\n\
With no FILE, or when FILE is -, read standard input.\n\
"), stdout);
printf (_("\
\n\
Examples:\n\
%s f - g Output f's contents, then standard input, then g's contents.\n\
%s Copy standard input to standard output.\n\
"),
program_name, program_name);
emit_ancillary_info ();
}
exit (status);
}
/* Compute the next line number. */
static void
next_line_num (void)
{
char *endp = line_num_end;
do
{
if ((*endp)++ < '9')
return;
*endp-- = '0';
}
while (endp >= line_num_start);
if (line_num_start > line_buf)
*--line_num_start = '1';
else
*line_buf = '>';
if (line_num_start < line_num_print)
line_num_print--;
}
/* Plain cat. Copies the file behind `input_desc' to STDOUT_FILENO.
Return true if successful. */
static bool
simple_cat (
/* Pointer to the buffer, used by reads and writes. */
char *buf,
/* Number of characters preferably read or written by each read and write
call. */
size_t bufsize)
{
/* Actual number of characters read, and therefore written. */
size_t n_read;
{
/* The following is ok, since we know that 0 < n_read. */
size_t n = n_read;
if (full_write (STDOUT_FILENO, buf, n) != n)
error (EXIT_FAILURE, errno, _("write error"));
}
}
}
/* Write any pending output to STDOUT_FILENO.
Pending is defined to be the *BPOUT - OUTBUF bytes starting at OUTBUF.
Then set *BPOUT to OUTPUT if it's not already that value. */
/* Cat the file behind INPUT_DESC to the file behind OUTPUT_DESC.
Return true if successful.
Called if any option more than -u was specified.
A newline character is always put at the end of the buffer, to make
an explicit test for buffer end unnecessary. */
static bool
cat (
/* Pointer to the beginning of the input buffer. */
char *inbuf,
/* Number of characters read in each read call. */
size_t insize,
/* Pointer to the beginning of the output buffer. */
char *outbuf,
/* Number of characters written by each write call. */
size_t outsize,
/* Variables that have values according to the specified options. */
bool show_nonprinting,
bool show_tabs,
bool number,
bool number_nonblank,
bool show_ends,
bool squeeze_blank)
{
/* Last character read from the input buffer. */
unsigned char ch;
/* Pointer to the next character in the input buffer. */
char *bpin;
/* Pointer to the first non-valid byte in the input buffer, i.e. the
current end of the buffer. */
char *eob;
/* Pointer to the position where the next character shall be written. */
char *bpout;
/* Number of characters read by the last read call. */
size_t n_read;
/* Determines how many consecutive newlines there have been in the
input. 0 newlines makes NEWLINES -1, 1 newline makes NEWLINES 1,
etc. Initially 0 to indicate that we are at the beginning of a
new line. The "state" of the procedure is determined by
NEWLINES. */
int newlines = newlines2;
#ifdef FIONREAD
/* If nonzero, use the FIONREAD ioctl, as an optimization.
(On Ultrix, it is not supported on NFS file systems.) */
bool use_fionread = true;
#endif
/* The inbuf pointers are initialized so that BPIN > EOB, and thereby input
is read immediately. */
eob = inbuf;
bpin = eob + 1;
bpout = outbuf;
Name:
Anonymous2012-02-24 21:49
>>51
while (true)
{
do
{
/* Write if there are at least OUTSIZE bytes in OUTBUF. */
if (outbuf + outsize <= bpout)
{
char *wp = outbuf;
size_t remaining_bytes;
do
{
if (full_write (STDOUT_FILENO, wp, outsize) != outsize)
error (EXIT_FAILURE, errno, _("write error"));
wp += outsize;
remaining_bytes = bpout - wp;
}
while (outsize <= remaining_bytes);
/* Move the remaining bytes to the beginning of the
buffer. */
/* Update the pointers and insert a sentinel at the buffer
end. */
bpin = inbuf;
eob = bpin + n_read;
*eob = '\n';
}
else
{
/* It was a real (not a sentinel) newline. */
/* Was the last line empty?
(i.e. have two or more consecutive newlines been read?) */
if (++newlines > 0)
{
if (newlines >= 2)
{
/* Limit this to 2 here. Otherwise, with lots of
consecutive newlines, the counter could wrap
around at INT_MAX. */
newlines = 2;
/* Are multiple adjacent empty lines to be substituted
by single ditto (-s), and this was the second empty
line? */
if (squeeze_blank)
{
ch = *bpin++;
continue;
}
}
/* Are line numbers to be written at empty lines (-n)? */
/* The loops below continue until a newline character is found,
which means that the buffer is empty or that a proper newline
has been found. */
/* If quoting, i.e. at least one of -v, -e, or -t specified,
scan for chars that need conversion. */
if (show_nonprinting)
{
while (true)
{
if (ch >= 32)
{
if (ch < 127)
*bpout++ = ch;
else if (ch == 127)
{
*bpout++ = '^';
*bpout++ = '?';
}
else
{
*bpout++ = 'M';
*bpout++ = '-';
if (ch >= 128 + 32)
{
if (ch < 128 + 127)
*bpout++ = ch - 128;
else
{
*bpout++ = '^';
*bpout++ = '?';
}
}
else
{
*bpout++ = '^';
*bpout++ = ch - 128 + 64;
}
}
}
else if (ch == '\t' && !show_tabs)
*bpout++ = '\t';
else if (ch == '\n')
{
newlines = -1;
break;
}
else
{
*bpout++ = '^';
*bpout++ = ch + 64;
}
ch = *bpin++;
}
}
Name:
Anonymous2012-02-24 21:49
>>52
else
{
/* Not quoting, neither of -v, -e, or -t specified. */
while (true)
{
if (ch == '\t' && show_tabs)
{
*bpout++ = '^';
*bpout++ = ch + 64;
}
else if (ch != '\n')
*bpout++ = ch;
else
{
newlines = -1;
break;
}
ch = *bpin++;
}
}
}
}
int
main (int argc, char **argv)
{
/* Optimal size of i/o operations of output. */
size_t outsize;
/* Optimal size of i/o operations of input. */
size_t insize;
size_t page_size = getpagesize ();
/* Pointer to the input buffer. */
char *inbuf;
/* Pointer to the output buffer. */
char *outbuf;
bool ok = true;
int c;
/* Index in argv to processed argument. */
int argind;
/* Device number of the output (file or whatever). */
dev_t out_dev;
/* I-node number of the output. */
ino_t out_ino;
/* True if the output file should not be the same as any input file. */
bool check_redirection = true;
/* Nonzero if we have ever read standard input. */
bool have_read_stdin = false;
struct stat stat_buf;
/* Variables that are set according to the specified options. */
bool number = false;
bool number_nonblank = false;
bool squeeze_blank = false;
bool show_ends = false;
bool show_nonprinting = false;
bool show_tabs = false;
int file_open_mode = O_RDONLY;
/* Arrange to close stdout if we exit via the
case_GETOPT_HELP_CHAR or case_GETOPT_VERSION_CHAR code.
Normally STDOUT_FILENO is used rather than stdout, so
close_stdout does nothing. */
atexit (close_stdout);
/* Parse command line options. */
while ((c = getopt_long (argc, argv, "benstuvAET", long_options, NULL))
!= -1)
{
switch (c)
{
case 'b':
number = true;
number_nonblank = true;
break;
case 'e':
show_ends = true;
show_nonprinting = true;
break;
case 'n':
number = true;
break;
case 's':
squeeze_blank = true;
break;
case 't':
show_tabs = true;
show_nonprinting = true;
break;
case 'u':
/* We provide the -u feature unconditionally. */
break;
outsize = io_blksize (stat_buf);
/* Input file can be output file for non-regular files.
fstat on pipes returns S_IFSOCK on some systems, S_IFIFO
on others, so the checking should not be done for those types,
and to allow things like cat < /dev/tty > /dev/tty, checking
is not done for device files either. */
/* Compare the device and i-node numbers of this input file with
the corresponding values of the (output file associated with)
stdout, and skip this input file if they coincide. Input
files cannot be redirected to themselves. */
/* Why are
(OUTSIZE - 1 + INSIZE * 4 + LINE_COUNTER_BUF_LEN + PAGE_SIZE - 1)
bytes allocated for the output buffer?
A test whether output needs to be written is done when the input
buffer empties or when a newline appears in the input. After
output is written, at most (OUTSIZE - 1) bytes will remain in the
buffer. Now INSIZE bytes of input is read. Each input character
may grow by a factor of 4 (by the prepending of M-^). If all
characters do, and no newlines appear in this block of input, we
will have at most (OUTSIZE - 1 + INSIZE * 4) bytes in the buffer.
If the last character in the preceding block of input was a
newline, a line number may be written (according to the given
options) as the first thing in the output buffer. (Done after the
new input is read, but before processing of the input begins.)
A line number requires seldom more than LINE_COUNTER_BUF_LEN
positions.
Align the output buffer to a page size boundary, for efficency on
some paging implementations, so add PAGE_SIZE - 1 bytes to the
request to make room for the alignment. */
/* @cat.c */
/* NOTE: GNU's cat will not cat a file into itself. Should we check for this?
Possibility of infinite loop here. POSIX doesn't specify what happens if
standard input happens to be standard output (cat to temp. file and then
write that to the output? don't write what was already written?) */
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define CATBUF_SIZE 65536 /* increase this if you want OMG MOAR SPEED! */
char catbuf[CATBUF_SIZE];
int main(int argc, char** argv) {
int uflag = 0, retval = 0, i = 1, c;
FILE *input;