Annotation of sbbs/src/sbbs3/nopen.c, revision 1.1

1.1     ! root        1: /* nopen.c */
        !             2: 
        !             3: /* Network open functions (nopen and fnopen) */
        !             4: 
        !             5: /* $Id: nopen.c,v 1.19 2006/01/13 08:48:25 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 2006 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:        } 
        !            54:        else if((access&~(O_TEXT|O_BINARY))==O_RDONLY) 
        !            55:                share=SH_DENYWR;
        !            56:     else 
        !            57:                share=SH_DENYRW;
        !            58: 
        !            59: #if !defined(__unix__) /* Basically, a no-op on Unix anyway */
        !            60:        if(!(access&O_TEXT))
        !            61:                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:         if(count)
        !            66:             mswait(100);
        !            67:     return(file);
        !            68: }
        !            69: /****************************************************************************/
        !            70: /* This function performs an nopen, but returns a file stream with a buffer */
        !            71: /* allocated.                                                                                                                          */
        !            72: /****************************************************************************/
        !            73: FILE* fnopen(int* fd, const char* str, int access)
        !            74: {
        !            75:        char    mode[128];
        !            76:        int             file;
        !            77:        FILE *  stream;
        !            78: 
        !            79:     if((file=nopen(str,access))==-1)
        !            80:         return(NULL);
        !            81: 
        !            82:     if(fd!=NULL)
        !            83:         *fd=file;
        !            84: 
        !            85:     if(access&O_APPEND) {
        !            86:         if((access&O_RDWR)==O_RDWR)
        !            87:             strcpy(mode,"a+");
        !            88:         else
        !            89:             strcpy(mode,"a"); 
        !            90:        } else if(access&(O_TRUNC|O_WRONLY)) {
        !            91:                if((access&O_RDWR)==O_RDWR)
        !            92:                        strcpy(mode,"w+");
        !            93:                else
        !            94:                        strcpy(mode,"w");
        !            95:        } else {
        !            96:         if((access&O_RDWR)==O_RDWR)
        !            97:             strcpy(mode,"r+");
        !            98:         else
        !            99:             strcpy(mode,"r"); 
        !           100:        }
        !           101:     stream=fdopen(file,mode);
        !           102:     if(stream==NULL) {
        !           103:         close(file);
        !           104:         return(NULL); 
        !           105:        }
        !           106:     setvbuf(stream,NULL,_IOFBF,FNOPEN_BUF_SIZE);
        !           107:     return(stream);
        !           108: }
        !           109: 
        !           110: BOOL ftouch(const char* fname)
        !           111: {
        !           112:        int file;
        !           113: 
        !           114:        if((file=nopen(fname,O_WRONLY|O_CREAT))<0)
        !           115:                return(FALSE);
        !           116:        close(file);
        !           117: 
        !           118:        return(TRUE);
        !           119: }
        !           120: 
        !           121: BOOL fmutex(const char* fname, const char* text, long max_age)
        !           122: {
        !           123:        int file;
        !           124:        long t;
        !           125: #if !defined(NO_SOCKET_SUPPORT)
        !           126:        char hostname[128];
        !           127:        if(text==NULL && gethostname(hostname,sizeof(hostname))==0)
        !           128:                text=hostname;
        !           129: #endif
        !           130: 
        !           131:        if(max_age && (t=fdate(fname)) >= 0 && (time(NULL)-t) > max_age) {
        !           132:                if(remove(fname)!=0)
        !           133:                        return(FALSE);
        !           134:        }
        !           135:        if((file=open(fname,O_CREAT|O_WRONLY|O_EXCL,S_IREAD|S_IWRITE))<0)
        !           136:                return(FALSE);
        !           137:        if(text!=NULL)
        !           138:                write(file,text,strlen(text));
        !           139:        close(file);
        !           140:        return(TRUE);
        !           141: }

unix.superglobalmegacorp.com

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