Skip to content

Commit 3a60863

Browse files
committed
AVRO-2955: Build avromod and avropipe for Windows.
Import getopt_long() from FreeBSD.
1 parent 91d26dd commit 3a60863

File tree

5 files changed

+775
-7
lines changed

5 files changed

+775
-7
lines changed

lang/c/src/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,18 @@ add_executable(avroappend avroappend.c)
138138
target_link_libraries(avroappend avro-static)
139139
install(TARGETS avroappend RUNTIME DESTINATION bin)
140140

141-
if (NOT WIN32)
142-
#TODO: Port getopt() to Windows to compile avropipe.c and avromod.c
141+
if(MSVC)
142+
add_executable(avropipe avropipe.c getopt/getopt_long.c)
143+
else(MSVC)
143144
add_executable(avropipe avropipe.c)
145+
endif(MSVC)
144146
target_link_libraries(avropipe avro-static)
145147
install(TARGETS avropipe RUNTIME DESTINATION bin)
146148

149+
if(MSVC)
150+
add_executable(avromod avromod.c getopt/getopt_long.c)
151+
else(MSVC)
147152
add_executable(avromod avromod.c)
153+
endif(MSVC)
148154
target_link_libraries(avromod avro-static)
149155
install(TARGETS avromod RUNTIME DESTINATION bin)
150-
endif(NOT WIN32)

lang/c/src/avromod.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
*/
1717

1818
#include <errno.h>
19-
#include <getopt.h>
2019
#include <limits.h>
2120
#include <stdio.h>
2221
#include <stdlib.h>
2322
#include <string.h>
23+
#ifdef _WIN32
24+
#include "getopt/getopt.h"
25+
#else
26+
#include <getopt.h>
27+
#endif
2428

2529
#include "avro.h"
2630
#include "avro_private.h"
@@ -130,6 +134,15 @@ int main(int argc, char **argv)
130134
char *in_filename;
131135
char *out_filename;
132136

137+
#ifdef _MSC_VER
138+
{
139+
const char *cp = argv[0] + strlen(argv[0]);
140+
while (cp > argv[0] && *(cp-1) != '/' && *(cp-1) != '\\' && *(cp-1) != ':')
141+
--cp;
142+
opt_progname = cp;
143+
}
144+
#endif
145+
133146
int ch;
134147
while ((ch = getopt_long(argc, argv, "b:c:", longopts, NULL)) != -1) {
135148
switch (ch) {
@@ -157,7 +170,8 @@ int main(int argc, char **argv)
157170
in_filename = NULL;
158171
out_filename = argv[0];
159172
} else {
160-
fprintf(stderr, "Can't read from multiple input files.\n");
173+
if (argc > 2)
174+
fprintf(stderr, "Can't read from multiple input files.\n");
161175
usage();
162176
exit(1);
163177
}

lang/c/src/avropipe.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717

1818
#include <ctype.h>
1919
#include <errno.h>
20-
#include <getopt.h>
21-
#include <avro/platform.h>
2220
#include <avro/platform.h>
2321
#include <stdio.h>
2422
#include <stdlib.h>
2523
#include <string.h>
24+
#ifdef _WIN32
25+
#include "getopt/getopt.h"
26+
#else
27+
#include <getopt.h>
28+
#endif
2629

2730
#include "avro.h"
2831
#include "avro_private.h"
@@ -400,6 +403,15 @@ int main(int argc, char **argv)
400403
{
401404
char *data_filename;
402405

406+
#ifdef _MSC_VER
407+
{
408+
const char *cp = argv[0] + strlen(argv[0]);
409+
while (cp > argv[0] && *(cp-1) != '/' && *(cp-1) != '\\' && *(cp-1) != ':')
410+
--cp;
411+
opt_progname = cp;
412+
}
413+
#endif
414+
403415
int ch;
404416
while ((ch = getopt_long(argc, argv, "s:", longopts, NULL) ) != -1) {
405417
switch (ch) {

lang/c/src/getopt/getopt.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* $NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $ */
2+
/* $FreeBSD: release/12.2.0/include/getopt.h 326192 2017-11-25 17:09:43Z pfg $ */
3+
4+
/*-
5+
* SPDX-License-Identifier: BSD-2-Clause-NetBSD
6+
*
7+
* Copyright (c) 2000 The NetBSD Foundation, Inc.
8+
* All rights reserved.
9+
*
10+
* This code is derived from software contributed to The NetBSD Foundation
11+
* by Dieter Baron and Thomas Klausner.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
* 1. Redistributions of source code must retain the above copyright
17+
* notice, this list of conditions and the following disclaimer.
18+
* 2. Redistributions in binary form must reproduce the above copyright
19+
* notice, this list of conditions and the following disclaimer in the
20+
* documentation and/or other materials provided with the distribution.
21+
*
22+
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
#ifndef _GETOPT_H_
36+
#define _GETOPT_H_
37+
38+
#ifdef __CYGWIN__
39+
#error Not for cygwin.
40+
#endif
41+
42+
#ifdef unix
43+
#include <sys/cdefs.h>
44+
#endif
45+
46+
/*
47+
* GNU-like getopt_long()/getopt_long_only() with 4.4BSD optreset extension.
48+
* getopt() is declared here too for GNU programs.
49+
*/
50+
#define no_argument 0
51+
#define required_argument 1
52+
#define optional_argument 2
53+
54+
struct option {
55+
/* name of long option */
56+
const char *name;
57+
/*
58+
* one of no_argument, required_argument, and optional_argument:
59+
* whether option takes an argument
60+
*/
61+
int has_arg;
62+
/* if not NULL, set *flag to val when option found */
63+
int *flag;
64+
/* if flag not NULL, value to set *flag to; else return value */
65+
int val;
66+
};
67+
68+
#ifdef __FreeBSD__
69+
__BEGIN_DECLS
70+
#endif
71+
int getopt_long(int, char * const *, const char *,
72+
const struct option *, int *);
73+
int getopt_long_only(int, char * const *, const char *,
74+
const struct option *, int *);
75+
#ifndef _GETOPT_DECLARED
76+
#define _GETOPT_DECLARED
77+
int getopt(int, char * const [], const char *);
78+
79+
extern char *optarg; /* getopt(3) external variables */
80+
extern int optind, opterr, optopt;
81+
#endif
82+
#ifndef _OPTRESET_DECLARED
83+
#define _OPTRESET_DECLARED
84+
extern int optreset; /* getopt(3) external variable */
85+
#endif
86+
#ifdef _MSC_VER
87+
extern const char *opt_progname;
88+
#endif
89+
#ifdef __FreeBSD__
90+
__END_DECLS
91+
#endif
92+
93+
#endif /* !_GETOPT_H_ */

0 commit comments

Comments
 (0)