summaryrefslogtreecommitdiff
path: root/src/common/lock.c
blob: 3419034347b040f85d7b9d491bd89da08afd4b2d (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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "lock.h"
#include "showmsg.h"

// �������݃t�@�C���̕ی쏈��
// �i�������݂��I���܂ŁA���t�@�C����ۊǂ��Ă����j

// �V�����t�@�C���̏������݊J�n
FILE* lock_fopen(const char* filename,int *info) {
	char newfile[512];
	FILE *fp;
	int  no = 0;

	// ���S�ȃt�@�C�����𓾂�i�蔲���j
	do {
		sprintf(newfile,"%s_%04d.tmp",filename,++no);
	} while((fp = fopen(newfile,"r")) && (fclose(fp), no<9999) );
	*info = no;
	return fopen(newfile,"w");
}

// ���t�@�C�����폜���V�t�@�C�������l�[��
int lock_fclose(FILE *fp,const char* filename,int *info) {
	int  ret = 0;
	char newfile[512];
	if(fp != NULL) {
		ret = fclose(fp);
		sprintf(newfile,"%s_%04d.tmp",filename,*info);
		remove(filename);
		// ���̃^�C�~���O�ŗ�����ƍň��B
		if (rename(newfile,filename) != 0) {
			sprintf(tmp_output,"%s - '"CL_WHITE"%s"CL_RESET"'\n", strerror(errno), newfile);
			ShowError(tmp_output);
		}
		return ret;
	} else {
		return 1;
	}
}