Skip to content

Commit 579d1fb

Browse files
Ramsay Allan JonesJunio C Hamano
authored andcommitted
Add NO_C99_FORMAT to support older compilers.
The NO_C99_FORMAT macro allows compilers that lack support for the ll,hh,j,z,t size specifiers (eg. gcc 2.95.2) to adapt the code to avoid runtime errors in the formatted IO functions. Signed-off-by: Ramsay Allan Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 446c6fa commit 579d1fb

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ all:
2424
# Define NO_D_TYPE_IN_DIRENT if your platform defines DT_UNKNOWN but lacks
2525
# d_type in struct dirent (latest Cygwin -- will be fixed soonish).
2626
#
27+
# Define NO_C99_FORMAT if your formatted IO functions (printf/scanf et.al.)
28+
# do not support the 'size specifiers' introduced by C99, namely ll, hh,
29+
# j, z, t. (representing long long int, char, intmax_t, size_t, ptrdiff_t).
30+
# some c compilers supported these specifiers prior to C99 as an extension.
31+
#
2732
# Define NO_STRCASESTR if you don't have strcasestr.
2833
#
2934
# Define NO_STRLCPY if you don't have strlcpy.
@@ -432,6 +437,9 @@ endif
432437
ifdef NO_D_INO_IN_DIRENT
433438
ALL_CFLAGS += -DNO_D_INO_IN_DIRENT
434439
endif
440+
ifdef NO_C99_FORMAT
441+
ALL_CFLAGS += -DNO_C99_FORMAT
442+
endif
435443
ifdef NO_SYMLINK_HEAD
436444
ALL_CFLAGS += -DNO_SYMLINK_HEAD
437445
endif

alloc.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,21 @@ DEFINE_ALLOCATOR(tree)
3939
DEFINE_ALLOCATOR(commit)
4040
DEFINE_ALLOCATOR(tag)
4141

42+
#ifdef NO_C99_FORMAT
43+
#define SZ_FMT "%u"
44+
#else
45+
#define SZ_FMT "%zu"
46+
#endif
47+
48+
static void report(const char* name, unsigned int count, size_t size)
49+
{
50+
fprintf(stderr, "%10s: %8u (" SZ_FMT " kB)\n", name, count, size);
51+
}
52+
53+
#undef SZ_FMT
54+
4255
#define REPORT(name) \
43-
fprintf(stderr, "%10s: %8u (%zu kB)\n", #name, name##_allocs, name##_allocs*sizeof(struct name) >> 10)
56+
report(#name, name##_allocs, name##_allocs*sizeof(struct name) >> 10)
4457

4558
void alloc_report(void)
4659
{

mktag.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ static int verify_object(unsigned char *sha1, const char *expected_type)
3939
return ret;
4040
}
4141

42+
#ifdef NO_C99_FORMAT
43+
#define PD_FMT "%d"
44+
#else
45+
#define PD_FMT "%td"
46+
#endif
47+
4248
static int verify_tag(char *buffer, unsigned long size)
4349
{
4450
int typelen;
@@ -67,15 +73,15 @@ static int verify_tag(char *buffer, unsigned long size)
6773
/* Verify tag-line */
6874
tag_line = strchr(type_line, '\n');
6975
if (!tag_line)
70-
return error("char%td: could not find next \"\\n\"", type_line - buffer);
76+
return error("char" PD_FMT ": could not find next \"\\n\"", type_line - buffer);
7177
tag_line++;
7278
if (memcmp(tag_line, "tag ", 4) || tag_line[4] == '\n')
73-
return error("char%td: no \"tag \" found", tag_line - buffer);
79+
return error("char" PD_FMT ": no \"tag \" found", tag_line - buffer);
7480

7581
/* Get the actual type */
7682
typelen = tag_line - type_line - strlen("type \n");
7783
if (typelen >= sizeof(type))
78-
return error("char%td: type too long", type_line+5 - buffer);
84+
return error("char" PD_FMT ": type too long", type_line+5 - buffer);
7985

8086
memcpy(type, type_line+5, typelen);
8187
type[typelen] = 0;
@@ -92,14 +98,14 @@ static int verify_tag(char *buffer, unsigned long size)
9298
break;
9399
if (c > ' ')
94100
continue;
95-
return error("char%td: could not verify tag name", tag_line - buffer);
101+
return error("char" PD_FMT ": could not verify tag name", tag_line - buffer);
96102
}
97103

98104
/* Verify the tagger line */
99105
tagger_line = tag_line;
100106

101107
if (memcmp(tagger_line, "tagger", 6) || (tagger_line[6] == '\n'))
102-
return error("char%td: could not find \"tagger\"", tagger_line - buffer);
108+
return error("char" PD_FMT ": could not find \"tagger\"", tagger_line - buffer);
103109

104110
/* TODO: check for committer info + blank line? */
105111
/* Also, the minimum length is probably + "tagger .", or 63+8=71 */
@@ -108,6 +114,8 @@ static int verify_tag(char *buffer, unsigned long size)
108114
return 0;
109115
}
110116

117+
#undef PD_FMT
118+
111119
int main(int argc, char **argv)
112120
{
113121
unsigned long size = 4096;

0 commit comments

Comments
 (0)