Your task is to implement a program similar to the
nl command-line tool from the GNU core utilities.
Standard loopholes are banned.
You may not use any built-in or external function, program or utility for numbering the lines of a file or string, such as
nl itself or the
= command in GNU sed.
The program accepts filenames as arguments. Your code does not have to be cross-platform; the filename format of the OS running the code should be used, i.e. if you happen to be on Windows, the directory separator can be
You must be able to take 64 input files, including
- if it is specified. If over 64 are given, only handle the first 64.
In the list of filenames,
- represents standard input.
If filenames are given, read from the files in the order that they are given and concatenate their contents, inserting a new line between each one and at the end. If you cannot read from one or more of the filenames (because the file does not exist or you do not have read permissions for it), ignore them. If all filenames specified are invalid, output nothing.
If no filenames are given, read from standard input. Only read from standard input if no filenames are given or if
- is given.
The program will output, to standard output, the input with lines numbered thus (You may assume that the input has
\r line endings; pick whichever is convenient for you, but specify which one):
<5 spaces>1<tab><content of line 1 of input> <5 spaces>2<tab><content of line 2 of input> ... <4 spaces>10<tab><content of line 10 of input> ... <3 spaces>100<tab><content of line 100 of input> ... ...
6 characters of space are allocated for the line number, and it is inserted at the end of these characters; the rest become spaces (e.g.
1 will have 5 leading spaces,
22 will have 4 leading spaces, ...). If the input is sufficiently long, you will eventually run out of space for the line number, at line
999999. You must not output anything after line 999999.
If the input is empty, output nothing.
The lower numbers take priority: if errors 1 and 2 were encountered, exit with status 1.
Exit with status 0 if the input was successfully received, and the lines successfully numbered and output.
Exit with status 1 if one or more of the files specified on the command line were not found or could not be read from.
Exit with status 2 if too many files (more than 64) were given.
Exit with status 3 if the input was too long (more than 999999 lines).\
This is code-golf - shortest program wins!
I may add bonuses later for implementing certain options that
nl has. There are no bonuses at the moment.