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

1.1       root        1: /* nopen.c */
                      2: 
                      3: /* Network open functions (nopen and fnopen) */
                      4: 
1.1.1.2 ! root        5: /* $Id: nopen.c,v 1.27 2010/06/15 22:54:35 rswindell Exp $ */
1.1       root        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:  *                                                                                                                                                     *
1.1.1.2 ! root       11:  * Copyright 2010 Rob Swindell - http://www.synchro.net/copyright.html         *
1.1       root       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: /****************************************************************************/
1.1.1.2 ! root       42: /* Network open function. Opens all files DENYALL, DENYWRITE, or DENYNONE      */
        !            43: /* depending on access, and retries LOOP_NOPEN number of times if the          */
        !            44: /* attempted file is already open or denying access  for some other reason. */
        !            45: /* All files are opened in BINARY mode.                                                                                */
1.1       root       46: /****************************************************************************/
                     47: int nopen(const char* str, int access)
                     48: {
                     49:        int file,share,count=0;
                     50: 
                     51:     if(access&O_DENYNONE) {
                     52:         share=SH_DENYNO;
                     53:         access&=~O_DENYNONE; 
                     54:        } 
                     55:        else if((access&~(O_TEXT|O_BINARY))==O_RDONLY) 
                     56:                share=SH_DENYWR;
                     57:     else 
                     58:                share=SH_DENYRW;
                     59: 
                     60: #if !defined(__unix__) /* Basically, a no-op on Unix anyway */
                     61:        if(!(access&O_TEXT))
                     62:                access|=O_BINARY;
                     63: #endif
1.1.1.2 ! root       64:     while(((file=sopen(str,access,share,DEFFILEMODE))==-1)
1.1       root       65:         && (errno==EACCES || errno==EAGAIN || errno==EDEADLOCK) && count++<LOOP_NOPEN)
                     66:         if(count)
                     67:             mswait(100);
                     68:     return(file);
                     69: }
1.1.1.2 ! root       70: 
1.1       root       71: /****************************************************************************/
                     72: /* This function performs an nopen, but returns a file stream with a buffer */
                     73: /* allocated.                                                                                                                          */
                     74: /****************************************************************************/
                     75: FILE* fnopen(int* fd, const char* str, int access)
                     76: {
1.1.1.2 ! root       77:        char*   mode;
1.1       root       78:        int             file;
                     79:        FILE *  stream;
                     80: 
                     81:     if((file=nopen(str,access))==-1)
                     82:         return(NULL);
                     83: 
                     84:     if(fd!=NULL)
                     85:         *fd=file;
                     86: 
                     87:     if(access&O_APPEND) {
                     88:         if((access&O_RDWR)==O_RDWR)
1.1.1.2 ! root       89:             mode="a+";
1.1       root       90:         else
1.1.1.2 ! root       91:             mode="a"; 
1.1       root       92:        } else if(access&(O_TRUNC|O_WRONLY)) {
                     93:                if((access&O_RDWR)==O_RDWR)
1.1.1.2 ! root       94:                        mode="w+";
1.1       root       95:                else
1.1.1.2 ! root       96:                        mode="w";
1.1       root       97:        } else {
                     98:         if((access&O_RDWR)==O_RDWR)
1.1.1.2 ! root       99:             mode="r+";
1.1       root      100:         else
1.1.1.2 ! root      101:             mode="r"; 
1.1       root      102:        }
                    103:     stream=fdopen(file,mode);
                    104:     if(stream==NULL) {
                    105:         close(file);
                    106:         return(NULL); 
                    107:        }
                    108:     setvbuf(stream,NULL,_IOFBF,FNOPEN_BUF_SIZE);
                    109:     return(stream);
                    110: }
                    111: 
                    112: BOOL ftouch(const char* fname)
                    113: {
                    114:        int file;
1.1.1.2 ! root      115:        struct utimbuf ut;
        !           116: 
        !           117:        /* update the time stamp */
        !           118:        ut.actime = ut.modtime = time(NULL);
        !           119:        if(utime(fname, &ut)==0)
        !           120:                return(TRUE);
1.1       root      121: 
1.1.1.2 ! root      122:        /* create the file */
1.1       root      123:        if((file=nopen(fname,O_WRONLY|O_CREAT))<0)
                    124:                return(FALSE);
                    125:        close(file);
                    126:        return(TRUE);
                    127: }
                    128: 
                    129: BOOL fmutex(const char* fname, const char* text, long max_age)
                    130: {
                    131:        int file;
                    132:        long t;
                    133: #if !defined(NO_SOCKET_SUPPORT)
                    134:        char hostname[128];
                    135:        if(text==NULL && gethostname(hostname,sizeof(hostname))==0)
                    136:                text=hostname;
                    137: #endif
                    138: 
                    139:        if(max_age && (t=fdate(fname)) >= 0 && (time(NULL)-t) > max_age) {
                    140:                if(remove(fname)!=0)
                    141:                        return(FALSE);
                    142:        }
1.1.1.2 ! root      143:        if((file=open(fname,O_CREAT|O_WRONLY|O_EXCL,DEFFILEMODE))<0)
1.1       root      144:                return(FALSE);
                    145:        if(text!=NULL)
                    146:                write(file,text,strlen(text));
                    147:        close(file);
                    148:        return(TRUE);
                    149: }

unix.superglobalmegacorp.com

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