Home | History | Annotate | Download | only in bionic-fixup
      1 /*
      2  * Copyright 2012, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <stdio.h>
     18 
     19 ssize_t getline(char **lineptr, size_t *n, FILE *stream)
     20 {
     21     char *ptr;
     22 
     23     ptr = fgetln(stream, n);
     24 
     25     if (ptr == NULL) {
     26         return -1;
     27     }
     28 
     29     /* Free the original ptr */
     30     if (*lineptr != NULL) free(*lineptr);
     31 
     32     /* Add one more space for '\0' */
     33     size_t len = n[0] + 1;
     34 
     35     /* Update the length */
     36     n[0] = len;
     37 
     38     /* Allocate a new buffer */
     39     *lineptr = malloc(len);
     40 
     41     /* Copy over the string */
     42     memcpy(*lineptr, ptr, len-1);
     43 
     44     /* Write the NULL character */
     45     (*lineptr)[len-1] = '\0';
     46 
     47     /* Return the length of the new buffer */
     48     return len;
     49 }
     50