blob: d35db940d999d3a5887908e432bdc3133dd373c9 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1c_internal.h"
Lev Walkin79f54952004-08-13 16:58:19 +00002#include "asn1c_compat.h"
Lev Walkinacd9f8b2004-08-19 13:29:03 +00003#include "asn1c_fdeps.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00004#include "asn1c_lang.h"
5#include "asn1c_misc.h"
6#include "asn1c_save.h"
7#include "asn1c_out.h"
Lev Walkinf15320b2004-06-03 03:38:44 +00008
Lev Walkinacd9f8b2004-08-19 13:29:03 +00009static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +000010static int asn1c_print_streams(arg_t *arg);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000011static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +000012static int asn1c_copy_over(arg_t *arg, char *path);
Lev Walkin4604d032005-03-04 08:52:50 +000013static int identical_files(const char *fname1, const char *fname2);
Lev Walkinf15320b2004-06-03 03:38:44 +000014
15int
Lev Walkin866cff12005-03-05 00:50:53 +000016asn1c_save_compiled_output(arg_t *arg, const char *datadir,
17 int argc, char **argv) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000018 asn1c_fdeps_t *deps = 0;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000019 asn1c_fdeps_t *dlist;
Lev Walkinb85a8132005-08-18 13:38:19 +000020 asn1p_module_t *mod;
Lev Walkin866cff12005-03-05 00:50:53 +000021 FILE *mkf;
22 int i;
Lev Walkinf15320b2004-06-03 03:38:44 +000023
Lev Walkinacd9f8b2004-08-19 13:29:03 +000024 deps = asn1c_read_file_dependencies(arg, datadir);
25 if(!deps && datadir) {
26 WARNING("Cannot read file-dependencies information "
27 "from %s\n", datadir);
28 }
Lev Walkinf15320b2004-06-03 03:38:44 +000029
Lev Walkinb85a8132005-08-18 13:38:19 +000030 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
31 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +000032 if(asn1_lang_map[arg->expr->meta_type]
33 [arg->expr->expr_type].type_cb) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000034 if(asn1c_dump_streams(arg, deps))
Lev Walkinf15320b2004-06-03 03:38:44 +000035 return -1;
36 }
37 }
38 }
39
40 /*
41 * Dump out the Makefile template and the rest of the support code.
42 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000043 if((arg->flags & A1C_PRINT_COMPILED)
44 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
45 return 0; /* Finished */
46 }
Lev Walkinf15320b2004-06-03 03:38:44 +000047
Lev Walkin4604d032005-03-04 08:52:50 +000048 mkf = asn1c_open_file("Makefile.am", ".sample", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000049 if(mkf == NULL) {
50 perror("Makefile.am.sample");
51 return -1;
52 }
Lev Walkinf15320b2004-06-03 03:38:44 +000053
Lev Walkinacd9f8b2004-08-19 13:29:03 +000054 fprintf(mkf, "ASN_MODULE_SOURCES=");
Lev Walkinb85a8132005-08-18 13:38:19 +000055 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
56 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000057 if(asn1_lang_map[arg->expr->meta_type]
58 [arg->expr->expr_type].type_cb) {
Lev Walkind29bbce2004-09-23 22:19:14 +000059 fprintf(mkf, "\t\\\n\t%s.c",
Lev Walkinacd9f8b2004-08-19 13:29:03 +000060 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000061 }
62 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000063 }
Lev Walkind29bbce2004-09-23 22:19:14 +000064 fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
Lev Walkinb85a8132005-08-18 13:38:19 +000065 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
66 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkind29bbce2004-09-23 22:19:14 +000067 if(asn1_lang_map[arg->expr->meta_type]
68 [arg->expr->expr_type].type_cb) {
69 fprintf(mkf, "\t\\\n\t%s.h",
70 arg->expr->Identifier);
71 }
72 }
73 }
74 fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000075
Lev Walkinacd9f8b2004-08-19 13:29:03 +000076 /*
77 * Move necessary skeleton files and add them to Makefile.am.sample.
78 */
79 dlist = asn1c_deps_makelist(deps);
80 if(dlist) {
81 char buf[8129];
82 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000083 size_t dlen = strlen(datadir);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000084
Lev Walkin4efbfb72005-02-25 14:20:30 +000085 assert(dlen < (sizeof(buf) / 2 - 2));
86 memcpy(buf, datadir, dlen);
87 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000088 *dir_end++ = '/';
89
90 for(i = 0; i < dlist->el_count; i++) {
91 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000092 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000093
94 assert(strlen(fname) < (sizeof(buf) / 2));
95 strcpy(dir_end, fname);
96
97 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000098 fprintf(mkf, ">>>ABORTED<<<");
99 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000100 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +0000101 }
102 dotH = strrchr(fname, 'h');
103 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) {
104 fprintf(mkf, "ASN_MODULE_HEADERS+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 } else {
Lev Walkind29bbce2004-09-23 22:19:14 +0000106 fprintf(mkf, "ASN_MODULE_SOURCES+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 }
108 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 }
110
Lev Walkind29bbce2004-09-23 22:19:14 +0000111 fprintf(mkf, "\n\n"
112 "lib_LTLIBRARIES=libsomething.la\n"
113 "libsomething_la_SOURCES="
114 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
115 "\n"
116 "# This file may be used as an input for make(3)\n"
117 "# Remove the lines below to convert it into a pure .am file\n"
118 "TARGET = progname\n"
119 "CFLAGS += -I.\n"
120 "OBJS=${ASN_MODULE_SOURCES:.c=.o} $(TARGET).o\n"
121 "\nall: $(TARGET)\n"
122 "\n$(TARGET): ${OBJS}"
123 "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n"
124 "\n.SUFFIXES:"
125 "\n.SUFFIXES: .c .o\n"
126 "\n.c.o:"
127 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
128 "\nclean:"
129 "\n\trm -f $(TARGET)"
130 "\n\trm -f $(OBJS)\n"
Lev Walkin866cff12005-03-05 00:50:53 +0000131 "\nregen: regenerate-from-asn1-source\n"
132 "\nregenerate-from-asn1-source:\n\t"
Lev Walkind29bbce2004-09-23 22:19:14 +0000133 );
Lev Walkin866cff12005-03-05 00:50:53 +0000134
135 for(i = 0; i < argc; i++)
136 fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
137 fprintf(mkf, "\n\n");
138
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000139 fclose(mkf);
140 fprintf(stderr, "Generated Makefile.am.sample\n");
141
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 return 0;
143}
144
145/*
146 * Dump the streams.
147 */
148static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000149asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 if(arg->flags & A1C_PRINT_COMPILED) {
151 return asn1c_print_streams(arg);
152 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000153 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000154 }
155}
156
157static int
158asn1c_print_streams(arg_t *arg) {
159 compiler_streams_t *cs = arg->expr->data;
160 asn1p_expr_t *expr = arg->expr;
161 int i;
162
Lev Walkin64399722004-08-11 07:17:22 +0000163 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000164 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000165 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000166 continue;
167
168 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
169 _compiler_stream2str[i],
170 expr->Identifier);
171
Lev Walkin59004fa2004-08-20 13:37:01 +0000172 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 fwrite(ot->buf, ot->len, 1, stdout);
174 }
175 }
176
177 return 0;
178}
179
180static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000181asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 asn1p_expr_t *expr = arg->expr;
183 compiler_streams_t *cs = expr->data;
184 out_chunk_t *ot;
185 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000186 char *tmpname_c, *tmpname_h;
187 char *name_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000189 const char *c_retained = "";
190 const char *h_retained = "";
Lev Walkinf15320b2004-06-03 03:38:44 +0000191
192 if(cs == NULL) {
193 fprintf(stderr, "Cannot compile %s at line %d\n",
194 expr->Identifier, expr->_lineno);
195 return -1;
196 }
197
Lev Walkin4604d032005-03-04 08:52:50 +0000198 fp_c = asn1c_open_file(expr->Identifier, ".c", &tmpname_c);
199 fp_h = asn1c_open_file(expr->Identifier, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000201 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
202 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000203 return -1;
204 }
205
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000206 fprintf(fp_c,
207 "/*\n"
208 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000209 " * From ASN.1 module \"%s\"\n"
210 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000211 " */\n\n",
Lev Walkinb85a8132005-08-18 13:38:19 +0000212 expr->module->ModuleName,
213 expr->module->source_file_name
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000214 );
215 fprintf(fp_h,
216 "/*\n"
217 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000218 " * From ASN.1 module \"%s\"\n"
219 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000220 " */\n\n",
Lev Walkinb85a8132005-08-18 13:38:19 +0000221 expr->module->ModuleName,
222 expr->module->source_file_name
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000223 );
224
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000225 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000226 fprintf(fp_h,
227 "#ifndef\t_%s_H_\n"
228 "#define\t_%s_H_\n"
229 "\n", header_id, header_id);
230
Lev Walkin3d551c02005-07-15 18:49:41 +0000231 fprintf(fp_h, "\n#include <asn_application.h>\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000232
Lev Walkinb9b8b952005-03-05 00:33:27 +0000233#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000234 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000235 fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000236 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
237 if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000238 fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000239 } \
240} while(0)
241
Lev Walkinb9b8b952005-03-05 00:33:27 +0000242 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
Lev Walkin3d551c02005-07-15 18:49:41 +0000243
244 fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
245
Lev Walkinb9b8b952005-03-05 00:33:27 +0000246 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
247 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
248 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
249 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
250 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
251 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000252
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000253 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
254 "#endif\t/* _%s_H_ */\n",
255 header_id);
256
Lev Walkin0ffabe22004-09-22 16:02:03 +0000257 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkine8318b82005-03-06 09:29:03 +0000258 fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000259 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000260 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000261 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000262 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000263 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
264 fwrite(ot->buf, ot->len, 1, fp_c);
265 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000266 fwrite(ot->buf, ot->len, 1, fp_c);
267
Lev Walkinc8285712005-03-04 22:18:20 +0000268 assert(OT_MAX == 10); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000269
270 fclose(fp_c);
271 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000272
273 name_buf = alloca(strlen(expr->Identifier) + 3);
274
275 sprintf(name_buf, "%s.c", expr->Identifier);
276 if(identical_files(name_buf, tmpname_c)) {
277 c_retained = " (contents unchanged)";
278 unlink(tmpname_c);
279 } else {
280 if(rename(tmpname_c, name_buf)) {
281 unlink(tmpname_c);
282 perror(tmpname_c);
Lev Walkina895afb2005-10-06 10:09:34 +0000283 free(tmpname_c);
284 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000285 return -1;
286 }
287 }
288
289 sprintf(name_buf, "%s.h", expr->Identifier);
290 if(identical_files(name_buf, tmpname_h)) {
291 h_retained = " (contents unchanged)";
292 unlink(tmpname_h);
293 } else {
294 if(rename(tmpname_h, name_buf)) {
295 unlink(tmpname_h);
296 perror(tmpname_h);
Lev Walkina895afb2005-10-06 10:09:34 +0000297 free(tmpname_c);
298 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000299 return -1;
300 }
301 }
302
303 free(tmpname_c);
304 free(tmpname_h);
305
306 fprintf(stderr, "Compiled %s.c%s\n",
307 expr->Identifier, c_retained);
308 fprintf(stderr, "Compiled %s.h%s\n",
309 expr->Identifier, h_retained);
Lev Walkinf15320b2004-06-03 03:38:44 +0000310 return 0;
311}
312
Lev Walkin4604d032005-03-04 08:52:50 +0000313static int
314identical_files(const char *fname1, const char *fname2) {
Lev Walkina895afb2005-10-06 10:09:34 +0000315 char buf[2][4096];
Lev Walkin4604d032005-03-04 08:52:50 +0000316 FILE *fp1, *fp2;
317 size_t olen, nlen;
318 int retval = 1; /* Files are identical */
319
Lev Walkina895afb2005-10-06 10:09:34 +0000320#ifndef WIN32
321 struct stat sb;
322
323 if(lstat(fname1, &sb) || !S_ISREG(sb.st_mode)
324 || lstat(fname2, &sb) || !S_ISREG(sb.st_mode)) {
325 return 0; /* Files are not identical */
326 }
327#endif
328
Lev Walkin4604d032005-03-04 08:52:50 +0000329 fp1 = fopen(fname1, "r");
330 if(!fp1) { return 0; }
331 fp2 = fopen(fname2, "r");
332 if(!fp2) { fclose(fp1); return 0; }
333
334 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
335 nlen = fread(buf[1], 1, olen, fp2);
336 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
337 retval = 0;
338 break;
339 }
340 }
341 nlen = fread(buf[1], 1, 1, fp2);
342 if(nlen) retval = 0;
343
344 fclose(fp1);
345 fclose(fp2);
346 return retval;
347}
348
Lev Walkin4efbfb72005-02-25 14:20:30 +0000349/*
350 * Copy file for real.
351 */
352static int
353real_copy(const char *src, const char *dst) {
Lev Walkina895afb2005-10-06 10:09:34 +0000354 unsigned char buf[4096];
355 char *tmpname;
Lev Walkin4efbfb72005-02-25 14:20:30 +0000356 FILE *fpsrc, *fpdst;
357 size_t len;
358 int retval = 0;
359
Lev Walkin4604d032005-03-04 08:52:50 +0000360 if(identical_files(src, dst))
361 return retval; /* Success, no need to copy for real. */
362
363 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000364 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkina895afb2005-10-06 10:09:34 +0000365 fpdst = asn1c_open_file(dst, "", &tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000366 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
367
368 while(!feof(fpsrc)) {
369 len = fread(buf, 1, sizeof(buf), fpsrc);
Lev Walkinc0e70712005-10-05 09:01:51 +0000370 if(fwrite(buf, 1, len, fpdst) != len) {
Lev Walkina895afb2005-10-06 10:09:34 +0000371 perror(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000372 errno = EIO;
373 retval = -1;
374 break;
375 }
376 }
Lev Walkin4efbfb72005-02-25 14:20:30 +0000377 fclose(fpsrc);
378 fclose(fpdst);
Lev Walkina895afb2005-10-06 10:09:34 +0000379
380 /* Check if copied correctly, and rename into a permanent name */
381 if(retval) {
382 unlink(tmpname);
383 } else if(rename(tmpname, dst)) {
384 unlink(tmpname);
385 perror(tmpname);
386 retval = -1;
387 }
388 free(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000389 return retval;
390}
391
Lev Walkinf15320b2004-06-03 03:38:44 +0000392static int
393asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000394 char *fname;
Lev Walkina895afb2005-10-06 10:09:34 +0000395#ifdef WIN32
396 int use_real_copy = 1;
397#else
398 int use_real_copy = (arg->flags & A1C_SKELETONS_COPY);
399#endif
Lev Walkind9bd7752004-06-05 08:17:50 +0000400
Lev Walkin79f54952004-08-13 16:58:19 +0000401 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000402 if(!fname
Lev Walkina895afb2005-10-06 10:09:34 +0000403 || (use_real_copy ? real_copy(path, fname) : symlink(path, fname))
Lev Walkin4efbfb72005-02-25 14:20:30 +0000404 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000405 if(errno == EEXIST) {
406 struct stat sb1, sb2;
407 if(stat(path, &sb1) == 0
408 && stat(fname, &sb2) == 0
409 && sb1.st_dev == sb2.st_dev
410 && sb1.st_ino == sb2.st_ino) {
411 /*
412 * Nothing to do.
413 */
414 fprintf(stderr,
415 "File %s is already here as %s\n",
416 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000417 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000418 } else {
419 fprintf(stderr,
420 "Retaining local %s (%s suggested)\n",
421 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000422 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000423 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000424 } else if(errno == ENOENT) {
425 /* Ignore this */
426 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000427 } else {
Lev Walkina895afb2005-10-06 10:09:34 +0000428 fprintf(stderr, "%s %s -> %s failed: %s\n",
429 use_real_copy ? "Copy" : "Symlink",
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 path, fname, strerror(errno));
431 return -1;
432 }
433 }
434
Lev Walkina895afb2005-10-06 10:09:34 +0000435 fprintf(stderr, "%s %s\t-> %s\n",
436 use_real_copy ? "Copied" : "Symlinked", path, fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000437
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000438 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000439}
440