├── README.md └── index.c /README.md: -------------------------------------------------------------------------------- 1 | # qwasar-My-Readline 2 | tqiqlangan usul ishlatilgan e'tibor beringlar 3 | -------------------------------------------------------------------------------- /index.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | int READLINE_READ_SIZE = 512; 8 | 9 | void init_my_readline(){ 10 | READLINE_READ_SIZE = 512; 11 | } 12 | 13 | //function to read a line from a file descriptor 14 | char *my_readline(int fd){ 15 | // uzunlik -- length 16 | char *line = NULL; 17 | // ozgaruvchilarni e`lon qilamiz -- we declare variables 18 | int number_alloc = 0; 19 | int l = 0; 20 | char m = 0; 21 | int readnumber = 0; 22 | // Bu erda biz tekshirish uchun sikl aylantiramiz -- Here we cycle to check 23 | for (l = 0; l < READLINE_READ_SIZE; l++){ 24 | 25 | readnumber = read(fd, &m, 1); 26 | // agar readnumber teng bo`lsa o ga -- if readnumber is equal to o 27 | if (readnumber == 0){ 28 | //siklni to`xtat -- stop the cycle 29 | break; 30 | } 31 | //agar readnumber teng bo`lsa -1 ga -- if readnumber is equal to -1 32 | if (readnumber == -1){ 33 | perror("read"); 34 | exit(EXIT_FAILURE); 35 | } 36 | // if (m == '\n') 37 | // { 38 | // break; 39 | // } 40 | // agar number_alloc teng bo`lsa 0 ga -- if number_alloc is equal to 0 41 | if (number_alloc == 0){ 42 | line = malloc(sizeof(char) * READLINE_READ_SIZE); 43 | //agar line teng bo`lsa NULL ga -- if line is equal to NULL 44 | if (line == NULL){ 45 | perror("malloc"); 46 | exit(EXIT_FAILURE); 47 | } 48 | number_alloc = READLINE_READ_SIZE; 49 | } 50 | 51 | //agar l teng bo`lsa number_alloc dan 1 ni ayirilganiga -- 1 is subtracted from number_alloc if l is equal 52 | if (l == number_alloc - 1){ 53 | //line ni qiymatini o`zgartirish -- change the value of line 54 | line = realloc(line, sizeof(char) * (number_alloc + READLINE_READ_SIZE)); 55 | // agar line teng bo`lsa NULL ga -- if line is equal to NULL 56 | if (line == NULL){ 57 | perror("realloc"); 58 | exit(EXIT_FAILURE); 59 | } 60 | // number_alloc ga READLINE_READ_SIZE ni qo`shish -- Add READLINE_READ_SIZE to number_alloc 61 | number_alloc += READLINE_READ_SIZE; 62 | } 63 | // line ni 64 | line[l] = m; 65 | } 66 | return line; 67 | } 68 | 69 | int main(int ac, char **av) 70 | { 71 | char *str = NULL; 72 | 73 | int fd = open("./README.md", O_RDONLY); 74 | while ((str = my_readline(fd)) != NULL) 75 | { 76 | printf("%s\n", str); 77 | free(str); 78 | } 79 | close(fd); 80 | // 81 | // Yes it's also working with stdin :-) 82 | // printf("%s", my_readline(0)); 83 | // 84 | 85 | return 0; 86 | } --------------------------------------------------------------------------------