int fputc(int c, FAR FILE *stream) { unsigned char buf = (unsigned char)c; int ret; ret = lib_fwrite(&buf, 1, stream); if (ret > 0) { /* Flush the buffer if a newline is output */ #ifdef CONFIG_STDIO_LINEBUFFER if (c == '\n') { ret = lib_fflush(stream, true); if (ret < 0) { return EOF; } } #endif return c; } else { return EOF; } }
int fputs(FAR const char *s, FAR FILE *stream) { int ntowrite; int nput; /* Make sure that a string was provided. */ #ifdef CONFIG_DEBUG /* Most parameter checking is disabled if DEBUG is off */ if (!s) { set_errno(EINVAL); return EOF; } #endif /* Get the length of the string. */ ntowrite = strlen(s); if (ntowrite == 0) { return 0; } /* Write the string */ nput = lib_fwrite(s, ntowrite, stream); if (nput < 0) { return EOF; } return nput; }
size_t fwrite(FAR const void *ptr, size_t size, size_t n_items, FAR FILE *stream) { size_t full_size = n_items * (size_t)size; ssize_t bytes_written; size_t items_written = 0; /* Write the data into the stream buffer */ bytes_written = lib_fwrite(ptr, full_size, stream); if (bytes_written > 0) { /* Return the number of full items written */ items_written = bytes_written / size; } return items_written; }
int fputs(FAR const char *s, FAR FILE *stream) { int nput; int ret; char ch; /* Make sure that a string was provided. */ #ifdef CONFIG_DEBUG /* Most parameter checking is disabled if DEBUG is off */ if (!s) { set_errno(EINVAL); return EOF; } #endif /* Write the string. Loop until the null terminator is encountered */ for (nput = 0, ch = up_romgetc(s); ch; nput++, s++, ch = up_romgetc(s)) { /* Write the next character to the stream buffer */ ret = lib_fwrite(&ch, 1, stream); if (ret <= 0) { return EOF; } /* Flush the buffer if a newline was written to the buffer */ #ifdef CONFIG_STDIO_LINEBUFFER if (ch == '\n') { ret = lib_fflush(stream, true); if (ret < 0) { return EOF; } } #endif } return nput; }
int puts(FAR const char *s) { FILE *stream = stdout; int nwritten; int nput = EOF; int ret; /* Write the string (the next two steps must be atomic) */ lib_take_semaphore(stream); /* Write the string without its trailing '\0' */ nwritten = fputs(s, stream); if (nwritten > 0) { /* Followed by a newline */ char newline = '\n'; ret = lib_fwrite(&newline, 1, stream); if (ret > 0) { nput = nwritten + 1; /* Flush the buffer after the newline is output if line buffering * is enabled. */ if ((stream->fs_flags & __FS_FLAG_LBF) != 0) { ret = lib_fflush(stream, true); if (ret < 0) { nput = EOF; } } } } lib_give_semaphore(stdout); return nput; }
int puts(FAR const char *s) { FILE *stream = stdout; int nwritten; int nput = EOF; int ret; /* Write the string (the next two steps must be atomic) */ lib_take_semaphore(stream); /* Write the string without its trailing '\0' */ nwritten = fputs(s, stream); if (nwritten > 0) { /* Followed by a newline */ char newline = '\n'; ret = lib_fwrite(&newline, 1, stream); if (ret > 0) { nput = nwritten + 1; /* Flush the buffer after the newline is output. */ #ifdef CONFIG_STDIO_LINEBUFFER ret = lib_fflush(stream, true); if (ret < 0) { nput = EOF; } #endif } } lib_give_semaphore(stdout); return nput; }