comparison src/dmres.c @ 1282:086f49d616ac

Improve fgets() implementation in dmres.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 18 Aug 2017 02:15:51 +0300
parents 1b1e7c771698
children 93d1050eac99
comparison
equal deleted inserted replaced
1281:767b6ad59599 1282:086f49d616ac
951 { 951 {
952 f->atime = time(NULL); 952 f->atime = time(NULL);
953 return f->fops->fwrite(ptr, size, nmemb, f); 953 return f->fops->fwrite(ptr, size, nmemb, f);
954 } 954 }
955 955
956 char *dmfgets(char *s, int size, DMResource * f) 956 char *dmfgets(char *str, int size, DMResource * f)
957 { 957 {
958 char *p = s, c; 958 char *ptr = str, *end = str + size - 1;
959 int n = 0; 959 int c;
960 960
961 while ((c = f->fops->fgetc(f)) != EOF) 961 if (size <= 0)
962 { 962 return NULL;
963 n++; 963
964 while (ptr < end && (c = f->fops->fgetc(f)) != EOF)
965 {
966 *ptr++ = c;
964 if (c == '\n') 967 if (c == '\n')
965 break; 968 break;
966 else 969 }
967 if (n < size - 1) 970 *ptr = 0;
968 *p++ = c; 971
969 } 972 return (ptr > str) ? str : NULL;
970 *p = 0;
971
972 return (n > 0) ? s : NULL;
973 } 973 }
974 974
975 975
976 int dmResourceRef(DMResource *node) 976 int dmResourceRef(DMResource *node)
977 { 977 {