Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <getopt.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <stdarg.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include "kitten.h"
- #define ERR_SIZE 256
- extern int errno;
- char err_buf[ERR_SIZE];
- int main ( int argc, char* argv[])
- {
- struct global_opt opt = {
- .number_nonblank = false, .number = false,
- .squeeze_blank = false, .show_nonprinting = false,
- .show_ends = false, .show_tabs = false
- };
- bool ok;
- int c;
- while (( c = getopt_long(argc, argv, "bnsvET", NULL, NULL)) != -1)
- {
- switch(c)
- {
- case 'b':
- opt.number = true;
- opt.number_nonblank = true;
- break;
- case 'n':
- opt.number = true;
- break;
- case 's':
- opt.squeeze_blank = true;
- break;
- case 'v':
- opt.show_nonprinting = true;
- break;
- case 'E':
- opt.show_ends = true;
- break;
- case 'T':
- opt.show_tabs = true;
- break;
- default:
- return -1;
- }
- }
- char *file = "-";
- int argid = optind;
- int input_desc;
- do
- {
- if( argid < argc)
- file = argv[argid];
- if( STREQ(file, "-"))
- input_desc = STDIN_FILENO;
- else
- {
- input_desc = open (file, O_RDONLY);
- if (input_desc < 0)
- {
- strncat(err_buf, "Cannot open ", 13);
- strncat(err_buf, file, strlen(file));
- strncat(err_buf, ": ", 2);
- strerror_r (errno,err_buf+13+strlen(file), ERR_SIZE);
- strncat(err_buf, "\n", 2);
- write ( STDOUT_FILENO, err_buf, ERR_SIZE);
- continue;
- }
- }
- if (! (opt.number || opt.show_ends || opt.show_nonprinting
- || opt.show_tabs || opt.squeeze_blank))
- ok &= simple_cat(input_desc);
- else ok &= cat_opt ( &opt, input_desc);
- }
- while ( ++argid < argc);
- return ok ? EXIT_SUCCESS : EXIT_FAILURE;
- }
- static char line_buf[ LINE_COUNTER] =
- {
- ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '\t', '\0'
- };
- static char* num_print = line_buf + LINE_COUNTER - 6;
- void next_line_num (void)
- {
- static char* num_start = line_buf + LINE_COUNTER - 3;
- char* endp = line_buf + LINE_COUNTER - 3;
- do
- {
- if( (*endp)++ < '9')
- return;
- *endp-- = '0';
- } while ( endp >= num_start);
- if (num_start > line_buf)
- *--num_start = '1';
- else
- *line_buf = '>';
- if(num_start < num_print)
- num_print--;
- }
- bool simple_cat(int input_desc)
- {
- char buf[BUF_SIZE] = {0};
- int char_read;
- while ( true )
- {
- char_read = read ( input_desc, buf, BUF_SIZE);
- if( char_read == -1 )
- {
- strerror_r (errno,err_buf, ERR_SIZE);
- strncat(err_buf, "\n", 2);
- write ( STDOUT_FILENO, err_buf, ERR_SIZE);
- return false;
- }
- if ( char_read == 0 )
- return true;
- {
- int n = char_read;
- if( write (STDOUT_FILENO, buf, n) != n )
- {
- strerror_r (errno,err_buf, ERR_SIZE);
- strncat(err_buf, "\n", 2);
- write ( STDOUT_FILENO, err_buf, ERR_SIZE);
- }
- }
- }
- }
- static inline void write_remain ( char* outbuf, char** bpout)
- {
- int n_write = *bpout - outbuf;
- if ( n_write > 0)
- {
- if(write (STDOUT_FILENO, outbuf, n_write) != n_write)
- {
- strerror_r (errno,err_buf, ERR_SIZE);
- strncat(err_buf, "\n", 2);
- write ( STDOUT_FILENO, err_buf, ERR_SIZE);
- }
- *bpout = outbuf;
- }
- }
- bool cat_opt( struct global_opt* p_opt, int input_desc)
- {
- char inbuf [BUF_SIZE];
- char outbuf [2*BUF_SIZE];
- int newlines = 0;
- unsigned char ch;
- char *bpin;
- char *eob;
- char *bpout;
- int n_read = 1;
- eob = inbuf;
- bpin = eob + 1;
- bpout = outbuf;
- while (n_read != 0)
- {
- do
- {
- if (outbuf + n_read <= bpout)
- {
- int n_write = bpout - outbuf;
- if( write ( STDOUT_FILENO, outbuf, n_write) != n_write)
- {
- strerror_r (errno,err_buf, ERR_SIZE);
- strncat(err_buf, "\n", 2);
- write ( STDOUT_FILENO, err_buf, ERR_SIZE);
- }
- bpout = outbuf;
- }
- if ( bpin > eob)
- {
- n_read = read ( input_desc, inbuf, BUF_SIZE);
- if ( n_read == -1)
- {
- strerror_r (errno,err_buf, ERR_SIZE);
- strncat(err_buf, "\n", 2);
- write ( STDOUT_FILENO, err_buf, ERR_SIZE);
- write_remain (outbuf, &bpout);
- return false;
- }
- if ( n_read == 0)
- {
- write_remain (outbuf, &bpout);
- return true;
- }
- bpin = inbuf;
- eob = bpin + n_read;
- *eob = '\n';
- }
- else
- {
- if( ++newlines > 0)
- {
- if( newlines >= 2)
- if( p_opt -> squeeze_blank)
- {
- ch = *bpin++;
- continue;
- }
- if(p_opt -> number && !(p_opt -> number_nonblank))
- {
- next_line_num();
- bpout = stpcpy(bpout, num_print);
- }
- }
- if (p_opt -> show_ends)
- *bpout++ = '$';
- *bpout++ = '\n';
- }
- ch = *bpin++;
- } while ( ch == '\n');
- if( newlines > -1 && p_opt -> number)
- {
- next_line_num();
- bpout = stpcpy (bpout, num_print);
- }
- while (bpout < outbuf + 2*BUF_SIZE )
- {
- if(ch == '\t' && p_opt -> show_tabs)
- {
- *bpout++ = '^';
- *bpout++ = 'I';
- }
- else if ( ch != '\n' )
- *bpout++ = ch;
- else
- {
- newlines = -1;
- break;
- }
- ch = *bpin++;
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement