nopen.c 4.52 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)		*
 *																			*
rswindell's avatar
rswindell committed
11
 * Copyright 2011 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
 *																			*
 * 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"

/****************************************************************************/
42 43 44 45
/* Network open function. Opens all files DENYALL, DENYWRITE, or DENYNONE	*/
/* depending on access, 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.										*/
46
/****************************************************************************/
rswindell's avatar
rswindell committed
47
int nopen(const char* str, int access)
48 49 50 51 52
{
	int file,share,count=0;

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

#if !defined(__unix__)	/* Basically, a no-op on Unix anyway */
61 62
	if(!(access&O_TEXT))
		access|=O_BINARY;
63
#endif
64
    while(((file=sopen(str,access,share,DEFFILEMODE))==-1)
65
        && (errno==EACCES || errno==EAGAIN || errno==EDEADLOCK) && count++<LOOP_NOPEN)
66 67 68 69
        if(count)
            mswait(100);
    return(file);
}
70

71 72 73 74
/****************************************************************************/
/* This function performs an nopen, but returns a file stream with a buffer */
/* allocated.																*/
/****************************************************************************/
rswindell's avatar
rswindell committed
75
FILE* fnopen(int* fd, const char* str, int access)
76
{
77
	char*	mode;
78 79
	int		file;
	FILE *	stream;
80

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

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

    if(access&O_APPEND) {
deuce's avatar
deuce committed
88
        if((access&O_RDWR)==O_RDWR)
89
            mode="a+";
90
        else
91
            mode="a"; 
92
	} else if(access&(O_TRUNC|O_WRONLY)) {
deuce's avatar
deuce committed
93
		if((access&O_RDWR)==O_RDWR)
94
			mode="w+";
95
		else
96
			mode="w";
97
	} else {
deuce's avatar
deuce committed
98
        if((access&O_RDWR)==O_RDWR)
99
            mode="r+";
100
        else
101
            mode="r"; 
102 103 104 105 106 107 108 109 110
	}
    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
111 112 113 114

BOOL ftouch(const char* fname)
{
	int file;
115
	struct utimbuf ut;
rswindell's avatar
rswindell committed
116

117
	/* update the time stamp */
118 119
	ut.actime = ut.modtime = time(NULL);
	if(utime(fname, &ut)==0)
120 121 122 123 124 125 126
		return(TRUE);

	/* create the file */
	if((file=nopen(fname,O_WRONLY|O_CREAT))<0)
		return(FALSE);
	close(file);
	return(TRUE);
rswindell's avatar
rswindell committed
127
}
128

129
BOOL fmutex(const char* fname, const char* text, long max_age)
130 131
{
	int file;
rswindell's avatar
rswindell committed
132
	time_t t;
133
#if !defined(NO_SOCKET_SUPPORT)
134
	char hostname[128];
135 136 137
	if(text==NULL && gethostname(hostname,sizeof(hostname))==0)
		text=hostname;
#endif
138

139 140 141 142
	if(max_age && (t=fdate(fname)) >= 0 && (time(NULL)-t) > max_age) {
		if(remove(fname)!=0)
			return(FALSE);
	}
143
	if((file=open(fname,O_CREAT|O_WRONLY|O_EXCL,DEFFILEMODE))<0)
144 145 146 147 148 149
		return(FALSE);
	if(text!=NULL)
		write(file,text,strlen(text));
	close(file);
	return(TRUE);
}