Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dprintf() polyfill for Illumos #1911

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/nnn.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@
#define alloca(size) __builtin_alloca(size)
#endif

#ifdef __sun
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Illumos is maintained, why don't they add dprintf? It's more economical for them to add it instead of N projects to carry their own implementation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better if they did indeed. But that does leave legacy systems, and even there nnn can be very useful.

#define NEED_DPRINTF
#endif

#include "nnn.h"
#include "dbg.h"

Expand Down Expand Up @@ -856,6 +860,27 @@ static void notify_fifo(bool force);

/* Functions */

#ifdef NEED_DPRINTF
static int dprintf(int fd, const char *format, ...)
{
va_list ap;
char *s;
int len, nwritten;

va_start(ap, format);
len = vasprintf(&s, format, ap);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although posix doesn't require it, the general expectation is for dprintf to not do any memory allocation so that it can be used in signal handler context. Not doing allocation would also remove allocation related failure point.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it would be better but I couldn't find a better alternative with the available primitives. One could use a static buffer but then you're length limited.

va_end(ap);

if (len == -1)
return -1;

nwritten = write(fd, s, len);
free(s);

return nwritten;
}
#endif

static void sigint_handler(int sig)
{
(void) sig;
Expand Down
Loading