source: trunk/sys/dietlibc/fwrite.c @ 249

Last change on this file since 249 was 1, checked in by alain, 7 years ago

First import

File size: 1.6 KB
Line 
1#include "dietstdio.h"
2#include <unistd.h>
3#include <errno.h>
4#include <string.h>
5
6size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE *stream) {
7  ssize_t res;
8  size_t len=size*nmemb;
9  size_t i,done;
10
11  //log_msg("fwrite_unlocked started, streem's fd %d, size %d, nmemb %d\n", stream->fd, size, nmemb);
12  if (!(stream->flags&CANWRITE) || __fflush4(stream,0)) {
13kaputt:
14    //log_msg("fwrite_unlocked errnor at line %d, streem's fd %d, can write ? %d\n", __LINE__, stream->fd,
15    //      stream->flags & CANWRITE);
16    stream->flags|=ERRORINDICATOR;
17    return 0;
18  }
19
20  if (!nmemb || len/nmemb!=size) return 0; /* check for integer overflow */
21 
22  if (len>stream->buflen || (stream->flags&NOBUF)) {
23    if (fflush_unlocked(stream)) return 0;
24    i=2;
25    do {
26      res= write(stream->fd,ptr,len);
27    } while ((res==-1) && (i--));// && errno==EINTR);
28  } else {
29    /* try to make the common case fast */
30    size_t todo=stream->buflen-stream->bm;
31    if (todo>len) todo=len;
32   
33    if (todo) {
34      if (stream->flags&BUFLINEWISE) {
35        for (i=0; i<todo; ++i) {
36          if ((stream->buf[stream->bm++]=((char*)ptr)[i])=='\n') {
37            if (fflush_unlocked(stream)) goto kaputt;
38          }
39        }
40      } else {
41        memcpy(stream->buf+stream->bm,ptr,todo);
42        stream->bm+=todo;
43      }
44      done=todo;
45    } else
46      done=0;
47    for (i=done; i<len; ++i)
48      if (fputc_unlocked(((char*)ptr)[i],stream)) {
49        res=len-i;
50        goto abort;
51      }
52    res=len;
53  }
54  if (res<0) {
55    stream->flags|=ERRORINDICATOR;
56    return 0;
57  }
58abort:
59  return size?res/size:0;
60}
61
62size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) __attribute__((weak,alias("fwrite_unlocked")));
Note: See TracBrowser for help on using the repository browser.