home *** CD-ROM | disk | FTP | other *** search
- /* ==( help/garbage.c )== */
- /* ----------------------------------------------- */
- /* Pro-C Copyright (C) 1988 - 1990 Vestronix Inc. */
- /* Modification to this source is not supported */
- /* by Vestronix Inc. */
- /* All Rights Reserved */
- /* ----------------------------------------------- */
- /* Written JPK 26-Sep-88 */
- /* Modified Geo 18-Jan-90 See comments below */
- /* ----------------------------------------------- */
- /* %W% (%H% %T%) */
-
- /*
- * Modifications
- *
- * 18-Jan-90 Geo - Allowed access for renfile for dirw()
- * 12-Dec-89 Geo - V2 version with variable lines
- * 25-Oct-89 Geo - 1.32 Merge
- *
- */
-
- /*
- * contains the routines that monitor and clean the garbage from
- * the help text file
- */
-
- /*
- * Routines in this file :
- *
- * static int renfile(char *, char *);
- * renames a file
- *
- * void put_null_rec(void);
- * forces an initial null record in index file
- *
- * int eat_garbage(char *);
- * cleans the fragmentation out of the help text file
- *
- */
-
- # include <stdio.h>
- # include <bench.h>
- # include <fileio.h>
- # include "help.h"
-
- PROTO (int renfile, (char *, char *));
- PROTO (long fsize, (int));
-
- /* This is required by dirw() as well */
-
- int renfile(old, new)
- char * old;
- char * new;
- {
- # ifdef UNIX
- char buffer[80];
-
- (void) sprintf(buffer, "/bin/mv %s %s", old, new);
- return(system(buffer));
-
- # else
-
- return(rename(old, new));
-
- # endif
- }
-
- void get_garbage_rec(garbage_ndx)
- struct help_ndx *garbage_ndx;
- {
- /* write total amount of garbage */
- get_rec(nfd, 0, (char *)garbage_ndx);
- }
-
- void put_garbage_rec(garbage_ndx)
- struct help_ndx *garbage_ndx;
- {
- /* write total amount of garbage */
- put_rec(nfd, 0, (char *)garbage_ndx);
- }
-
- int eat_garbage(file)
- char * file;
- {
- FILE *newt;
- int newi = -1;
- char oldhlp[128];
- char newhlp[128];
- char oldndx[128];
- char newndx[128];
- struct help_ndx h_new;
- int num = 1;
- int size = 0;
- int ch;
-
- get_garbage_rec(&h_new);
- if (h_new.size < fsize(fileno(hfptr))/5)
- return(TRUE);
-
- /* clean all the fragmentation out of the help file by copying the help */
- /* nodes from the old help file to a new one at newly calculated seek */
- /* positions */
-
- (void) strcpy(newndx, file);
- (void) strcat(newndx, ".onf");
- (void) strcpy(newhlp, file);
- (void) strcat(newhlp, ".ohf");
-
- (void) strcpy(oldndx, file);
- (void) strcat(oldndx, ".ndx");
- (void) strcpy(oldhlp, file);
- (void) strcat(oldhlp, ".hlp");
-
- openf(newndx, SH_OPENRWC, sizeof(struct help_ndx), &newi);
- if ((newt = fopen(newhlp, "w+")) == NULL)
- return(FALSE);
-
- /* Should be no Garbage on a new file */
- h_new.size = 0;
- h_new.seekpos = 0L;
- put_rec(newi, 0, (char *)&h_new);
-
- /* loop through existing help messages */
- while (read_index(num) != FALSE)
- {
- h_new.size = h_ndx.size;
- h_new.seekpos = fsize(fileno(newt));
-
- /* Help text */
- fseek(hfptr, h_ndx.seekpos, SEEK_SET);
- fseek(newt, h_new.seekpos, SEEK_SET);
-
- /* copy the help stuff to the new file */
- while(((ch = getc(hfptr)) != EOF) && ch != '\f'/* &&
- (size < h_ndx.size + HSTRSIZE) */)
- {
- putc(ch, newt);
- size++;
- }
- putc('\f', newt);
- fflush(newt);
-
- /* write the new index record */
- put_rec(newi, num, (char *)&h_new);
-
- num++;
- size = 0;
- }
- /* close the files */
- closef(nfd);
- closef(newi);
- nfd = -1;
- (void) fclose(hfptr);
- (void) fclose(newt);
-
- /* erase the old files */
- unlink(oldhlp);
- unlink(oldndx);
-
- /* rename the new files to that of the old files */
- (void) renfile(newhlp, oldhlp);
- (void) renfile(newndx, oldndx);
-
- /* reopen the updated help files */
- openf(oldndx, SH_OPENRWNC, sizeof(struct help_ndx), &nfd);
- lock_index_file(WLOK);
- if ((hfptr = fopen(oldhlp, "r+")) == NULL)
- return(FALSE);
-
- return(TRUE);
- }
-