nopen.c 4.14 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
52
53
54
55
56
{
	int file,share,count=0;

    if(access&O_DENYNONE) {
        share=SH_DENYNO;
        access&=~O_DENYNONE; }
    else if(access==O_RDONLY) share=SH_DENYWR;
    else share=SH_DENYRW;
	if(!(access&O_TEXT))
		access|=O_BINARY;
57
    while(((file=sopen(str,access,share,S_IREAD|S_IWRITE))==-1)
58
        && (errno==EACCES || errno==EAGAIN || errno==EDEADLOCK) && count++<LOOP_NOPEN)
59
60
61
62
63
64
65
66
        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
67
FILE* fnopen(int* fd, const char* str, int access)
68
69
70
71
{
	char	mode[128];
	int		file;
	FILE *	stream;
72

73
74
75
76
77
78
79
    if((file=nopen(str,access))==-1)
        return(NULL);

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

    if(access&O_APPEND) {
deuce's avatar
deuce committed
80
        if((access&O_RDWR)==O_RDWR)
81
82
83
            strcpy(mode,"a+");
        else
            strcpy(mode,"a"); 
84
	} else if(access&(O_TRUNC|O_WRONLY)) {
deuce's avatar
deuce committed
85
		if((access&O_RDWR)==O_RDWR)
86
			strcpy(mode,"w+");
87
88
		else
			strcpy(mode,"w");
89
	} else {
deuce's avatar
deuce committed
90
        if((access&O_RDWR)==O_RDWR)
91
92
93
94
95
96
97
98
99
100
101
102
            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
103
104
105
106
107

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

108
	if((file=nopen(fname,O_WRONLY|O_CREAT))<0)
rswindell's avatar
rswindell committed
109
110
111
112
113
		return(FALSE);
	close(file);

	return(TRUE);
}
114
115
116
117

BOOL fmutex(const char* fname, const char* text)
{
	int file;
118
#if !defined(NO_SOCKET_SUPPORT)
119
	char hostname[128];
120
121
122
	if(text==NULL && gethostname(hostname,sizeof(hostname))==0)
		text=hostname;
#endif
123
124
125
126
127
128
129
130

	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);
}