Annotation of sbbs/sbbs3/nopen.c, revision 1.1.1.1

1.1       root        1: /* nopen.c */
                      2: 
                      3: /* Network open functions (nopen and fnopen) */
                      4: 
                      5: /* $Id: nopen.c,v 1.15 2004/12/24 07:53:52 rswindell Exp $ */
                      6: 
                      7: /****************************************************************************
                      8:  * @format.tab-size 4          (Plain Text/Source Code File Header)                    *
                      9:  * @format.use-tabs true       (see http://www.synchro.net/ptsc_hdr.html)              *
                     10:  *                                                                                                                                                     *
                     11:  * Copyright 2004 Rob Swindell - http://www.synchro.net/copyright.html         *
                     12:  *                                                                                                                                                     *
                     13:  * This program is free software; you can redistribute it and/or                       *
                     14:  * modify it under the terms of the GNU General Public License                         *
                     15:  * as published by the Free Software Foundation; either version 2                      *
                     16:  * of the License, or (at your option) any later version.                                      *
                     17:  * See the GNU General Public License for more details: gpl.txt or                     *
                     18:  * http://www.fsf.org/copyleft/gpl.html                                                                                *
                     19:  *                                                                                                                                                     *
                     20:  * Anonymous FTP access to the most recent released source is available at     *
                     21:  * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net     *
                     22:  *                                                                                                                                                     *
                     23:  * Anonymous CVS access to the development source and modification history     *
                     24:  * is available at cvs.synchro.net:/cvsroot/sbbs, example:                                     *
                     25:  * cvs -d :pserver:[email protected]:/cvsroot/sbbs login                       *
                     26:  *     (just hit return, no password is necessary)                                                     *
                     27:  * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src                *
                     28:  *                                                                                                                                                     *
                     29:  * For Synchronet coding style and modification guidelines, see                                *
                     30:  * http://www.synchro.net/source.html                                                                          *
                     31:  *                                                                                                                                                     *
                     32:  * You are encouraged to submit any modifications (preferably in Unix diff     *
                     33:  * format) via e-mail to [email protected]                                                                      *
                     34:  *                                                                                                                                                     *
                     35:  * Note: If this box doesn't appear square, then you need to fix your tabs.    *
                     36:  ****************************************************************************/
                     37: 
                     38: #include "sbbs.h"
                     39: #include "crc32.h"
                     40: 
                     41: /****************************************************************************/
                     42: /* Network open function. Opens all files DENYALL and retries LOOP_NOPEN    */
                     43: /* number of times if the attempted file is already open or denying access  */
                     44: /* for some other reason.      All files are opened in BINARY mode.                    */
                     45: /****************************************************************************/
                     46: int nopen(const char* str, int access)
                     47: {
                     48:        int file,share,count=0;
                     49: 
                     50:     if(access&O_DENYNONE) {
                     51:         share=SH_DENYNO;
                     52:         access&=~O_DENYNONE; }
                     53:     else if(access==O_RDONLY) share=SH_DENYWR;
                     54:     else share=SH_DENYRW;
                     55:        if(!(access&O_TEXT))
                     56:                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:         if(count)
                     60:             mswait(100);
                     61:     return(file);
                     62: }
                     63: /****************************************************************************/
                     64: /* This function performs an nopen, but returns a file stream with a buffer */
                     65: /* allocated.                                                                                                                          */
                     66: /****************************************************************************/
                     67: FILE* fnopen(int* fd, const char* str, int access)
                     68: {
                     69:        char    mode[128];
                     70:        int             file;
                     71:        FILE *  stream;
                     72: 
                     73:     if((file=nopen(str,access))==-1)
                     74:         return(NULL);
                     75: 
                     76:     if(fd!=NULL)
                     77:         *fd=file;
                     78: 
                     79:     if(access&O_APPEND) {
                     80:         if((access&O_RDWR)==O_RDWR)
                     81:             strcpy(mode,"a+");
                     82:         else
                     83:             strcpy(mode,"a"); 
                     84:        } else if(access&(O_TRUNC|O_WRONLY)) {
                     85:                if((access&O_RDWR)==O_RDWR)
                     86:                        strcpy(mode,"w+");
                     87:                else
                     88:                        strcpy(mode,"w");
                     89:        } else {
                     90:         if((access&O_RDWR)==O_RDWR)
                     91:             strcpy(mode,"r+");
                     92:         else
                     93:             strcpy(mode,"r"); 
                     94:        }
                     95:     stream=fdopen(file,mode);
                     96:     if(stream==NULL) {
                     97:         close(file);
                     98:         return(NULL); 
                     99:        }
                    100:     setvbuf(stream,NULL,_IOFBF,FNOPEN_BUF_SIZE);
                    101:     return(stream);
                    102: }
                    103: 
                    104: BOOL ftouch(const char* fname)
                    105: {
                    106:        int file;
                    107: 
                    108:        if((file=nopen(fname,O_WRONLY|O_CREAT))<0)
                    109:                return(FALSE);
                    110:        close(file);
                    111: 
                    112:        return(TRUE);
                    113: }
                    114: 
                    115: BOOL fmutex(const char* fname, const char* text)
                    116: {
                    117:        int file;
                    118: #if !defined(NO_SOCKET_SUPPORT)
                    119:        char hostname[128];
                    120:        if(text==NULL && gethostname(hostname,sizeof(hostname))==0)
                    121:                text=hostname;
                    122: #endif
                    123: 
                    124:        if((file=open(fname,O_CREAT|O_WRONLY|O_EXCL,S_IREAD|S_IWRITE))<0)
                    125:                return(FALSE);
                    126:        if(text!=NULL)
                    127:                write(file,text,strlen(text));
                    128:        close(file);
                    129:        return(TRUE);
                    130: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.