Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Author-XAgent Smith*/
- /*Date:Step 25,2018*/
- #include <stdio.h>
- #include <stdlib.h>
- /*Start of getln fuction*/
- void *reallo_c(char ** c,size_t size,size_t len)
- {
- char *k=NULL;
- short i=0;
- k=(char*)malloc(size*sizeof(char));
- while((( 0<=(len--)&&*((*c)+i)!='\0')?*(k+i++)=*(*(c)+i) : 0));
- free(*c);
- return k ;
- }
- char *getln(void)
- {
- //Initializing variables and pointers
- const size_t sizeIncrement=5;
- char *buffer=(char*)malloc(5*sizeof(char));
- char *currentposition=buffer;
- size_t max_length=sizeIncrement;
- size_t length=0;
- static size_t L=0;
- char character;
- /*getting inputes*/
- while(1)
- {
- character=fgetc(stdin);//getting character from stdin file
- if(character=='\n') break;
- if(length>=max_length-1)
- {
- /*Allocating memory for next increment extra chararcter*/
- char *newbuffer=reallo_c(&buffer,(max_length+sizeIncrement),L-1);
- max_length+=sizeIncrement;
- if(newbuffer==NULL)
- {
- free(buffer);
- return NULL;
- }
- currentposition=newbuffer+L;
- buffer=newbuffer;
- }
- *(currentposition++)=character;
- L++;
- length++;
- }
- *currentposition='\0';
- return buffer;
- /*End of getln function call*/
- }
- /*Start of main*/
- int main()
- {
- //calling getln();
- printf("%s\n",getln());
- return 0;
- /*End of main*/
- }
Add Comment
Please, Sign In to add comment