Annotation of researchv10no/lbin/mailx/myfopen.c, revision 1.1.1.1

1.1       root        1: #ident "@(#)myfopen.c  1.3 'attmail mail(1) command'"
                      2: #ident "@(#)mailx:myfopen.c    1.1"
                      3: /*     Copyright (c) 1988 AT&T */
                      4: /*       All Rights Reserved   */
                      5: 
                      6: /*     THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T     */
                      7: /*     The copyright notice above does not evidence any        */
                      8: /*     actual or intended publication of such source code.     */
                      9: 
                     10: #ident "@(#)mailx:myfopen.c    1.0"
                     11: 
                     12: #include "rcv.h"
                     13: 
                     14: #undef fopen
                     15: #undef fclose
                     16: 
                     17: /*
                     18:  * mailx -- a modified version of a University of California at Berkeley
                     19:  *     mail program
                     20:  *
                     21:  * Local version of fopen() and fclose(). These maintain a list of
                     22:  * file pointers which can be run down when we need to close
                     23:  * all files, such as before executing external commands.
                     24:  */
                     25: 
                     26: static NODE    *append();
                     27: static NODE    *del1();
                     28: static NODE    *getnode();
                     29: static NODE    *search();
                     30: 
                     31: static NODE *
                     32: getnode(fp)
                     33: FILE *fp;
                     34: {
                     35:        NODE *new;
                     36: 
                     37:        if ((new = (NODE *)malloc(sizeof(NODE))) == (NODE *)NULL) {
                     38:                (void) fputs("Cannot allocate node space\n", stderr);
                     39:                exit(3);
                     40:        }
                     41:        new->fp = fp;
                     42:        new->next = (NODE *)NULL;
                     43:        return(new);
                     44: }
                     45: 
                     46: static NODE *
                     47: search(fp)
                     48: FILE *fp;
                     49: {
                     50:        register NODE *tmp;
                     51:        
                     52:        for (tmp = fplist; tmp != (NODE *)NULL; tmp = tmp->next)
                     53:                if (tmp->fp == fp)
                     54:                        break;
                     55:        return( tmp != (NODE *)NULL ? tmp : NOFP);
                     56: }
                     57:        
                     58: static NODE *
                     59: append(fp)
                     60: FILE *fp;
                     61: {
                     62:        register NODE *new;
                     63: 
                     64:        if ((new = getnode(fp)) == (NODE *)NULL)
                     65:                return(NOFP);
                     66:        if (fplist == NOFP) {
                     67:                fplist = new;
                     68:        } else {
                     69:                new->next = curptr->next;
                     70:                curptr->next = new;
                     71:        }
                     72:        return(new);
                     73: }
                     74: 
                     75: static NODE *
                     76: del1(oldcur)
                     77: NODE *oldcur;
                     78: {
                     79:        register NODE *cur, *prev;
                     80: 
                     81:        for (prev = cur = fplist; cur != (NODE *)NULL; cur = cur->next) {
                     82:                if (cur == oldcur) {
                     83:                        if (cur == fplist) {
                     84:                                cur = fplist = cur->next;
                     85:                        } else {
                     86:                                prev->next = cur->next;
                     87:                                cur = prev->next ? prev->next : prev;
                     88:                        }
                     89:                        free(oldcur);
                     90:                        break;
                     91:                }
                     92:                prev = cur;
                     93:        }
                     94:        return(cur);
                     95: }
                     96: 
                     97: FILE *
                     98: my_fopen(file, mode)
                     99: char   *file, *mode;
                    100: {
                    101:        FILE *fp;
                    102: 
                    103:        if ((fp = fopen(file, mode)) == (FILE *)NULL) {
                    104:                fplist = NOFP;
                    105:                return(fp);
                    106:        } else {
                    107:                curptr = append(fp);
                    108:        }
                    109:        return(fp);
                    110: }
                    111: 
                    112: int
                    113: my_fclose(iop)
                    114: register FILE *iop;
                    115: {
                    116:        register NODE *cur;
                    117: 
                    118:        int ret = fclose(iop);
                    119:        if (fplist != NOFP) {
                    120:                cur = search(iop);
                    121:                cur = del1(cur);
                    122:        }
                    123:        return ret;
                    124: }

unix.superglobalmegacorp.com

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