Annotation of coherent/g/usr/bin/uuinstall/delete.c, revision 1.1

1.1     ! root        1: /* delete.c: given a filename, the number of lines before the entry
        !             2:  *     to delete, and the number of lines in the entry, delete the
        !             3:  *     entry by copying the lines before the entry to a temp file,
        !             4:  *     skipping the lines the entry we wish to delete occuppies,
        !             5:  *     then copying the remaining lines to the temp file. Delete
        !             6:  *     the old file and link the temp file to the newly modified file.
        !             7:  *     A similar scheme is used to add a modified entry.
        !             8:  */
        !             9: 
        !            10: #include "uuinstall.h"
        !            11: 
        !            12: delete_entry(fname,before,during,insert_flag)
        !            13: char * fname;                                  /* file name to work with */
        !            14: int before, during;
        !            15: int insert_flag;                               /* insert a modified entry? */
        !            16: {
        !            17:        FILE *filefd;
        !            18:        FILE *tmpfd;
        !            19:        FILE *newfd;
        !            20:        int x;
        !            21:        char *env;
        !            22:        char workstring[88];
        !            23:        char syscmd[65];                /* used to build system command to edit */
        !            24:        char b;
        !            25: 
        !            26:        extern char * getenv();
        !            27: 
        !            28:        wclear(portwin);
        !            29: 
        !            30:        /* open file for reading */
        !            31:        if ((filefd = fopen(fname,"r")) == NULL){
        !            32:                mvwaddstr(portwin,12,29,"Error opening file for reading!");
        !            33:                wrefresh(portwin);
        !            34:                sleep(1);
        !            35:                wclear(portwin);
        !            36:                wrefresh(portwin);
        !            37:                return;
        !            38:        }
        !            39: 
        !            40:        /* open edit file for writing */
        !            41:        if(insert_flag){
        !            42:                if ((newfd = fopen(UUEDIT,"w")) == NULL){
        !            43:                        mvwaddstr(portwin,12,29,"Error opening file for reading!");
        !            44:                        wrefresh(portwin);
        !            45:                        sleep(1);
        !            46:                        wclear(portwin);
        !            47:                        wrefresh(portwin);
        !            48:                        fclose(filefd);
        !            49:                        return;
        !            50:                }
        !            51:        }
        !            52:        /* open tmp file for writing */
        !            53:        if ((tmpfd = fopen(UUTMP,"w")) == NULL){
        !            54:                mvwaddstr(portwin,12,29,"Error opening file for writing!");
        !            55:                wrefresh(portwin);
        !            56:                sleep(1);
        !            57:                wclear(portwin);
        !            58:                wrefresh(portwin);
        !            59:                fclose(filefd);
        !            60:                fclose(tmpfd);
        !            61:                return;
        !            62:        }
        !            63: 
        !            64:        wmove(portwin,12,0);
        !            65:        wclrtoeol(portwin);     
        !            66:        if(!insert_flag){
        !            67:                mvwaddstr(portwin,12,29,"Updating...");
        !            68:        }else{
        !            69:                mvwaddstr(portwin,12,29,"Standby ...");
        !            70:        }
        !            71:        wrefresh(portwin);
        !            72: 
        !            73:        /* skip the lines we don't want to delete */
        !            74: 
        !            75:        for(x = 0 ; x < before ; x++){
        !            76:                fgets(workstring, sizeof(workstring) -1, filefd);
        !            77:                fputs(workstring, tmpfd);
        !            78:                fflush(tmpfd);
        !            79:        }
        !            80: 
        !            81:        /* now read the entry to delete, but don't copy it to the tmp file...
        !            82:         * UNLESS we're going to edit an entry, in which case, we write the
        !            83:         * indicated entry to another temp file and then invoke an editor on it.
        !            84:         */
        !            85: 
        !            86:        during++;
        !            87: 
        !            88:        for(x = 0; x < during ; x++){
        !            89:                fgets(workstring, sizeof(workstring) -1, filefd);
        !            90: 
        !            91:                /* write to edit file */
        !            92:                if(insert_flag){
        !            93:                        fputs(workstring,newfd);
        !            94:                }
        !            95:        }
        !            96: 
        !            97:        strcpy(workstring,"");
        !            98: 
        !            99: /* if edit, get the EDITOR environment variable. If there is none, use emacs */
        !           100: 
        !           101:        if (insert_flag){
        !           102:                fclose(newfd);
        !           103: 
        !           104:                /* build system command to edit temp editfile */
        !           105:                if( (env = getenv("EDITOR")) != NULL){
        !           106:                        sprintf(syscmd,"%s %s",env, UUEDIT);
        !           107:                }else{
        !           108:                        sprintf(syscmd,"%s %s","/usr/bin/me", UUEDIT);
        !           109:                }
        !           110: 
        !           111:                /* invoke editor */
        !           112:                system(syscmd);
        !           113: 
        !           114:        /* if we editted an entry, we need to insert the tmp editfile now. 
        !           115:         * Prompt to really save changes, re-open the edit file and
        !           116:         * copy its contents to the temp file.
        !           117:         */
        !           118: 
        !           119:                wclear(portwin);
        !           120:                mvwaddstr(portwin,12,23,"Do you wish to save changes? (y/n)");
        !           121:                wrefresh(portwin);
        !           122:                do{
        !           123:                        b = ' ';
        !           124:                        b = wgetch(portwin);
        !           125:                }
        !           126:                while ((b != 'y') && (b != 'n'));
        !           127: 
        !           128:                if(b == 'y'){
        !           129:                        if((newfd = fopen(UUEDIT,"r")) == NULL){
        !           130:                                wclear(portwin);
        !           131:                                mvwaddstr(portwin,12,16,"Failed to open temp file to get modified entry!");
        !           132:                                mvwaddstr(portwin,14,20,"Leaving all files in original condition.");
        !           133:                                wrefresh(portwin);
        !           134:                                sleep(2);
        !           135:                                fclose(tmpfd);
        !           136:                                fclose(filefd);
        !           137:                                return;
        !           138:                        }
        !           139: 
        !           140:                        while(fgets(workstring,sizeof(workstring) -1,newfd) != NULL){
        !           141:                                fputs(workstring, tmpfd);
        !           142:                                fflush(tmpfd);
        !           143:                        }
        !           144:                        fclose(newfd);
        !           145:                }else{
        !           146:                        /* user doesn't want to save changes, so close the
        !           147:                         * files we opened and delete the temp files.
        !           148:                         */
        !           149: 
        !           150:                        fclose(tmpfd);
        !           151:                        fclose(filefd);
        !           152:                        unlink(UUTMP);  /* don't care if this fails, it may */
        !           153:                        unlink(UUEDIT); /* prove useful to keep these files */
        !           154:                        return;         /* around anyways. */
        !           155:                }
        !           156:        }
        !           157: 
        !           158:        /* now copy the rest of the file */
        !           159:        while (fgets(workstring,sizeof(workstring)-1,filefd) != NULL){
        !           160:                fputs(workstring, tmpfd);
        !           161:                fflush(tmpfd);
        !           162:        }
        !           163: 
        !           164:        /* close the files */
        !           165: 
        !           166:        fclose(filefd);
        !           167:        fclose(tmpfd);
        !           168: 
        !           169:        /* now unlink the old and link in the new! */
        !           170: 
        !           171:        if (unlink(fname) == -1){
        !           172:                wclear(portwin);
        !           173:                mvwaddstr(portwin,12,28,"Error deleting old file!");
        !           174:                wmove(portwin,13,15);
        !           175:                wprintw(portwin,"Leaving old file intact. New data saved to %s.",UUTMP);
        !           176:                wrefresh(portwin);
        !           177:                sleep(2);
        !           178:                return;
        !           179:        }
        !           180: 
        !           181:        if(link(UUTMP,fname) == -1){
        !           182:                wclear(portwin);
        !           183:                wmove(portwin,12,15);
        !           184:                wprintw(portwin,"Error writing data to %s. Data is saved in %s!",fname, UUTMP);
        !           185:                wrefresh(portwin);
        !           186:                sleep(2);
        !           187:                return;
        !           188:        }
        !           189: 
        !           190:        /* We don't care about the tmp file being unlinked, as there is no
        !           191:         * code here that appends to a tmp file.
        !           192:         */
        !           193: 
        !           194:        unlink(UUTMP);
        !           195: 
        !           196:        wclear(portwin);
        !           197:        wrefresh(portwin);
        !           198: }

unix.superglobalmegacorp.com

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