#include #include #include int main(int argc, char **argv) { /* Operation mode. */ #ifdef HEAD int head = 1; #else int head = 0; #endif #ifdef TAIL int tail = 1; #else int tail = 0; #endif /* Line number or count associated with operation. This is positive if measured from start of file, or negative if measured from end of file. */ int line; /* Current read position. */ int current_line = 1; char *buffer = NULL; /* Input buffer. */ int capacity = 0; /* Buffer capacity. */ int size = 0; /* Number of bytes read. */ int eof = 0; /* End of file. */ char *r, *w; /* General purpose pointers inside buffer. */ char *e; /* End of previous line (1 byte after newline). */ if( argc != 2 ) { return printf("%s {line_%s}\n", *argv, head || tail ? "count" : "number"); } line = atoi(argv[1]); if( head ) line = abs(line); else if( tail ) line = -abs(line); while( !eof ) { /* Extend input buffer. */ if( size == capacity ) { if( (buffer = (char*)realloc(buffer, capacity += 4096)) == NULL ) { fprintf(stderr, "Out of memory (need %d)\n", capacity); return 1; } } size += fread(buffer + size, 1, capacity - size, stdin); eof = size < capacity; if( line >= 0 ) { /* Scanning forward. */ for(r = w = buffer; r != buffer + size; r++) { if( (head && current_line <= line) || (!head && current_line != line) ) { *w++ = *r; } if( *r == '\n' ) current_line++; } if( !tail ) { /* If tail mode is called with line count set to zero, we will end up in the forward scanning branch. We will drop all output bytes in that case. */ fwrite(buffer, w - buffer, 1, stdout); } size = 0; } else { /* Scanning backward. */ if( size == 0 ) continue; r = e = buffer + size; if( r[-1] == '\n' ) r--; current_line = -1; for(; r-- != buffer;) { if( *r == '\n' ) { current_line--; if( current_line < line ) { current_line++; break; } e = r + 1; } } r++; /* "r" now points at the first byte of the desired line, or at the start of the buffer. "e" points at the end of the desired line. */ if( !tail ) { /* Write prefix in slash mode, since we know none of these bytes would be cut. */ fwrite(buffer, r - buffer, 1, stdout); } /* Move start of desired line and all subsequent bytes to start of buffer. */ memmove(buffer, r, size -= (r - buffer)); /* If we have read everything, we can write the trailing parts out, optionally skipping over the first line if we are in slash mode. */ if( eof ) { if( !tail && current_line == line ) { e -= r - buffer; memmove(buffer, e, size -= (e - buffer)); } fwrite(buffer, size, 1, stdout); } } } return 0; }