aboutsummaryrefslogtreecommitdiffstats
path: root/src/io.c
blob: d596aab8b7629ff21c5f1e0fb3f8d1396665de87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
/*
 * Calcurse - text-based organizer
 *
 * Copyright (c) 2004-2023 calcurse Development Team <misc@calcurse.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the
 *        following disclaimer in the documentation and/or other
 *        materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Send your feedback or comments to : misc@calcurse.org
 * Calcurse home page : http://calcurse.org
 *
 */

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
#include <errno.h>

#include "calcurse.h"
#include "sha1.h"

struct ht_keybindings_s {
	const char *label;
	enum vkey key;
	HTABLE_ENTRY(ht_keybindings_s);
};

static void load_keys_ht_getkey(struct ht_keybindings_s *, const char **,
				int *);
static int load_keys_ht_compare(struct ht_keybindings_s *,
				struct ht_keybindings_s *);

#define HSIZE 256
HTABLE_HEAD(ht_keybindings, HSIZE, ht_keybindings_s);
HTABLE_PROTOTYPE(ht_keybindings, ht_keybindings_s)
    HTABLE_GENERATE(ht_keybindings, ht_keybindings_s, load_keys_ht_getkey,
		load_keys_ht_compare)

static int modified = 0;
static char apts_sha1[SHA1_DIGESTLEN * 2 + 1];
static char todo_sha1[SHA1_DIGESTLEN * 2 + 1];

/* Ask user for a file name to export data to. */
static FILE *get_export_stream(enum export_type type)
{
	FILE *stream;
	char *home, *stream_name;
	const char *question =
	    _("Choose the file used to export calcurse data:");
	const char *wrong_name =
	    _("The file cannot be accessed, please enter another file name.");
	const char *press_enter = _("Press [ENTER] to continue.");
	const char *file_ext[IO_EXPORT_NBTYPES] = { "ical", "txt" };

	stream = NULL;
	if ((home = getenv("HOME")) != NULL)
		asprintf(&stream_name, "%s/calcurse.%s", home, file_ext[type]);
	else
		asprintf(&stream_name, "%s/calcurse.%s", get_tempdir(),
			 file_ext[type]);

	while (stream == NULL) {
		status_mesg(question, "");
		if (updatestring(win[STA].p, &stream_name, 0, 1)) {
			mem_free(stream_name);
			return NULL;
		}
		stream = fopen(stream_name, "w");
		if (stream == NULL) {
			status_mesg(wrong_name, press_enter);
			keys_wait_for_any_key(win[KEY].p);
		}
	}

	mem_free(stream_name);
	return stream;
}

/* Append a line to a file. */
unsigned io_fprintln(const char *fname, const char *fmt, ...)
{
	FILE *fp;
	va_list ap;
	char *buf;
	int ret;

	fp = fopen(fname, "a");
	RETVAL_IF(!fp, 0, _("Failed to open \"%s\", - %s\n"), fname,
		  strerror(errno));

	va_start(ap, fmt);
	ret = vasprintf(&buf, fmt, ap);
	RETVAL_IF(ret < 0, 0, _("Failed to build message\n"));
	va_end(ap);

	ret = fprintf(fp, "%s", buf);
	RETVAL_IF(ret < 0, 0, _("Failed to print message \"%s\"\n"), buf);

	ret = fclose(fp);
	RETVAL_IF(ret != 0, 0, _("Failed to close \"%s\" - %s\n"),
		  fname, strerror(errno));

	mem_free(buf);
	return 1;
}

/*
 * Initialization of data paths. The cfile argument is the variable
 * which contains the calendar file. If none is given, then the default
 * one (~/.local/share/calcurse/apts) is taken. If the one given does not exist,
 * it is created.
 * The datadir argument can be used to specify an alternative data root dir.
 * The confdir argument can be used to specify an alternative configuration dir.
 * If ~/.calcurse exists, it will be used instead for backward compatibility.
 */
void io_init(const char *cfile, const char *datadir, const char *confdir)
{
	char* home_dir = getenv("HOME");
	char* legacy_dir = NULL;

	if (home_dir) {
		asprintf(&legacy_dir, "%s%s", home_dir, "/" DIR_NAME_LEGACY);
		if (!io_dir_exists(legacy_dir)) {
			mem_free(legacy_dir);
			legacy_dir = NULL;
		}
	}

	if (datadir)
		asprintf(&path_ddir, "%s%s", datadir, "/");
	else if (legacy_dir)
		path_ddir = mem_strdup(legacy_dir);
	else if ((path_ddir = getenv("XDG_DATA_HOME")))
		asprintf(&path_ddir, "%s%s", path_ddir, "/" DIR_NAME);
	else if (home_dir)
		asprintf(&path_ddir, "%s%s", home_dir, "/.local/share/" DIR_NAME);
	else
		path_ddir = mem_strdup("./." DIR_NAME);


	if (confdir)
		asprintf(&path_cdir, "%s%s", confdir, "/");
	else if (datadir)
		path_cdir = mem_strdup(path_ddir);
	else if (legacy_dir)
		path_cdir = mem_strdup(legacy_dir);
	else if ((path_cdir = getenv("XDG_CONFIG_HOME")))
		asprintf(&path_cdir, "%s%s", path_cdir, "/" DIR_NAME);
	else if (home_dir)
		asprintf(&path_cdir, "%s%s", home_dir, "/.config/" DIR_NAME);
	else
		path_cdir = mem_strdup("./." DIR_NAME);

	if (legacy_dir)
		mem_free(legacy_dir);

	/* Data files */
	if (cfile) {
		path_apts = mem_strdup(cfile);
		EXIT_IF(!io_file_exists(path_apts), _("%s does not exist"),
			path_apts);
	} else {
		asprintf(&path_apts, "%s%s", path_ddir, APTS_PATH_NAME);
	}
	asprintf(&path_todo, "%s%s", path_ddir, TODO_PATH_NAME);
	asprintf(&path_cpid, "%s%s", path_ddir, CPID_PATH_NAME);
	asprintf(&path_dpid, "%s%s", path_ddir, DPID_PATH_NAME);
	asprintf(&path_notes, "%s%s", path_ddir, NOTES_DIR_NAME);
	asprintf(&path_dmon_log, "%s%s", path_ddir, DLOG_PATH_NAME);

	/* Configuration files */
	asprintf(&path_conf, "%s%s", path_cdir, CONF_PATH_NAME);
	asprintf(&path_keys, "%s%s", path_cdir, KEYS_PATH_NAME);
	asprintf(&path_hooks, "%s%s", path_cdir, HOOKS_DIR_NAME);
}

void io_extract_data(char *dst_data, const char *org, int len)
{
	int i;

	for (; *org == ' ' || *org == '\t'; org++) ;
	for (i = 0; i < len - 1; i++) {
		if (*org == '\n' || *org == '\0')
			break;
		*dst_data++ = *org++;
	}
	*dst_data = '\0';
}

static pthread_mutex_t io_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t io_periodic_save_mutex = PTHREAD_MUTEX_INITIALIZER;

static void io_mutex_lock(void)
{
	pthread_mutex_lock(&io_mutex);
}

static void io_mutex_unlock(void)
{
	pthread_mutex_unlock(&io_mutex);
}

/* Print all appointments and events to stdout. */
void io_dump_apts(const char *fmt_apt, const char *fmt_rapt,
		  const char *fmt_ev, const char *fmt_rev)
{
	llist_item_t *i;

	LLIST_FOREACH(&recur_elist, i) {
		struct recur_event *rev = LLIST_GET_DATA(i);
		time_t day = DAY(rev->day);
		print_recur_event(fmt_rev, day, rev);
	}

	LLIST_TS_FOREACH(&recur_alist_p, i) {
		struct recur_apoint *rapt = LLIST_GET_DATA(i);
		time_t day = DAY(rapt->start);
		print_recur_apoint(fmt_rapt, day, rapt->start, rapt);
	}

	LLIST_TS_FOREACH(&alist_p, i) {
		struct apoint *apt = LLIST_TS_GET_DATA(i);
		time_t day = DAY(apt->start);
		print_apoint(fmt_apt, day, apt);
	}

	LLIST_FOREACH(&eventlist, i) {
		struct event *ev = LLIST_TS_GET_DATA(i);
		time_t day = DAY(ev->day);
		print_event(fmt_ev, day, ev);
	}
}

/*
 * Save the apts data file, which contains the
 * appointments first, and then the events.
 * Recursive items are written first.
 */
unsigned io_save_apts(const char *aptsfile)
{
	llist_item_t *i;
	FILE *fp;

	if (aptsfile) {
		if (read_only)
			return 1;

		if ((fp = fopen(aptsfile, "w")) == NULL)
			return 0;
	} else {
		fp = stdout;
	}

	recur_save_data(fp);

	if (ui_mode == UI_CURSES)
		LLIST_TS_LOCK(&alist_p);
	LLIST_TS_FOREACH(&alist_p, i) {
		struct apoint *apt = LLIST_TS_GET_DATA(i);
		apoint_write(apt, fp);
	}
	if (ui_mode == UI_CURSES)
		LLIST_TS_UNLOCK(&alist_p);

	LLIST_FOREACH(&eventlist, i) {
		struct event *ev = LLIST_TS_GET_DATA(i);
		event_write(ev, fp);
	}

	if (aptsfile)
		file_close(fp, __FILE_POS__);

	return 1;
}

/* Print all todo items to stdout. */
void io_dump_todo(const char *fmt_todo)
{
	llist_item_t *i;

	LLIST_FOREACH(&todolist, i) {
		struct todo *todo = LLIST_TS_GET_DATA(i);
		print_todo(fmt_todo, todo);
	}
}

/* Save the todo data file. */
unsigned io_save_todo(const char *todofile)
{
	llist_item_t *i;
	FILE *fp;

	if (todofile) {
		if (read_only)
			return 1;

		if ((fp = fopen(todofile, "w")) == NULL)
			return 0;
	} else {
		fp = stdout;
	}

	LLIST_FOREACH(&todolist, i) {
		struct todo *todo = LLIST_TS_GET_DATA(i);
		todo_write(todo, fp);
	}

	if (todofile)
		file_close(fp, __FILE_POS__);

	return 1;
}

/* Save user-defined keys */
unsigned io_save_keys(void)
{
	FILE *fp;

	if (read_only)
		return 1;

	if ((fp = fopen(path_keys, "w")) == NULL)
		return 0;

	keys_save_bindings(fp);
	file_close(fp, __FILE_POS__);

	return 1;
}

static int io_compute_hash(const char *path, char *buf)
{
	FILE *fp = fopen(path, "r");

	if (!fp)
		return 0;
	sha1_stream(fp, buf);
	fclose(fp);

	return 1;
}

/* A merge implies a save operation and must be followed by reload of data. */
static void io_merge_data(void)
{
	char *path_apts_new, *path_todo_new;
	const char *new_ext = ".new";

	asprintf(&path_apts_new, "%s%s", path_apts, new_ext);
	asprintf(&path_todo_new, "%s%s", path_todo, new_ext);

	io_save_apts(path_apts_new);
	io_save_todo(path_todo_new);

	/*
	 * We do not directly write to the data files here; however, the
	 * external merge tool might incorporate changes from the new file into
	 * the main data files.
	 */
	run_hook("pre-save");

	if (!io_files_equal(path_apts, path_apts_new)) {
		const char *arg_apts[] = { conf.mergetool, path_apts,
					   path_apts_new, NULL };
		wins_launch_external(arg_apts);
	}

	if (!io_files_equal(path_todo, path_todo_new)) {
		const char *arg_todo[] = { conf.mergetool, path_todo,
					   path_todo_new, NULL };
		wins_launch_external(arg_todo);
	}

	mem_free(path_apts_new);
	mem_free(path_todo_new);

	/*
	 * We do not directly write to the data files here; however, the
	 * external merge tool will likely have incorporated changes from the
	 * new file into the main data files at this point.
	 */
	run_hook("post-save");

	/*
	 * The user has merged, so override the modified flag
	 * (and follow up with reload of the data files).
	 */
	io_unset_modified();
}

/* For the return values, see io_save_cal() below. */
static int resolve_save_conflict(void)
{
	char *msg_um_asktype = NULL;
	const char *msg_um_prefix =
			_("Data files have changed and will be overwritten:");
	const char *msg_um_overwrite = _("(c)ontinue");
	const char *msg_um_merge = _("(m)erge");
	const char *msg_um_keep = _("c(a)ncel");
	const char *msg_um_choice = _("[cma]");
	int ret = IO_SAVE_CANCEL;

	asprintf(&msg_um_asktype, "%s %s, %s, %s", msg_um_prefix,
		 msg_um_overwrite, msg_um_merge, msg_um_keep);

	switch (status_ask_choice(msg_um_asktype, msg_um_choice, 3)) {
	case 1:
		ret = IO_SAVE_CTINUE;
		break;
	case 2:
		io_merge_data();
		io_load_data(NULL, FORCE);
		ret = IO_SAVE_RELOAD;
		break;
	case 3:
		/* FALLTHROUGH */
	default:
		ret = IO_SAVE_CANCEL;
	}

	mem_free(msg_um_asktype);
	return ret;
}

/*
 * Return codes for new_data() and io_load_data().
 * Note that they are file internal.
 */
#define NONEW		0
#define APTS		(1 << 0)
#define TODO		(1 << 1)
#define APTS_TODO	APTS | TODO
#define NOKNOW		-1
static int new_data()
{
	char sha1_new[SHA1_DIGESTLEN * 2 + 1];
	int ret = NONEW;

	if (io_compute_hash(path_apts, sha1_new)) {
		if (strncmp(sha1_new, apts_sha1, SHA1_DIGESTLEN * 2) != 0) {
			ret |= APTS;
		}
	} else {
		ret = NOKNOW;
		goto exit;
	}

	if (io_compute_hash(path_todo, sha1_new)) {
		if (strncmp(sha1_new, todo_sha1, SHA1_DIGESTLEN * 2) != 0) {
			ret |= TODO;
		}
	} else {
		ret = NOKNOW;
		goto exit;
	}
   exit:
	return ret;
}

/*
 * Save the calendar data.
 * The return value tells how a possible save conflict should be/was resolved:
 * IO_SAVE_CTINUE: continue save operation and overwrite the data files
 * IO_SAVE_RELOAD: cancel save operation (data files changed and reloaded)
 * IO_SAVE_CANCEL: cancel save operation (user's decision, keep data files, no reload)
 * IO_SAVE_NOOP: cancel save operation (nothing has changed)
 * IO_SAVE_ERROR: cannot access data
 */
int io_save_cal(enum save_type s_t)
{
	int ret, new;

	if (read_only)
		return IO_SAVE_CANCEL;

	io_mutex_lock();
	if ((new = new_data()) == NOKNOW) {
		ret = IO_SAVE_ERROR;
		goto cleanup;
	}
	if (new) { /* New data */
		if (s_t == periodic) {
			ret = IO_SAVE_CANCEL;
			goto cleanup;
		}
		/* Interactively decide what to do. */
		if ((ret = resolve_save_conflict()))
			goto cleanup;
	} else /* No new data */
		if (!io_get_modified()) {
			ret = IO_SAVE_NOOP;
			goto cleanup;
		}

	ret = IO_SAVE_CTINUE;
	run_hook("pre-save");
	if (io_save_todo(path_todo) &&
	    io_save_apts(path_apts)) {
		io_compute_hash(path_apts, apts_sha1);
		io_compute_hash(path_todo, todo_sha1);
		io_unset_modified();
	} else
		ret = IO_SAVE_ERROR;
	run_hook("post-save");

cleanup:
	io_mutex_unlock();
	return ret;
}

static void io_load_error(const char *filename, unsigned line,
			  const char *mesg)
{
	EXIT("%s:%u: %s", filename, line, mesg);
}

/*
 * Check what type of data is written in the appointment file,
 * and then load either: a new appointment, a new event, or a new
 * recursive item (which can also be either an event or an appointment).
 */
void io_load_app(struct item_filter *filter)
{
	FILE *data_file;
	int c, is_appointment, is_event, is_recursive;
	struct tm start, end, until, lt;
	struct rpt rpt;
	time_t t;
	int id = 0;
	char type, state = 0L;
	char note[MAX_NOTESIZ + 1], *notep;
	unsigned line = 0;
	char *scan_error;

	t = time(NULL);
	localtime_r(&t, &lt);
	start = end = until = lt;

	data_file = fopen(path_apts, "r");
	EXIT_IF(data_file == NULL, _("failed to open appointment file"));

	sha1_stream(data_file, apts_sha1);
	rewind(data_file);

	for (;;) {
		is_appointment = is_event = is_recursive = 0;
		line++;
		scan_error = NULL;

		c = getc(data_file);
		if (c == EOF)
			break;
		ungetc(c, data_file);

		/* Read the date first: it is common to both events
		 * and appointments.
		 */
		if (fscanf(data_file, "%d / %d / %d ",
			   &start.tm_mon, &start.tm_mday,
			   &start.tm_year) != 3)
			io_load_error(path_apts, line,
				      _("syntax error in the item date"));

		/* Read the next character : if it is an '@' then we have
		 * an appointment, else if it is an '[' we have en event.
		 */
		c = getc(data_file);

		if (c == '@')
			is_appointment = 1;
		else if (c == '[')
			is_event = 1;
		else
			io_load_error(path_apts, line,
				      _("no event nor appointment found"));

		/* Read the remaining informations. */
		if (is_appointment) {
			if (fscanf
			    (data_file,
			     " %d : %d -> %d / %d / %d @ %d : %d ",
			     &start.tm_hour, &start.tm_min, &end.tm_mon,
			     &end.tm_mday, &end.tm_year, &end.tm_hour,
			     &end.tm_min) != 7)
				io_load_error(path_apts, line,
					      _("syntax error in item time or duration"));
		} else if (is_event) {
			if (fscanf(data_file, " %d ", &id) != 1
			    || getc(data_file) != ']')
				io_load_error(path_apts, line,
					      _("syntax error in item identifier"));
			while ((c = getc(data_file)) == ' ') ;
			ungetc(c, data_file);
		} else {
			io_load_error(path_apts, line,
				      _("wrong format in the appointment or event"));
			/* NOTREACHED */
		}

		/* Check if we have a recursive item. */
		c = getc(data_file);

		if (c == '{') {
			is_recursive = 1;
			if (fscanf(data_file, " %d%c ", &rpt.freq, &type) != 2)
				io_load_error(path_apts, line,
					      _("syntax error in item repetition"));
			else
				rpt.type = recur_char2def(type);
			c = getc(data_file);
			/* Optional until date */
			if (c == '-' && getc(data_file) == '>') {
				if (fscanf
				    (data_file, " %d / %d / %d ",
				     &until.tm_mon, &until.tm_mday,
				     &until.tm_year) != 3)
					io_load_error(path_apts, line,
						      _("syntax error in until date"));
				if (!check_date(until.tm_year, until.tm_mon,
						until.tm_mday))
					io_load_error(path_apts, line,
						      _("until date error"));
				until.tm_hour = 0;
				until.tm_min = 0;
				until.tm_sec = 0;
				until.tm_isdst = -1;
				until.tm_year -= 1900;
				until.tm_mon--;
				rpt.until = mktime(&until);
				c = getc(data_file);
			} else
				rpt.until = 0;
			/* Optional bymonthday list */
			if (c == 'd') {
				if (rpt.type == RECUR_WEEKLY)
					io_load_error(path_apts, line,
						      _("BYMONTHDAY illegal with WEEKLY"));
				ungetc(c, data_file);
				recur_bymonthday(&rpt.bymonthday, data_file);
				c = getc(data_file);
			} else
				LLIST_INIT(&rpt.bymonthday);
			/* Optional bywday list */
			if (c == 'w') {
				ungetc(c, data_file);
				recur_bywday(rpt.type, &rpt.bywday, data_file);
				c = getc(data_file);
			} else
				LLIST_INIT(&rpt.bywday);
			/* Optional bymonth list */
			if (c == 'm') {
				ungetc(c, data_file);
				recur_bymonth(&rpt.bymonth, data_file);
				c = getc(data_file);
			} else
				LLIST_INIT(&rpt.bymonth);
			/* Optional exception dates */
			if (c == '!') {
				ungetc(c, data_file);
				recur_exc_scan(&rpt.exc, data_file);
				c = getc(data_file);
			} else
				LLIST_INIT(&rpt.exc);
			/* End of recurrence rule */
			if (c != '}')
				io_load_error(path_apts, line,
					      _("missing end of recurrence"));
			while ((c = getc(data_file)) == ' ') ;
		}

		/* Check if a note is attached to the item. */
		if (c == '>') {
			note_read(note, data_file);
			c = getc(data_file);
			notep = note;
		} else
			notep = NULL;

		/*
		 * Last: read the item description and load it into its
		 * corresponding linked list, depending on the item type.
		 */
		if (is_appointment) {
			if (c == '!')
				state |= APOINT_NOTIFY;
			else if (c == '|')
				state = 0L;
			else
				io_load_error(path_apts, line,
					      _("syntax error in item state"));

			if (is_recursive)
				scan_error = recur_apoint_scan(data_file, start, end, state,
						  notep, filter, &rpt);
			else
				scan_error = apoint_scan(data_file, start, end, state,
					    notep, filter);
		} else if (is_event) {
			ungetc(c, data_file);
			if (is_recursive)
				scan_error = recur_event_scan(data_file, start, id, notep,
						 filter, &rpt);
			else
				scan_error = event_scan(data_file, start, id, notep, filter);
		} else {
			io_load_error(path_apts, line,
				      _("wrong format in the appointment or event"));
			/* NOTREACHED */
		}
		if (scan_error)
			io_load_error(path_apts, line, scan_error);
	}
	file_close(data_file, __FILE_POS__);
}

/* Load the todo data */
void io_load_todo(struct item_filter *filter)
{
	FILE *data_file;
	char *newline;
	int c, id, completed, cond;
	char buf[BUFSIZ], e_todo[BUFSIZ], note[MAX_NOTESIZ + 1];
	unsigned line = 0;

	data_file = fopen(path_todo, "r");
	EXIT_IF(data_file == NULL, _("failed to open todo file"));

	sha1_stream(data_file, todo_sha1);
	rewind(data_file);

	for (;;) {
		line++;
		c = getc(data_file);
		if (c == EOF) {
			break;
		} else if (c == '[') {
			/* new style with id */
			c = getc(data_file);
			if (c == '-') {
				completed = 1;
			} else {
				completed = 0;
				ungetc(c, data_file);
			}
			if (fscanf(data_file, " %d ", &id) != 1
			    || getc(data_file) != ']')
				io_load_error(path_todo, line,
					      _("syntax error in item identifier"));
			while ((c = getc(data_file)) == ' ') ;
			ungetc(c, data_file);
		} else {
			id = 9;
			completed = 0;
			ungetc(c, data_file);
		}
		/* Now read the attached note, if any. */
		c = getc(data_file);
		if (c == '>') {
			note_read(note, data_file);
		} else {
			note[0] = '\0';
			ungetc(c, data_file);
		}
		/* Then read todo description. */
		if (!fgets(buf, sizeof buf, data_file))
			buf[0] = '\0';
		newline = strchr(buf, '\n');
		if (newline)
			*newline = '\0';
		io_extract_data(e_todo, buf, sizeof buf);

		/* Filter item. */
		struct todo *todo = NULL;
		if (filter) {
			cond = (
				!(filter->type_mask & TYPE_MASK_TODO) ||
				(filter->regex && regexec(filter->regex, e_todo, 0, 0, 0)) ||
				(filter->priority && id != filter->priority) ||
				(filter->completed && !completed) ||
				(filter->uncompleted && completed)
			);
			if (filter->hash) {
				todo = todo_add(e_todo, id, completed, note);
				char *hash = todo_hash(todo);
				cond = cond || !hash_matches(filter->hash, hash);
				mem_free(hash);
			}

			if ((!filter->invert && cond) || (filter->invert && !cond)) {
				if (filter->hash)
					todo_delete(todo);
				continue;
			}
		}

		if (!todo)
			todo = todo_add(e_todo, id, completed, note);
	}
	file_close(data_file, __FILE_POS__);
}

/*
 * Load appointments and todo items.
 * Unless told otherwise, the function will only load a file that has changed
 * since last saved or loaded. The new_data() return code is passed on when
 * force is false. When force is true (FORCE), the return code is of no use.
 */
int io_load_data(struct item_filter *filter, int force)
{
	run_hook("pre-load");
	if (force)
		force = APTS_TODO;
	else
		force = new_data();

	if (force == NOKNOW)
		goto exit;

	if (force & APTS) {
		apoint_llist_free();
		event_llist_free();
		recur_apoint_llist_free();
		recur_event_llist_free();
		apoint_llist_init();
		event_llist_init();
		recur_apoint_llist_init();
		recur_event_llist_init();
		io_load_app(filter);
	}
	if (force & TODO) {
		todo_free_list();
		todo_init_list();
		io_load_todo(filter);
	}

	io_unset_modified();
   exit:
	run_hook("post-load");
	return force;
}

/*
 * The return codes reflect the user choice in case of unsaved in-memory changes.
 */
int io_reload_data(void)
{
	char *msg_um_asktype = NULL;
	int load = NOFORCE;
	int ret = IO_RELOAD_LOAD;

	io_mutex_lock();
	if (io_get_modified()) {
		const char *msg_um_prefix =
				_("Screen data have changed and will be lost:");
		const char *msg_um_discard = _("(c)ontinue");
		const char *msg_um_merge = _("(m)erge");
		const char *msg_um_keep = _("c(a)ncel");
		const char *msg_um_choice = _("[cma]");

		asprintf(&msg_um_asktype, "%s %s, %s, %s", msg_um_prefix,
			 msg_um_discard, msg_um_merge, msg_um_keep);

		switch (status_ask_choice(msg_um_asktype, msg_um_choice, 3)) {
		case 1:
			load = FORCE;
			ret = IO_RELOAD_CTINUE;
			break;
		case 2:
			io_merge_data();
			load = FORCE;
			ret = IO_RELOAD_MERGE;
			break;
		case 3:
			ret = IO_RELOAD_CANCEL;
			/* FALLTHROUGH */
		default:
			goto cleanup;
		}
	}

	load = io_load_data(NULL, load);
	if (load == NONEW)
		ret = IO_RELOAD_NOOP;
	else if (load == NOKNOW)
		ret = IO_RELOAD_ERROR;
cleanup:
	io_mutex_unlock();
	mem_free(msg_um_asktype);
	return ret;
}

static void
load_keys_ht_getkey(struct ht_keybindings_s *data, const char **key,
		    int *len)
{
	*key = data->label;
	*len = strlen(data->label);
}

static int
load_keys_ht_compare(struct ht_keybindings_s *data1,
		     struct ht_keybindings_s *data2)
{
	const int KEYLEN = strlen(data1->label);

	if (strlen(data2->label) == KEYLEN
	    && !memcmp(data1->label, data2->label, KEYLEN))
		return 0;
	else
		return 1;
}

/*
 * Load user-definable keys from file.
 * A hash table is used to speed up loading process in avoiding string
 * comparisons.
 * A log file is also built in case some errors were found in the key
 * configuration file.
 */
void io_load_keys(const char *pager)
{
	struct ht_keybindings_s virt_keys[NBVKEYS], *ht_elm, ht_entry;
	FILE *keyfp;
	char buf[BUFSIZ], key_label[BUFSIZ], key_str[BUFSIZ];
	char *p, *msg;
	struct io_file *log;
	int i, n, skipped, loaded, line, assigned, undefined, key;

	keys_init();

	struct ht_keybindings ht_keys = HTABLE_INITIALIZER(&ht_keys);

	for (i = 0; i < NBVKEYS; i++) {
		virt_keys[i].key = (enum vkey)i;
		virt_keys[i].label = keys_get_label((enum vkey)i);
		HTABLE_INSERT(ht_keybindings, &ht_keys, &virt_keys[i]);
	}

	keyfp = fopen(path_keys, "r");
	EXIT_IF(keyfp == NULL, _("failed to open key file"));

	log = io_log_init();
	skipped = loaded = line = 0;
	while (fgets(buf, BUFSIZ, keyfp) != NULL) {
		line++;
		p = buf;
		while (*p == ' ' || *p == '\t') p++;
		if (*p == '#' || *p == '\n')
			continue;

		/* Find the virtual key by key label. */
		if (sscanf(p, "%s", key_label) != 1) {
			skipped++;
			io_log_print(log, line,
				     _("Could not read key label"));
			continue;
		}
		p += strlen(key_label);
		ht_entry.label = key_label;
		ht_elm = HTABLE_LOOKUP(ht_keybindings, &ht_keys, &ht_entry);
		if (!ht_elm) {
			skipped++;
			asprintf(&msg,
				 _("Key label not recognized: \"%s\""),
				 key_label);
			io_log_print(log, line, msg);
			mem_free(msg);
			continue;
		}

		/* Assign keyboard keys to the virtual key. */
		assigned = undefined = 0;
		for (;;) {
			if (sscanf(p, "%s%n", key_str, &n) != 1) {
				if (assigned || undefined)
					loaded++;
				else {
					skipped++;
					asprintf(&msg,
						 _("No keys assigned to "
						   "\"%s\"."),
						 key_label);
					io_log_print(log, line, msg);
					mem_free(msg);
				}
				break;
			}
			p += n;
			if (!strcmp(key_str, "UNDEFINED")) {
				undefined++;
				keys_assign_binding(-1, ht_elm->key);
			} else if ((key = keys_str2int(key_str)) < 0) {
				skipped++;
				asprintf(&msg,
					 _("Keyname not recognized: \"%s\""),
					 key_str);
				io_log_print(log, line, msg);
				mem_free(msg);
			} else if (keys_assign_binding(key, ht_elm->key)) {
				skipped++;
				asprintf(&msg,
					 _("\"%s\" assigned twice: \"%s\"."),
					 key_str, key_label);
				io_log_print(log, line, msg);
				mem_free(msg);
			} else
				assigned++;
		}
	}
	file_close(keyfp, __FILE_POS__);
	if (loaded < NBVKEYS && (i = keys_fill_missing()) < 1) {
		skipped++;
		strcpy(key_label, keys_get_label((enum vkey)(-i)));
		strcpy(key_str, keys_get_binding((enum vkey)(-i)));
		asprintf(&msg, _("Action \"%s\" absent, but default key \"%s\" "
				 "assigned to another action."),
				 key_label, key_str);
		io_log_print(log, line, msg);
		mem_free(msg);
	}
	file_close(log->fd, __FILE_POS__);
	if (skipped > 0) {
		msg = _("Errors in the keys file.");
		io_log_display(log, msg, pager);
		WARN_MSG(_("Remove offending line(s) from the keys file, "
			   "aborting..."));
		exit_calcurse(EXIT_FAILURE);
	}
	io_log_free(log);
	/* Default keys were inserted. */
	if (loaded < NBVKEYS)
		io_save_keys();
	/* Should never occur. */
	EXIT_IF(keys_check_missing(),
		_("Some actions do not have any associated key bindings!"));
}

int io_check_dir(const char *dir)
{
	if (read_only)
		return -1;

	char *path = mem_strdup(dir);
	char *index;

	int existed = 1, failed = 0;
	errno = 0;
	for (index = path + 1; *index; index++) {
		if (*index == '/') {
			*index = '\0';
			if (mkdir(path, 0700) != 0) {
				if (errno != EEXIST) {
					failed = 1;
					break;
				}
			} else {
				existed = 0;
			}
			*index = '/';
		}
	}

	if (!failed && mkdir(path, 0700) != 0) {
		if (errno != EEXIST)
			failed = 1;
	} else {
		existed = 0;
	}

	if(failed) {
		fprintf(stderr,
			_("FATAL ERROR: could not create %s: %s\n"),
			path, strerror(errno));
		exit_calcurse(EXIT_FAILURE);
	}

	mem_free(path);
	return existed;
}

unsigned io_dir_exists(const char *path)
{
	struct stat st;

	return (!stat(path, &st) && S_ISDIR(st.st_mode));
}

unsigned io_file_exists(const char *file)
{
	FILE *fd;

	if (file && (fd = fopen(file, "r")) != NULL) {
		fclose(fd);
		return 1;
	} else {
		return 0;
	}
}

int io_check_file(const char *file)
{
	if (read_only)
		return -1;

	errno = 0;
	if (io_file_exists(file)) {
		return 1;
	} else {
		FILE *fd;

		if ((fd = fopen(file, "w")) == NULL) {
			fprintf(stderr,
				_("FATAL ERROR: could not create %s: %s\n"),
				file, strerror(errno));
			exit_calcurse(EXIT_FAILURE);
		}
		file_close(fd, __FILE_POS__);

		return 0;
	}
}

/*
 * Checks if data files exist. If not, create them.
 * The following structure has to be created:
 *
 *   <datadir>   <configdir>
 *        |             |
 *        |__ apts      |___ conf
 *        |__ todo      |___ keys
 *        |__ notes/    |___ hooks/
 *
 * Defaults:
 * - datadir: $XDG_DATA_HOME/calcurse (~/.local/share/calcurse)
 * - configdir: $XDG_CONFIG_HOME/calcurse (~/.config/calcurse)
 */
int io_check_data_files(void)
{
	int missing = 0;

	missing += io_check_dir(path_ddir) ? 0 : 1;
	missing += io_check_dir(path_notes) ? 0 : 1;
	missing += io_check_file(path_todo) ? 0 : 1;
	missing += io_check_file(path_apts) ? 0 : 1;
	missing += io_check_dir(path_cdir) ? 0 : 1;
	missing += io_check_file(path_conf) ? 0 : 1;
	missing += io_check_dir(path_hooks) ? 0 : 1;

	if (!io_check_file(path_keys)) {
		missing++;
		keys_dump_defaults(path_keys);
	}

	return missing;
}

/* Export calcurse data. */
void io_export_data(enum export_type type, int export_uid)
{
	FILE *stream = NULL;
	const char *success = _("The data were successfully exported");
	const char *enter = _("Press [ENTER] to continue");

	if (type < IO_EXPORT_ICAL || type >= IO_EXPORT_NBTYPES)
		EXIT(_("unknown export type"));

	switch (ui_mode) {
	case UI_CMDLINE:
		stream = stdout;
		break;
	case UI_CURSES:
		stream = get_export_stream(type);
		break;
	default:
		EXIT(_("wrong export mode"));
		/* NOTREACHED */
	}

	if (stream == NULL)
		return;

	if (type == IO_EXPORT_ICAL)
		ical_export_data(stream, export_uid);
	else if (type == IO_EXPORT_PCAL)
		pcal_export_data(stream);

	if (!quiet && ui_mode == UI_CURSES) {
		fclose(stream);
		status_mesg(success, enter);
		keys_wait_for_any_key(win[KEY].p);
	}
}

static FILE *get_import_stream(enum import_type type, char **stream_name)
{
	FILE *stream = NULL;
	const char *ask_fname =
	    _("Enter the file name to import data from:");
	const char *wrong_file =
	    _("The file cannot be accessed, please enter another file name.");
	const char *press_enter = _("Press [ENTER] to continue.");

	*stream_name = mem_malloc(BUFSIZ);
	memset(*stream_name, 0, BUFSIZ);
	while (stream == NULL) {
		status_mesg(ask_fname, "");
		if (updatestring(win[STA].p, stream_name, 0, 1)) {
			mem_free(*stream_name);
			return NULL;
		}
		stream = fopen(*stream_name, "r");
		if (stream == NULL) {
			status_mesg(wrong_file, press_enter);
			keys_wait_for_any_key(win[KEY].p);
		}
	}

	return stream;
}

/*
 * Import data from a given stream (either stdin in non-interactive mode, or the
 * user given file in interactive mode).
 * A temporary log file is created in /tmp to store the import process report,
 * and is cleared at the end.
 */
int  io_import_data(enum import_type type, char *stream_name,
		    const char *fmt_ev, const char *fmt_rev,
		    const char *fmt_apt, const char *fmt_rapt,
		    const char *fmt_todo)
{
	const char *proc_report =
	    _("Import process report: %04d lines read");
	char *stats_str[4];
	FILE *stream = NULL;
	struct io_file *log;
	struct {
		unsigned events, apoints, todos, lines, skipped;
	} stats;

	EXIT_IF(type < 0
		|| type >= IO_IMPORT_NBTYPES, _("unknown import type"));
	switch (ui_mode) {
	case UI_CMDLINE:
		if (!strcmp(stream_name, "-"))
			stream = stdin;
		else
			stream = fopen(stream_name, "r");
		EXIT_IF(stream == NULL,
			_("FATAL ERROR: the input file cannot be accessed, "
			 "Aborting..."));
		break;
	case UI_CURSES:
		stream = get_import_stream(type, &stream_name);
		break;
	default:
		EXIT(_("FATAL ERROR: wrong import mode"));
		/* NOTREACHED */
	}

	if (stream == NULL)
		return 0;

	memset(&stats, 0, sizeof stats);

	log = io_log_init();
	if (log == NULL) {
		if (stream != stdin)
			file_close(stream, __FILE_POS__);
		return 0;
	}

	if (type == IO_IMPORT_ICAL)
		ical_import_data(stream_name, stream, log->fd, &stats.events,
				 &stats.apoints, &stats.todos,
				 &stats.lines, &stats.skipped, fmt_ev, fmt_rev,
				 fmt_apt, fmt_rapt, fmt_todo);

	if (stream != stdin)
		file_close(stream, __FILE_POS__);

	if (ui_mode == UI_CURSES &&
	    (stats.apoints > 0 || stats.events > 0 || stats.todos > 0))
		io_set_modified();

	asprintf(&stats_str[0], ngettext("%d app", "%d apps", stats.apoints),
		 stats.apoints);
	asprintf(&stats_str[1],
		 ngettext("%d event", "%d events", stats.events),
		 stats.events);
	asprintf(&stats_str[2], ngettext("%d todo", "%d todos", stats.todos),
		 stats.todos);
	asprintf(&stats_str[3], _("%d skipped"), stats.skipped);

	if (ui_mode == UI_CURSES && !quiet) {
		char *read, *stat;

		asprintf(&read, proc_report, stats.lines);
		asprintf(&stat, "%s / %s / %s / %s (%s)",
			 stats_str[0], stats_str[1], stats_str[2],
			 stats_str[3], _("Press [ENTER] to continue"));
		status_mesg(read, stat);
		mem_free(read);
		mem_free(stat);
		keys_wait_for_any_key(win[KEY].p);
	} else if (ui_mode == UI_CMDLINE && !quiet) {
		printf(proc_report, stats.lines);
		printf("\n%s / %s / %s / %s\n", stats_str[0], stats_str[1],
		       stats_str[2], stats_str[3]);
	}

	/* User has the choice to look at the log file if some items could not be
	   imported.
	 */
	file_close(log->fd, __FILE_POS__);
	if (stats.skipped > 0) {
		const char *view_log = _("Some items could not be imported.");
		io_log_display(log, view_log, conf.pager);
	}

	mem_free(stats_str[0]);
	mem_free(stats_str[1]);
	mem_free(stats_str[2]);
	mem_free(stats_str[3]);
	if (ui_mode == UI_CURSES)
		mem_free(stream_name);
	if (!stats.skipped) {
		io_log_free(log);
		return 1;
	} else
		return 0;
}

struct io_file *io_log_init(void)
{
	char *logprefix, *logname;
	struct io_file *log = mem_malloc(sizeof(struct io_file));

	if (!log) {
		ERROR_MSG(_("Warning: could not open temporary log file, Aborting..."));
		return NULL;
	}
	asprintf(&logprefix, "%s/calcurse_log", get_tempdir());
	logname = new_tempfile(logprefix);
	if (!logname) {
		ERROR_MSG(_("Warning: could not create temporary log file, Aborting..."));
		goto error;
	}
	log->name = mem_strdup(logname);
	log->fd = fopen(log->name, "w");
	if (log->fd == NULL) {
		ERROR_MSG(_("Warning: could not open temporary log file, Aborting..."));
		goto error;
	}

	goto cleanup;
error:
	mem_free(log);
	log = NULL;
cleanup:
	mem_free(logprefix);
	mem_free(logname);
	return log;
}

void io_log_print(struct io_file *log, int line, const char *msg)
{
	if (log && log->fd)
		fprintf(log->fd, "line %d: %s\n", line, msg);
}

void io_log_display(struct io_file *log, const char *msg, const char *pager)
{
	char *msgq;

	RETURN_IF(log == NULL, _("No log file to display!"));
	if (ui_mode == UI_CMDLINE) {
		fprintf(stderr, "\n%s\n", msg);
		fprintf(stderr, _("See %s for details."), log->name);
		fputc('\n', stderr);
	} else {
		asprintf(&msgq, "%s %s", msg, _("Display log file?"));
		if (status_ask_bool(msgq) == 1) {
			const char *arg[] = { pager, log->name, NULL };
			wins_launch_external(arg);
		}
		mem_free(msgq);
		wins_erase_status_bar();
	}
}

void io_log_free(struct io_file *log)
{
	if (!log)
		return;
	EXIT_IF(unlink(log->name) != 0,
		_("Warning: could not erase temporary log file %s, Aborting..."),
		log->name);
	mem_free(log->name);
	mem_free(log);
}

/* Thread used to periodically save data. */
static void *io_psave_thread(void *arg)
{
	int delay = conf.periodic_save;
	EXIT_IF(delay < 0, _("Invalid delay"));
	char *mesg = _("Periodic save cancelled. Data files have changed. "
		     "Save and merge interactively");

	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
	for (;;) {
		sleep(delay * MININSEC);
		pthread_mutex_lock(&io_periodic_save_mutex);
		if (io_save_cal(periodic) == IO_SAVE_CANCEL)
			que_ins(mesg, now(), 2);
		pthread_mutex_unlock(&io_periodic_save_mutex);
	}
}

/* Launch the thread which handles periodic saves. */
void io_start_psave_thread(void)
{
	pthread_create(&io_t_psave, NULL, io_psave_thread, NULL);
}

/* Stop periodic data saves. */
void io_stop_psave_thread(void)
{
	/* Is the thread running? */
	if (pthread_equal(io_t_psave, pthread_self()))
		return;

	/* Lock the mutex to avoid cancelling the thread during saving. */
	pthread_mutex_lock(&io_periodic_save_mutex);
	pthread_cancel(io_t_psave);
	pthread_join(io_t_psave, NULL);
	pthread_mutex_unlock(&io_periodic_save_mutex);
	io_t_psave = pthread_self();
}

/*
 * This sets a lock file to prevent from having two different instances of
 * calcurse running.
 *
 * If the lock cannot be obtained, then warn the user and exit calcurse. Else,
 * create a .calcurse.pid file in the user defined directory, which will be
 * removed when calcurse exits.
 *
 * Note: When creating the lock file, the interactive mode is not initialized
 * yet.
 */
void io_set_lock(void)
{
	FILE *lock = fopen(path_cpid, "r");
	int pid;

	if (lock != NULL) {
		/* If there is a lock file, check whether the process exists. */
		if (fscanf(lock, "%d", &pid) == 1) {
			fclose(lock);
			if (kill(pid, 0) != 0 && errno == ESRCH)
				lock = NULL;
		} else {
			fclose(lock);
		}
	}

	if (lock != NULL) {
		fprintf(stderr,
			_("\nWARNING: it seems that another calcurse instance is "
			 "already running.\n"
			 "If this is not the case, please remove the following "
			 "lock file: \n\"%s\"\n"
			 "and restart calcurse.\n"), path_cpid);
		exit(EXIT_FAILURE);
	}

	if (!io_dump_pid(path_cpid))
		EXIT(_("FATAL ERROR: could not create %s: %s\n"),
		     path_cpid, strerror(errno));
}

/*
 * Create a new file and write the process pid inside (used to create a simple
 * lock for example). Overwrite already existing files.
 */
unsigned io_dump_pid(char *file)
{
	pid_t pid;
	FILE *fp;

	if (!file)
		return 0;

	pid = getpid();
	if (!(fp = fopen(file, "w"))
	    || fprintf(fp, "%ld\n", (long)pid) < 0 || fclose(fp) != 0)
		return 0;

	return 1;
}

/*
 * Return the pid number contained in a file previously created with
 * io_dump_pid ().
 * If no file was found, return 0.
 */
unsigned io_get_pid(char *file)
{
	FILE *fp;
	unsigned pid;

	if (!file)
		return 0;

	if ((fp = fopen(file, "r")) == NULL)
		return 0;

	if (fscanf(fp, "%u", &pid) != 1)
		return 0;

	fclose(fp);

	return pid;
}

/*
 * Check whether two files are equal.
 */
int io_files_equal(const char *file1, const char *file2)
{
	FILE *fp1, *fp2;
	int ret = 0;

	if (!file1 || !file2)
		return 0;

	fp1 = fopen(file1, "rb");
	fp2 = fopen(file2, "rb");

	while (!feof(fp1) && !feof(fp2)) {
		if (fgetc(fp1) != fgetc(fp2))
			goto cleanup;
	}

	ret = 1;
cleanup:
	fclose(fp1);
	fclose(fp2);
	return ret;
}

/*
 * Copy an existing file to a new location.
 */
int io_file_cp(const char *src, const char *dst)
{
	FILE *fp_src, *fp_dst;
	char *buffer[BUFSIZ];
	unsigned int bytes_read;

	if (!(fp_src = fopen(src, "rb")))
		return 0;
	if (!(fp_dst = fopen(dst, "wb")))
		return 0;

	while (!feof(fp_src)) {
		bytes_read = fread(buffer, 1, BUFSIZ, fp_src);
		if (bytes_read > 0) {
			if (fwrite(buffer, 1, bytes_read, fp_dst) !=
			    bytes_read)
				return 0;
		} else {
			return 0;
		}
	}

	fclose(fp_dst);
	fclose(fp_src);

	return 1;
}

void io_unset_modified(void)
{
	modified = 0;
}

void io_set_modified(void)
{
	modified = 1;
}

int io_get_modified(void)
{
	return modified;
}