Reputation: 87
I am learning C and trying to make sure my code is portable. For that effect, I build on Macs (ARM, PPC, Intel), Linux (ARM, PPC, PA-RISC) and HP-UX (PA-RISC). To make sure I have an easy way to output simple graphics, I am using GLUT.
I have the following code and functions:
GLfloat white[3] = { 1.0, 1.0, 1.0 };
GLfloat red[3] = { 1.0, 0.0, 0.0 };
GLfloat green[3] = { 0.0, 1.0, 0.0 };
void printText(char *text, const GLfloat colour[3], float posX, float posY) {
glColor3fv (colour);
glRasterPos2f(posX, posY); //define position on the screen
while(*text){
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, *text++);
}
}
void GLprintTextAndInteger (char *text, int value, float colour[3], float posX, float posY) {
int length = snprintf(NULL, 0, "%s %i", text, value);
char *stringToPrint = malloc(length + 1);
snprintf(stringToPrint, length + 1, "%s %i",text,value);
printText(stringToPrint,colour,posX,posY);
free(stringToPrint);
}
void GLprintTextAndLong (char *text, long value, float colour[3], float posX, float posY) {
int length = snprintf(NULL, 0, "%s %ld", text, value);
char *stringToPrint = malloc(length + 1);
snprintf(stringToPrint, length + 1, "%s %ld", text, value);
printText(stringToPrint,colour,posX,posY);
free(stringToPrint);
}
Which I call as follows, for example:
GLprintTextAndInteger("sample text", int whatever, white, -0.98f, 0.1f);
GLprintTextAndLong("sample text", long whatever, white, -0.98f, 0.0f);
printText("some text",white,-0.98f,-0.1f);
When I build on HP-UX, using both HP's compiler and also GCC, when I run the program, only printText works. GLprintTextAndInteger and GLprintTextAndLong do nothing (or maybe they work, but are black and then I can't see the output). The code builds without any warnings in all platforms. It runs perfectly well on Linux and Mac, in all architectures.
Any suggestions?
Edit:
During troubleshooting, I found out that if I replace:
int length = snprintf(NULL, 0, "%s %i", text, value);
with
int length = 40;
it works fine. Why is snprintf failing?
Upvotes: 0
Views: 103
Reputation: 1650
According to the man page for snprintf(3s)
(see https://www.unix.com/man-page/hpux/3s/snprintf/):
By default, returns a negative value if maxsize is smaller than the number of characters formatted. In the UNIX 2003 standards environment (see standards(5)) it returns the the number of bytes that would have been written to buffer s, excluding the terminating null byte, if maxsize had been sufficiently large.
The UNIX 2003 standard is only supported on HP-UX 11iv3 (11.31), if you compile like this:
$ export UNIX_STD=2003
$ make/cc/whatever
Here is a test program I used to verify this:
root@hpvm01:~/t$ cc -o s s.c
root@hpvm01:~/t$ ./s
r=0
root@hpvm01:~/t$ UNIX_STD=2003 cc -o s s.c
root@hpvm01:~/t$ ./s
r=6
root@hpvm01:~/t$ cat s.c
#include <stdio.h>
int main()
{
int r = snprintf(NULL, 0, "hello\n");
printf("r=%d\n", r);
}
So I hope you are using 11.31 and not an earlier version. In that case maybe an open source version of snprintf might help: http://hpux.connect.org.uk/hppd/hpux/Development/Libraries/snprintf-2.2/
Upvotes: 0