Skip to content

Commit 046e742

Browse files
committed
Use stdint.h types and inttypes.h formats.
This fixes an issue with formatting timestamps on 32-bit platforms, including the Raspberry Pi, where sizeof(long) = 4. Likely, the code was developed on a platform with sizeof(long) = 8, meaning things worked out by chance. While at it, also calculate the required memory capacity to hold the filename by using snprintf(), which returns the number of bytes that would have been written if the passed buffer was big enough.
1 parent 6a52a23 commit 046e742

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

v4l2grab.c

+9-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
#include <jpeglib.h>
5959
#include <libv4l2.h>
6060
#include <signal.h>
61+
#include <stdint.h>
62+
#include <inttypes.h>
6163

6264
#include "config.h"
6365
#include "yuv.h"
@@ -105,6 +107,8 @@ static char* jpegFilename = NULL;
105107
static char* jpegFilenamePart = NULL;
106108
static char* deviceName = "/dev/video0";
107109

110+
static const char* const continuousFilenameFmt = "%s_%010"PRIu32"_%"PRId64".jpg";
111+
108112
/**
109113
SIGINT interput handler
110114
*/
@@ -220,10 +224,10 @@ static void imageProcess(const void* p, struct timeval timestamp)
220224
YUV420toYUV444(width, height, src, dst);
221225

222226
if(continuous==1) {
223-
static int img_ind = 0;
224-
long timestamp_long;
227+
static uint32_t img_ind = 0;
228+
int64_t timestamp_long;
225229
timestamp_long = timestamp.tv_sec*1e6 + timestamp.tv_usec;
226-
sprintf(jpegFilename,"%s_%010d_%010ld.jpg",jpegFilenamePart,img_ind++,timestamp_long);
230+
sprintf(jpegFilename,continuousFilenameFmt,jpegFilenamePart,img_ind++,timestamp_long);
227231

228232
}
229233
// write jpeg
@@ -963,9 +967,9 @@ int main(int argc, char **argv)
963967
}
964968

965969
if(continuous == 1) {
966-
int max_name_len = strlen(jpegFilename)+10+10+3;
970+
int max_name_len = snprintf(NULL,0,continuousFilenameFmt,jpegFilename,UINT32_MAX,INT64_MAX);
967971
jpegFilenamePart = jpegFilename;
968-
jpegFilename = calloc(max_name_len,sizeof(char));
972+
jpegFilename = calloc(max_name_len+1,sizeof(char));
969973
strcpy(jpegFilename,jpegFilenamePart);
970974
}
971975

0 commit comments

Comments
 (0)