nopen.c 4.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* nopen.c */

/* Network open functions (nopen and fnopen) */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html		*
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
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"
#include "crc32.h"

/****************************************************************************/
/* Network open function. Opens all files DENYALL and retries LOOP_NOPEN    */
/* number of times if the attempted file is already open or denying access  */
/* for some other reason.	All files are opened in BINARY mode.			*/
/****************************************************************************/
rswindell's avatar
rswindell committed
46
int nopen(const char* str, int access)
47
48
49
50
51
{
	int file,share,count=0;

    if(access&O_DENYNONE) {
        share=SH_DENYNO;
52
53
54
55
56
57
        access&=~O_DENYNONE; 
	} 
	else if((access&~(O_TEXT|O_BINARY))==O_RDONLY) 
		share=SH_DENYWR;
    else 
		share=SH_DENYRW;
58
59

#if !defined(__unix__)	/* Basically, a no-op on Unix anyway */
60
61
	if(!(access&O_TEXT))
		access|=O_BINARY;
62
#endif
63
    while(((file=sopen(str,access,share,S_IREAD|S_IWRITE))==-1)
64
        && (errno==EACCES || errno==EAGAIN || errno==EDEADLOCK) && count++<LOOP_NOPEN)
65
66
67
68
69
70
71
72
        if(count)
            mswait(100);
    return(file);
}
/****************************************************************************/
/* This function performs an nopen, but returns a file stream with a buffer */
/* allocated.																*/
/****************************************************************************/
rswindell's avatar
rswindell committed
73
FILE* fnopen(int* fd, const char* str, int access)
74
75
76
77
{
	char	mode[128];
	int		file;
	FILE *	stream;
78

79
80
81
82
83
84
85
    if((file=nopen(str,access))==-1)
        return(NULL);

    if(fd!=NULL)
        *fd=file;

    if(access&O_APPEND) {
deuce's avatar
deuce committed
86
        if((access&O_RDWR)==O_RDWR)
87
88
89
            strcpy(mode,"a+");
        else
            strcpy(mode,"a"); 
90
	} else if(access&(O_TRUNC|O_WRONLY)) {
deuce's avatar
deuce committed
91
		if((access&O_RDWR)==O_RDWR)
92
			strcpy(mode,"w+");
93
94
		else
			strcpy(mode,"w");
95
	} else {
deuce's avatar
deuce committed
96
        if((access&O_RDWR)==O_RDWR)
97
98
99
100
101
102
103
104
105
106
107
108
            strcpy(mode,"r+");
        else
            strcpy(mode,"r"); 
	}
    stream=fdopen(file,mode);
    if(stream==NULL) {
        close(file);
        return(NULL); 
	}
    setvbuf(stream,NULL,_IOFBF,FNOPEN_BUF_SIZE);
    return(stream);
}
rswindell's avatar
rswindell committed
109
110
111
112
113

BOOL ftouch(const char* fname)
{
	int file;

114
	if((file=nopen(fname,O_WRONLY|O_CREAT))<0)
rswindell's avatar
rswindell committed
115
116
117
118
119
		return(FALSE);
	close(file);

	return(TRUE);
}
120
121
122
123

BOOL fmutex(const char* fname, const char* text)
{
	int file;
124
#if !defined(NO_SOCKET_SUPPORT)
125
	char hostname[128];
126
127
128
	if(text==NULL && gethostname(hostname,sizeof(hostname))==0)
		text=hostname;
#endif
129
130
131
132
133
134
135
136

	if((file=open(fname,O_CREAT|O_WRONLY|O_EXCL,S_IREAD|S_IWRITE))<0)
		return(FALSE);
	if(text!=NULL)
		write(file,text,strlen(text));
	close(file);
	return(TRUE);
}