Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7836729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:17:00+00:00 2026-06-02T14:17:00+00:00

I’m currently writing a linux program that produces colored output on the terminal. Since

  • 0

I’m currently writing a linux program that produces colored output on the terminal.
Since the program stdout could redirected into a textfile, or generally to a non-terminal sink, and the methods should stay as general-purpose as possible, I need to call isatty(int fd) to determine if I should send the ASCII color escape codes.

Since I’m unsure about the performance impact of calling isatty() before each call to printf(), I’ve implemented a buffer that buffers isatty() results for the first 16 fds:

#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>

#define TERM_NORMAL     "\x1b\x5bm"
#define TERM_BRIGHT     "\x1b\x5b\x31m"
#define TERM_BOLD       "\x1b\x5b\x31m"
#define TERM_BLINK      "\x1b\x5b\x35m"
#define TERM_RED        "\x1b\x5b\x33\x31m"

//to prevent unnecessary isatty() calls, provide this lookup table
//for the first 16 fds
//0 means that it has not been checked yet
//1 means the fd is not a tty
//2 means the fd is a tty 
char isattybuf[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
inline bool isattybuffered(int fd) {
        if(fd >= 0 && fd < sizeof(isattybuf)) {
                if(!isattybuf[fd])
                        isattybuf[fd] = isatty(fd) + 1;
                return isattybuf[16] - 1;
        } else {
                return isatty(fd);
        }
}

#define colprintf(col, format, ...)                                     \
        if(isattybuffered(fileno(stdout)))                              \
                printf(col format TERM_NORMAL, ## __VA_ARGS__);         \
        else                                                            \
                printf(format, ## __VA_ARGS__);

#define colfprintf(col, f, format, ...)                                 \
        if(isattybuffered(fileno(f)))                                   \
                fprintf(f, col format TERM_NORMAL, ## __VA_ARGS__);     \
        else                                                            \
                fprintf(f, format, ## __VA_ARGS__);

//for testing
int main() {
        colprintf(TERM_BRIGHT TERM_BLINK, "test1\n");
        colprintf(TERM_RED TERM_BRIGHT,   "test2\n");
}

But this has a few downsides as well:

  • Since it will be part of a library header file, there will be a buffer array for every single c file that includes the header file
  • Correspondingly, isatty() might be called n times, where n is the number of c files using the code
  • The buffer information might be wrong if a file is opened, isatty() is called, the file is then closed, and then a tty is opened with the same fd

A alternative solution that will eliminate the first two problems would be to put the buffer variable into a separate c file using the extern keyword, but will this work even if the code is compiled as a shared library object and used by multiple programs simultanously?

Unfortunately, the ISATTY(3) manpage does not provide any hints about the method’s performance impacts.

UPDATE
I just ran some benchmarks, and it seems isatty() does one ioctl syscall every time it is called, taking about 700ns or 500 clock cycles on my x86_64 ARCH system. A write() syscall (as invoked by printf) takes about the same amount of time, so if isatty() is not buffered, i lose less 1µs or about half of the performance per output operation (which seems neglegible as compared to the time required for the terminal scrolling, but can become important when redirecting the output into a large textfile). Especially when continually calling printf(), write syscalls are only invoked every 4096 bytes, so the code could spend a large portion of it’s time waiting for the results of isatty(), so buffering seems to make sense after all.

So I’d still like to hear your opinions on my attempt of buffering, and the problems i mentioned.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:17:02+00:00Added an answer on June 2, 2026 at 2:17 pm

    A quick benchmark showed that at least on Darwin, isatty isn’t cached and it does an ioctl every time.
    10 000 checks of file descriptors 0 – 99 took just 0.4 seconds on 2.8GHz i7 (mac). I would say that calling printf costs far more than calling isatty.

    Anyway, I would use a function pointer. At the start I would call one isatty and map a pointer to function (printf without ascii / printf with ascii) and then use that pointer.

    Martin

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.