forked from bonsai/harakit
		
	tail(1p): tail -n
This commit is contained in:
		
							parent
							
								
									94537bc948
								
							
						
					
					
						commit
						26bbb05777
					
				
							
								
								
									
										69
									
								
								src/tail.c
									
									
									
									
									
								
							
							
						
						
									
										69
									
								
								src/tail.c
									
									
									
									
									
								
							| @ -28,6 +28,75 @@ | ||||
| 
 | ||||
| #include "yac.h" | ||||
| 
 | ||||
| typedef struct string_t { | ||||
|   char *mem; | ||||
|   int len; | ||||
|   int capacity; | ||||
| } string_t; | ||||
| 
 | ||||
| string_t *string_new() { | ||||
|   string_t *string = calloc(1, sizeof(string_t)); | ||||
|   string->capacity = 1024; | ||||
|   string->len = 0; | ||||
|   string->mem = calloc(string->capacity, sizeof(char)); | ||||
|   return string; | ||||
| } | ||||
| 
 | ||||
| void string_putc(string_t *string, char c) { | ||||
|   string->mem[string->len] = c; | ||||
|   string->len++; | ||||
|   if (string->len >= string->capacity) { | ||||
|     string->capacity *= 2; | ||||
|     string->mem = realloc(string->mem, string->capacity * sizeof(char)); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| void tailc(FILE *f, long num) {} | ||||
| 
 | ||||
| void tailn(FILE *f, long num) { | ||||
|   string_t *lines[num]; | ||||
|   int cursor = 0; | ||||
|   int looped = 0; | ||||
| 
 | ||||
|   lines[cursor] = string_new(); | ||||
| 
 | ||||
|   int c = fgetc(f); | ||||
|   for (;;) { | ||||
|     if (c == EOF) { | ||||
|       string_putc(lines[cursor], '\0'); | ||||
|       break; | ||||
|     } | ||||
| 
 | ||||
|     string_putc(lines[cursor], c); | ||||
| 
 | ||||
|     if (c == '\n') { | ||||
|       string_putc(lines[cursor], '\0'); | ||||
| 
 | ||||
|       if ((c = fgetc(f)) == EOF) { | ||||
|         break; | ||||
|       } | ||||
| 
 | ||||
|       if (++cursor >= num) { | ||||
|         looped = 1; | ||||
|         cursor = 0; | ||||
|       } | ||||
| 
 | ||||
|       lines[cursor] = string_new(); | ||||
|     } else { | ||||
|       c = fgetc(f); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   int read = looped ? cursor + 1 : 0; | ||||
|   do { | ||||
|     if (read >= num) { | ||||
|       read = 0; | ||||
|     } | ||||
| 
 | ||||
|     fputs(lines[read]->mem, stdout); | ||||
|   } while (read++ != cursor); | ||||
| } | ||||
| 
 | ||||
| void tailf(FILE *file) { | ||||
| 	int byte; | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user