static void write_resolv_conf_search( OrderedSet *domains, FILE *f) { unsigned length = 0, count = 0; Iterator i; char *domain; assert(domains); assert(f); fputs_unlocked("search", f); ORDERED_SET_FOREACH(domain, domains, i) { if (++count > MAXDNSRCH) { fputs_unlocked("\n# Too many search domains configured, remaining ones ignored.", f); break; } length += strlen(domain) + 1; if (length > 256) { fputs_unlocked("\n# Total length of all search domains is too long, remaining ones ignored.", f); break; } fputc_unlocked(' ', f); fputs_unlocked(domain, f); } fputs_unlocked("\n", f); }
static int write_block(FILE *outfp, const char *buffer, size_t length, int compressed, unsigned int crc32c) { /* write compressed flag */ fputc_unlocked(compressed ? COMPRESSED_FLAG : UNCOMPRESSED_FLAG, outfp); trace("write 1 byte for compressed flag.\n"); /* write data length. */ fputc_unlocked((length >> 8), outfp); fputc_unlocked((length >> 0), outfp); trace("write 2 bytes for data length.\n"); /* write crc32c */ fputc_unlocked((crc32c >> 24), outfp); fputc_unlocked((crc32c >> 16), outfp); fputc_unlocked((crc32c >> 8), outfp); fputc_unlocked((crc32c >> 0), outfp); trace("write 4 bytes for crc32c.\n"); if (fwrite(buffer, length, 1, outfp) != 1) { print_error("Failed to write a file: %s\n", strerror(errno)); return -1; } trace("write %ld bytes for data.\n", (long)length); return 0; }
static int thread_callback(Dwfl_Thread *thread, void *userdata) { struct stack_context *c = userdata; pid_t tid; assert(thread); assert(c); if (c->n_thread >= THREADS_MAX) return DWARF_CB_ABORT; if (c->n_thread != 0) fputc_unlocked('\n', c->f); c->n_frame = 0; tid = dwfl_thread_tid(thread); fprintf(c->f, "Stack trace of thread " PID_FMT ":\n", tid); if (dwfl_thread_getframes(thread, frame_callback, c) < 0) return DWARF_CB_ABORT; c->n_thread++; return DWARF_CB_OK; }
void printescape(unsigned char *s, unsigned int l) { for (unsigned int i = 0; i < l; i++) { switch (s[i]) { case '<': fputs_unlocked("<", stdout); continue; case '>': fputs_unlocked(">", stdout); continue; case '&': fputs_unlocked("&", stdout); continue; case '\'': fputs_unlocked("'", stdout); continue; case '"': fputs_unlocked(""", stdout); continue; case '\r': fputs_unlocked(" ", stdout); continue; case '\n': fputs_unlocked(" ", stdout); continue; case '\t': fputs_unlocked("	", stdout); continue; default: fputc_unlocked(s[i], stdout); } } }
static void write_auth_hex (const char *field, const unsigned char *src, size_t len) { static const char hex[] = "0123456789abcdef"; size_t i; fprintf (authf, ", \"%s\": \"", field); for (i = 0; i < len; i++) { unsigned char byte = src[i]; fputc_unlocked (hex[byte >> 4], authf); fputc_unlocked (hex[byte & 0xf], authf); } fputc_unlocked ('\"', authf); }
static inline void fast_write_longint(long long int x) { // speed x2 static short int digits[32]; if (x == 0) { fputc_unlocked('0', fw); return; } if (x < 0) { fputc_unlocked('-', fw); x = -x; } short int i = -1; while (x) { i++; digits[i] = x%(10ll); x /= 10ll; } for (; i >= 0; i--) fputc_unlocked('0' + digits[i], fw); }
void vdebug_log (const char *filename, int line, const char *func, const char *format, va_list ap) { static FILE *fp = fopen ("/tmp/tiary_debug.txt", "w"); if (fp) { fprintf_unlocked (fp, "%s:%d:%s:", filename, line, func); vfprintf_unlocked (fp, format, ap); fputc_unlocked ('\n', fp); fflush_unlocked (fp); } }
void FileStream::writeByte_slow(byte b) { assert(m_fp); #ifdef __USE_MISC if (EOF == fputc_unlocked(b, m_fp)) #else if (EOF == fputc(b, m_fp)) #endif throw OutOfSpaceException(BOOST_CURRENT_FUNCTION); }
static int process_block (struct convtable *tbl, char *addr, size_t len, FILE *output) { size_t n = 0; while (n < len) { struct convtable *cur = tbl; unsigned char *curp = (unsigned char *) addr; unsigned int byte = *curp; int cnt; struct charseq *out; while (! is_term (cur, byte)) if (cur->val[byte].sub == NULL) { /* This is an invalid sequence. Skip the first byte if we are ignoring errors. Otherwise punt. */ if (! omit_invalid) { error (0, 0, _("illegal input sequence at position %Zd"), n); return -1; } n -= curp - (unsigned char *) addr; byte = *(curp = (unsigned char *) ++addr); if (++n >= len) /* All converted. */ return 0; cur = tbl; } else { cur = cur->val[byte].sub; if (++n >= len) { error (0, 0, _("\ incomplete character or shift sequence at end of buffer")); return -1; } byte = *++curp; } /* We found a final byte. Write the output bytes. */ out = cur->val[byte].out; for (cnt = 0; cnt < out->nbytes; ++cnt) fputc_unlocked (out->bytes[cnt], output); addr = (char *) curp + 1; ++n; }
static void write_env_var(FILE *f, const char *v) { const char *p; p = strchr(v, '='); if (!p) { /* Fallback */ fputs_unlocked(v, f); fputc_unlocked('\n', f); return; } p++; fwrite_unlocked(v, 1, p-v, f); if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) { fputc_unlocked('\"', f); for (; *p; p++) { if (strchr(SHELL_NEED_ESCAPE, *p)) fputc_unlocked('\\', f); fputc_unlocked(*p, f); } fputc_unlocked('\"', f); } else fputs_unlocked(p, f); fputc_unlocked('\n', f); }
static void write_auth_string (const char *field, const char *str) { const unsigned char *at; char buf[8]; fprintf (authf, ", \"%s\": \"", field); for (at = (const unsigned char *)str; *at; at++) { if (*at == '\\' || *at == '\"' || *at < 0x1f) { snprintf (buf, sizeof (buf), "\\u%04x", (int)*at); fputs_unlocked (buf, authf); } else { fputc_unlocked (*at, authf); } } fputc_unlocked ('\"', authf); }
int _find_steals(Node * ptr, char * hist, char* limit, char level, FILE * out_file){ int i; if(level == 26){ for(i = 0; i < ptr->len; i++){ fputs_unlocked((char *)ptr->children[i], out_file); fputc_unlocked('\n', out_file); } return 0; } for (i = hist[level]; i < ptr->len && !(limit && i > hist[level] + limit[level]); i++) if(ptr->children[i]) _find_steals(ptr->children[i], hist, limit, level + 1, out_file); return 0; }
/* Write an entry to the given stream. This must know the format of the group file. */ int putgrent (const struct group *gr, FILE *stream) { int retval; if (__glibc_unlikely (gr == NULL) || __glibc_unlikely (stream == NULL) || gr->gr_name == NULL || !__nss_valid_field (gr->gr_name) || !__nss_valid_field (gr->gr_passwd) || !__nss_valid_list_field (gr->gr_mem)) { __set_errno (EINVAL); return -1; } flockfile (stream); if (gr->gr_name[0] == '+' || gr->gr_name[0] == '-') retval = fprintf (stream, "%s:%s::", gr->gr_name, _S (gr->gr_passwd)); else retval = fprintf (stream, "%s:%s:%lu:", gr->gr_name, _S (gr->gr_passwd), (unsigned long int) gr->gr_gid); if (__builtin_expect (retval, 0) < 0) { funlockfile (stream); return -1; } if (gr->gr_mem != NULL) { for (size_t i = 0; gr->gr_mem[i] != NULL; i++) if (fprintf (stream, i == 0 ? "%s" : ",%s", gr->gr_mem[i]) < 0) { /* What else can we do? */ funlockfile (stream); return -1; } } retval = fputc_unlocked ('\n', stream); funlockfile (stream); return retval < 0 ? -1 : 0; }
void repeat(const char *alu, const char *title, int n) { int len = strlen(alu); char buffer[len + LINE_LEN]; int pos = 0; memcpy(buffer, alu, len); memcpy(buffer + len, alu, LINE_LEN); fputs_unlocked(title, stdout); while (n > 0) { int bytes = n > LINE_LEN ? LINE_LEN : n; fwrite_unlocked(buffer + pos, bytes, 1, stdout); pos += bytes; if (pos > len) { pos -= len; } fputc_unlocked('\n', stdout); n -= bytes; } }
int print_qr_code( FILE *output, const void *seed, size_t seed_size, uint64_t start, uint64_t interval, const char *hn, sd_id128_t machine) { FILE *f; char *url = NULL; size_t url_size = 0, i; QRcode* qr; unsigned x, y; assert(seed); assert(seed_size > 0); f = open_memstream(&url, &url_size); if (!f) return -ENOMEM; fputs_unlocked("fss://", f); for (i = 0; i < seed_size; i++) { if (i > 0 && i % 3 == 0) fputc_unlocked('-', f); fprintf(f, "%02x", ((uint8_t*) seed)[i]); } fprintf(f, "/%"PRIx64"-%"PRIx64"?machine=" SD_ID128_FORMAT_STR, start, interval, SD_ID128_FORMAT_VAL(machine)); if (hn) fprintf(f, ";hostname=%s", hn); if (ferror(f)) { fclose(f); free(url); return -ENOMEM; } fclose(f); qr = QRcode_encodeString(url, 0, QR_ECLEVEL_L, QR_MODE_8, 1); free(url); if (!qr) return -ENOMEM; print_border(output, qr->width); for (y = 0; y < (unsigned) qr->width; y += 2) { const uint8_t *row1, *row2; row1 = qr->data + qr->width * y; row2 = row1 + qr->width; fputs(WHITE_ON_BLACK, output); for (x = 0; x < 4; x++) fputs("\342\226\210", output); for (x = 0; x < (unsigned) qr->width; x ++) { bool a, b; a = row1[x] & 1; b = (y+1) < (unsigned) qr->width ? (row2[x] & 1) : false; if (a && b) fputc(' ', output); else if (a) fputs("\342\226\204", output); else if (b) fputs("\342\226\200", output); else fputs("\342\226\210", output); } for (x = 0; x < 4; x++) fputs("\342\226\210", output); fputs(NORMAL "\n", output); } print_border(output, qr->width); QRcode_free(qr); return 0; }
int __fputc_unlocked(int c, FILE *f) { return fputc_unlocked(c, f); }
size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE *stream) { ssize_t res; size_t len=size*nmemb; size_t i,done; if (!__likely(stream->flags&CANWRITE) || __fflush4(stream,0)) { kaputt: stream->flags|=ERRORINDICATOR; return 0; } if (!nmemb || len/nmemb!=size) return 0; /* check for integer overflow */ if (__unlikely(len>stream->buflen || (stream->flags&NOBUF))) { if (fflush_unlocked(stream)) return 0; do { res=__libc_write(stream->fd,ptr,len); } while (res==-1 && errno==EINTR); } else again: { /* try to make the common case fast */ size_t todo=stream->buflen-stream->bm; if (todo>len) todo=len; if (todo) { if (stream->flags&BUFLINEWISE) { if (__unlikely((stream->flags&CHECKLINEWISE)!=0)) { stream->flags&=~CHECKLINEWISE; /* stdout is set to BUFLINEWISE|CHECKLINEWISE by default. */ /* that means we should check whether it is connected to a * tty on first flush, and if not so, reset BUFLINEWISE */ if (!isatty(stream->fd)) { stream->flags&=~BUFLINEWISE; goto notlinewise; } } for (i=0; i<todo; ++i) { if ((stream->buf[stream->bm++]=((char*)ptr)[i])=='\n') { if (fflush_unlocked(stream)) goto kaputt; } } } else { notlinewise: memcpy(stream->buf+stream->bm,ptr,todo); stream->bm+=todo; if (stream->bm==stream->buflen) { if (fflush_unlocked(stream)) return 0; /* if we are here, we should not have an empty buffer */ len-=todo; if (!len) return nmemb; ptr+=todo; goto again; } else return nmemb; } done=todo; } else done=0; for (i=done; i<len; ++i) if (fputc_unlocked(((char*)ptr)[i],stream) == EOF) { res=len-i; goto abort; } res=len; } if (res<0) { stream->flags|=ERRORINDICATOR; return 0; } abort: return size?res/size:0; }
static inline void fast_write_char(char c) { fputc_unlocked(c, fw); }