LCOV - code coverage report
Current view: top level - omega-rpg-0.90-pa9 - file.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 157 413 38.0 %
Date: 2017-09-08 22:00:26 Functions: 9 24 37.5 %
Branches: 36 157 22.9 %

           Branch data     Line data    Source code
       1                 :            : /* omega (c) 1987,1988,1989 by Laurence Raphael Brothers */
       2                 :            : /* file.c */
       3                 :            : /* functions with file access in them. Also some direct calls to
       4                 :            :    curses functions */
       5                 :            : 
       6                 :            : #ifdef MSDOS_SUPPORTED_ANTIQUE
       7                 :            : # include "curses.h"
       8                 :            : #else
       9                 :            : # ifdef AMIGA
      10                 :            : #  include <curses210.h>
      11                 :            : # elif defined(USE_OPCURSES)
      12                 :            : #  include "../opcurses/curses.h"
      13                 :            : # else
      14                 :            : #  include <curses.h>
      15                 :            : # endif
      16                 :            : # include <sys/types.h>
      17                 :            : # include <sys/stat.h>
      18                 :            : # include <unistd.h>
      19                 :            : # include <sys/file.h>
      20                 :            : # include <fcntl.h>
      21                 :            : # include <errno.h>
      22                 :            : #endif
      23                 :            : 
      24                 :            : #include "glob.h"
      25                 :            : 
      26                 :         13 : FILE *checkfopen(char *filestring, char *optionstring)
      27                 :            : {
      28                 :            :   FILE *fd;
      29                 :            :   char response;
      30                 :            : 
      31                 :         13 :   change_to_game_perms();
      32                 :         13 :   fd = fopen(filestring,optionstring);
      33                 :         13 :   clearmsg();
      34         [ -  + ]:         13 :   while (fd == NULL) {
      35                 :          0 :     print3("Warning! Error opening file:");
      36                 :          0 :     nprint3(filestring);
      37                 :          0 :     print1(" Abort or Retry? [ar] ");
      38                 :          0 :     do response = (char) mcigetc();
      39 [ #  # ][ #  # ]:          0 :     while ((response != 'a') && (response != 'r'));
      40         [ #  # ]:          0 :     if (response == 'r') fd = fopen(filestring,optionstring);
      41                 :            :     else {
      42                 :          0 :       print2("Sorry 'bout that.... Saving character, then quitting.");
      43                 :          0 :       morewait();
      44                 :          0 :       save(optionp(COMPRESS_OPTION), TRUE);
      45                 :          0 :       endgraf();
      46                 :          0 :       exit(0);
      47                 :            :     }
      48                 :            :   }
      49                 :         13 :   return(fd);
      50                 :            : }
      51                 :            : 
      52                 :          0 : void commandlist(void)
      53                 :            : {
      54                 :          0 :   strcpy(Str1,Omegalib);
      55         [ #  # ]:          0 :   if (Current_Environment == E_COUNTRYSIDE)
      56                 :          0 :     strcat(Str1,"help13.txt");
      57                 :          0 :   else strcat(Str1,"help12.txt");
      58                 :          0 :   displayfile(Str1);
      59                 :          0 :   xredraw();
      60                 :          0 : }
      61                 :            : 
      62                 :            : 
      63                 :          0 : void user_intro(void)
      64                 :            : {
      65                 :          0 :   strcpy(Str1,Omegalib);
      66                 :          0 :   strcat(Str1,"intro.txt");
      67                 :          0 :   displaycryptfile(Str1);
      68                 :          0 :   xredraw();
      69                 :          0 : }
      70                 :            : 
      71                 :          0 : void show_license(void)
      72                 :            : {
      73                 :          0 :   strcpy(Str1,Omegalib);
      74                 :          0 :   strcat(Str1,"license.txt");
      75                 :          0 :   displayfile(Str1);
      76                 :          0 :   xredraw();
      77                 :          0 : }
      78                 :            : 
      79                 :            : 
      80                 :            : 
      81                 :          0 : void abyss_file(void)
      82                 :            : {
      83                 :          0 :   strcpy(Str1,Omegalib);
      84                 :          0 :   strcat(Str1,"abyss.txt");
      85                 :          0 :   displaycryptfile(Str1);
      86                 :          0 : }
      87                 :            : 
      88                 :            : 
      89                 :            : 
      90                 :            : 
      91                 :          0 : void inv_help(void)
      92                 :            : {
      93                 :          0 :   strcpy(Str1,Omegalib);
      94                 :          0 :   strcat(Str1,"help3.txt");
      95                 :          0 :   displayfile(Str1);
      96                 :          0 :   xredraw();
      97                 :          0 : }
      98                 :            : 
      99                 :            : 
     100                 :            : 
     101                 :          0 : void combat_help(void)
     102                 :            : {
     103                 :          0 :   strcpy(Str1,Omegalib);
     104                 :          0 :   strcat(Str1,"help5.txt");
     105                 :          0 :   displayfile(Str1);
     106                 :          0 :   menuclear();
     107                 :          0 : }
     108                 :            : 
     109                 :            : 
     110                 :            : 
     111                 :            : 
     112                 :          0 : void cityguidefile(void)
     113                 :            : {
     114                 :          0 :   strcpy(Str1,Omegalib);
     115                 :          0 :   strcat(Str1,"scroll2.txt");
     116                 :          0 :   displaycryptfile(Str1);
     117                 :          0 :   xredraw();
     118                 :          0 : }
     119                 :            : 
     120                 :            : 
     121                 :          0 : void wishfile(void)
     122                 :            : {
     123                 :          0 :   strcpy(Str1,Omegalib);
     124                 :          0 :   strcat(Str1,"scroll3.txt");
     125                 :          0 :   displaycryptfile(Str1);
     126                 :          0 :   xredraw();
     127                 :          0 : }
     128                 :            : 
     129                 :          0 : void adeptfile(void)
     130                 :            : {
     131                 :          0 :   strcpy(Str1,Omegalib);
     132                 :          0 :   strcat(Str1,"scroll4.txt");
     133                 :          0 :   displaycryptfile(Str1);
     134                 :          0 :   xredraw();
     135                 :          0 : }
     136                 :            : 
     137                 :          0 : void theologyfile(void)
     138                 :            : {
     139                 :          0 :   strcpy(Str1,Omegalib);
     140                 :          0 :   strcat(Str1,"scroll1.txt");
     141                 :          0 :   displaycryptfile(Str1);
     142                 :          0 :   xredraw();
     143                 :          0 : }
     144                 :            : 
     145                 :            : 
     146                 :          1 : void showmotd(void)
     147                 :            : {
     148                 :          1 :   strcpy(Str1,Omegalib);
     149                 :          1 :   strcat(Str1,"motd.txt");
     150                 :          1 :   displayfile(Str1);
     151                 :          1 : }
     152                 :            : 
     153                 :            : 
     154                 :          1 : void lock_score_file(void)
     155                 :            : {
     156                 :            : #ifndef MSDOS
     157                 :            :   int lock;
     158                 :            :   int thispid;
     159                 :          1 :   int lastpid = 0;
     160                 :          1 :   int attempts = 0;
     161                 :            : 
     162                 :            :   FILE * lockfile;
     163                 :            : 
     164                 :          1 :   strcpy(Str1,Omegavar);
     165                 :          1 :   strcat(Str1,"omega.hi.lock");
     166                 :            : 
     167                 :            :   do
     168                 :            :     {
     169                 :            :       /* create lock file */
     170                 :          1 :       lock = open(Str1, O_WRONLY | O_CREAT | O_EXCL, 0600);
     171 [ -  + ][ #  # ]:          1 :       if (lock < 0 && errno == EEXIST)
     172                 :            :         {
     173                 :          0 :           lockfile = fopen(Str1, "rb");
     174         [ #  # ]:          0 :           if (lockfile)
     175                 :            :             {
     176                 :          0 :               fscanf(lockfile, "%d", &thispid);
     177                 :          0 :               fclose(lockfile);
     178         [ #  # ]:          0 :               if (thispid != lastpid)
     179                 :          0 :                 attempts = 0;
     180                 :          0 :               lastpid = thispid;
     181                 :            :             }
     182                 :            : 
     183                 :          0 :           attempts++;
     184                 :            :           /* assume that lock file has been abandoned */
     185         [ #  # ]:          0 :           if (attempts > 10)
     186                 :            :             {
     187                 :            :               /* so we unlink it ourselves - ugly...    */
     188                 :          0 :               unlink(Str1);
     189                 :            :             }
     190                 :            :           else
     191                 :          0 :             sleep(2);
     192                 :            :         }
     193         [ -  + ]:          1 :       else if (lock < 0)
     194                 :            :         {
     195                 :            :           /* oops - something very wrong */
     196                 :          0 :           return;
     197                 :            :         }
     198                 :            :     }
     199         [ -  + ]:          1 :   while (lock < 0);
     200                 :            : 
     201                 :          1 :   sprintf(Str1, "%d", getpid());
     202                 :          1 :   write(lock, Str1, strlen(Str1));
     203                 :          1 :   close(lock);
     204                 :            : #endif
     205                 :            : }
     206                 :            : 
     207                 :          1 : void unlock_score_file(void)
     208                 :            : {
     209                 :            : #ifndef MSDOS
     210                 :          1 :     strcpy(Str1,Omegavar);
     211                 :          1 :     strcat(Str1,"omega.hi.lock");
     212                 :          1 :     unlink(Str1);
     213                 :            : #endif
     214                 :          1 : }
     215                 :            : 
     216                 :          1 : void showscores(void)
     217                 :            : {
     218                 :            :   FILE *fd;
     219                 :            :   int i;
     220                 :            : 
     221                 :          1 :   lock_score_file();
     222                 :          1 :   strcpy(Str1,Omegavar);
     223                 :          1 :   strcat(Str1,"omega.hi");
     224                 :          1 :   fd = checkfopen(Str1,"rb");
     225                 :          1 :   filescanstring(fd,Hiscorer);
     226                 :          1 :   filescanstring(fd,Hidescrip);
     227                 :          1 :   fscanf(fd,"%ld %d %d\n",&Hiscore,&Hilevel,&Hibehavior);
     228         [ +  + ]:          7 :   for(i=1;i<7;i++) {
     229                 :          6 :     filescanstring(fd,Priest[i]);
     230                 :          6 :     fscanf(fd,"%d %d\n",&(Priestlevel[i]),&(Priestbehavior[i]));
     231                 :            :   }
     232                 :          1 :   filescanstring(fd,Shadowlord);
     233                 :          1 :   fscanf(fd,"%d %d\n",&Shadowlordlevel,&Shadowlordbehavior);
     234                 :          1 :   filescanstring(fd,Commandant);
     235                 :          1 :   fscanf(fd,"%d %d\n",&Commandantlevel,&Commandantbehavior);
     236                 :          1 :   filescanstring(fd,Archmage);
     237                 :          1 :   fscanf(fd,"%d %d\n",&Archmagelevel,&Archmagebehavior);
     238                 :          1 :   filescanstring(fd,Prime);
     239                 :          1 :   fscanf(fd,"%d %d\n",&Primelevel,&Primebehavior);
     240                 :          1 :   filescanstring(fd,Champion);
     241                 :          1 :   fscanf(fd,"%d %d\n",&Championlevel,&Championbehavior);
     242                 :          1 :   filescanstring(fd,Duke);
     243                 :          1 :   fscanf(fd,"%d %d\n",&Dukelevel,&Dukebehavior);
     244                 :          1 :   filescanstring(fd,Chaoslord);
     245                 :          1 :   fscanf(fd,"%d %d %d\n",&Chaoslordlevel,&Chaos,&Chaoslordbehavior);
     246                 :          1 :   filescanstring(fd,Lawlord);
     247                 :          1 :   fscanf(fd,"%d %d %d\n",&Lawlordlevel,&Law,&Lawlordbehavior);
     248                 :          1 :   filescanstring(fd,Justiciar);
     249                 :          1 :   fscanf(fd,"%d %d\n",&Justiciarlevel,&Justiciarbehavior);
     250                 :          1 :   filescanstring(fd,Grandmaster);
     251                 :          1 :   fscanf(fd,"%d %d\n",&Grandmasterlevel,&Grandmasterbehavior);
     252                 :          1 :   fclose(fd);
     253                 :          1 :   unlock_score_file();
     254                 :          1 :   clear();
     255                 :          1 :   printw("High Score: %ld",Hiscore);
     256                 :          1 :   printw(", by %s (%s)",Hiscorer,levelname(Hilevel));
     257                 :          1 :   printw("\n%s\n",Hidescrip);
     258                 :          1 :   printw("\nLord of Chaos: %s (%s)",Chaoslord,levelname(Chaoslordlevel));
     259                 :          1 :   printw("\nLord of Law: %s (%s)",Lawlord,levelname(Lawlordlevel));
     260                 :          1 :   printw("\n\nDuke of Rampart:              ");
     261                 :          1 :   printw("%s (%s)",Duke,levelname(Dukelevel));
     262                 :          1 :   printw("\nJusticiar:                    ");
     263                 :          1 :   printw("%s (%s)",Justiciar,levelname(Justiciarlevel));
     264                 :          1 :   printw("\nCommandant:                   ");
     265                 :          1 :   printw("%s (%s)",Commandant,levelname(Commandantlevel));
     266                 :          1 :   printw("\nChampion:                     ");
     267                 :          1 :   printw("%s (%s)",Champion,levelname(Championlevel));
     268                 :          1 :   printw("\nArchmage:                     ");
     269                 :          1 :   printw("%s (%s)",Archmage,levelname(Archmagelevel));
     270                 :          1 :   printw("\nPrime Sorceror:               ");
     271                 :          1 :   printw("%s (%s)",Prime,levelname(Primelevel));
     272                 :          1 :   printw("\nShadowlord:                   ");
     273                 :          1 :   printw("%s (%s)",Shadowlord,levelname(Shadowlordlevel));
     274                 :          1 :   printw("\n\nHigh Priests:");
     275                 :          1 :   printw("\n of Odin:                     ");
     276                 :          1 :   printw("%s (%s)",Priest[ODIN],levelname(Priestlevel[ODIN]));
     277                 :          1 :   printw("\n of Set:                      ");
     278                 :          1 :   printw("%s (%s)",Priest[SET],levelname(Priestlevel[SET]));
     279                 :          1 :   printw("\n of Athena:                   ");
     280                 :          1 :   printw("%s (%s)",Priest[ATHENA],levelname(Priestlevel[ATHENA]));
     281                 :          1 :   printw("\n of Hecate:                   ");
     282                 :          1 :   printw("%s (%s)",Priest[HECATE],levelname(Priestlevel[HECATE]));
     283                 :          1 :   printw("\n of the Lords of Destiny:     ");
     284                 :          1 :   printw("%s (%s)",Priest[DESTINY],levelname(Priestlevel[DESTINY]));
     285                 :          1 :   printw("\nThe ArchDruid:                ");
     286                 :          1 :   printw("%s (%s)",Priest[DRUID],levelname(Priestlevel[DRUID]));
     287                 :          1 :   printw("\nTholian Grandmaster:          ");
     288                 :          1 :   printw("%s (%s)",Grandmaster,levelname(Grandmasterlevel));
     289                 :          1 :   printw("\n\nHit any key to continue.");
     290                 :          1 :   refresh();
     291                 :          1 :   wgetch(stdscr);
     292                 :          1 :   clear_screen();
     293                 :          1 : }
     294                 :            : 
     295                 :            : /* Writes a new high-score file, with the nominated npc as whatever it is */
     296                 :            : /* in this particular game, but the others as they appear in the file. */
     297                 :          0 : void save_hiscore_npc(int npc)
     298                 :            : {
     299                 :            :   FILE *infile, *outfile;
     300                 :            :   char buffer[80];
     301                 :            :   int i;
     302                 :            : 
     303         [ #  # ]:          0 :   if (gamestatusp(CHEATED))
     304                 :          0 :       return;
     305                 :          0 :   lock_score_file();
     306                 :          0 :   strcpy(Str1,Omegavar);
     307                 :          0 :   strcat(Str1,"omega.hi");
     308                 :          0 :   infile = checkfopen(Str1,"rb");
     309                 :          0 :   strcpy(Str2,Omegavar);
     310                 :            : #ifdef MSDOS
     311                 :            :   strcat(Str2,"omegahi.new"); /* stupid 8.3 msdos filename limit */
     312                 :            : #else
     313                 :          0 :   strcat(Str2,"omega.hi.new");
     314                 :            : #endif
     315                 :          0 :   outfile = checkfopen(Str2,"wb");
     316         [ #  # ]:          0 :   for (i = 0; i < 17; i++) {
     317         [ #  # ]:          0 :     if (npc == i) {
     318   [ #  #  #  #  :          0 :       switch (i) {
          #  #  #  #  #  
             #  #  #  # ]
     319                 :            :         case 0:
     320                 :          0 :           fprintf(outfile,"%s\n%s\n%ld %d %d\n",Hiscorer, Hidescrip, Hiscore,
     321                 :            :             Hilevel, Hibehavior);
     322                 :          0 :           break;
     323                 :            :         case 1: case 2: case 3: case 4: case 5: case 6:
     324                 :          0 :           fprintf(outfile,"%s\n%d %d\n", Priest[i], Priestlevel[i],
     325                 :            :             Priestbehavior[i]);
     326                 :          0 :           break;
     327                 :            :         case 7:
     328                 :          0 :           fprintf(outfile,"%s\n%d %d\n", Shadowlord, Shadowlordlevel,
     329                 :            :             Shadowlordbehavior);
     330                 :          0 :           break;
     331                 :            :         case 8:
     332                 :          0 :           fprintf(outfile,"%s\n%d %d\n", Commandant, Commandantlevel,
     333                 :            :             Commandantbehavior);
     334                 :          0 :           break;
     335                 :            :         case 9:
     336                 :          0 :           fprintf(outfile,"%s\n%d %d\n", Archmage, Archmagelevel,
     337                 :            :             Archmagebehavior);
     338                 :          0 :           break;
     339                 :            :         case 10:
     340                 :          0 :           fprintf(outfile,"%s\n%d %d\n", Prime, Primelevel, Primebehavior);
     341                 :          0 :           break;
     342                 :            :         case 11:
     343                 :          0 :           fprintf(outfile,"%s\n%d %d\n", Champion, Championlevel,
     344                 :            :             Championbehavior);
     345                 :          0 :           break;
     346                 :            :         case 12:
     347                 :          0 :           fprintf(outfile,"%s\n%d %d\n", Duke, Dukelevel, Dukebehavior);
     348                 :          0 :           break;
     349                 :            :         case 13:
     350                 :          0 :           fprintf(outfile,"%s\n%d %d %d\n",Chaoslord, Chaoslordlevel, Chaos,
     351                 :            :             Chaoslordbehavior);
     352                 :          0 :           break;
     353                 :            :         case 14:
     354                 :          0 :           fprintf(outfile,"%s\n%d %d %d\n", Lawlord, Lawlordlevel, Law,
     355                 :            :             Lawlordbehavior);
     356                 :          0 :           break;
     357                 :            :         case 15:
     358                 :          0 :           fprintf(outfile,"%s\n%d %d\n", Justiciar, Justiciarlevel,
     359                 :            :             Justiciarbehavior);
     360                 :          0 :           break;
     361                 :            :         case 16:
     362                 :          0 :           fprintf(outfile,"%s\n%d %d\n", Grandmaster, Grandmasterlevel,
     363                 :            :             Grandmasterbehavior);
     364                 :          0 :           break;
     365                 :            :       }
     366                 :            :     }
     367         [ #  # ]:          0 :     if (i == 0) {
     368                 :          0 :       fgets(buffer, 80, infile);
     369         [ #  # ]:          0 :       if (i != npc)
     370                 :          0 :         fputs(buffer, outfile);
     371                 :            :     }
     372                 :          0 :     fgets(buffer, 80, infile);
     373         [ #  # ]:          0 :     if (i != npc)
     374                 :          0 :       fputs(buffer, outfile);
     375                 :          0 :     fgets(buffer, 80, infile);
     376         [ #  # ]:          0 :     if (i != npc)
     377                 :          0 :       fputs(buffer, outfile);
     378                 :            :   }
     379                 :          0 :   fclose(infile);
     380                 :          0 :   fclose(outfile);
     381                 :          0 :   chmod(Str2, 0664);
     382                 :          0 :   unlink(Str1);
     383                 :            : #if defined(MSDOS) || defined(AMIGA)
     384                 :            :   rename(Str2, Str1);
     385                 :            : #else
     386                 :          0 :   link(Str2, Str1);
     387                 :          0 :   unlink(Str2); /* renames, but sys-V doesn't have rename()... */
     388                 :            : #endif
     389                 :          0 :   unlock_score_file();
     390                 :            : }
     391                 :            : 
     392                 :          0 : void checkhigh(char *descrip, int behavior)
     393                 :            : {
     394                 :            :   long points;
     395                 :            : 
     396         [ #  # ]:          0 :   if (FixedPoints > 0) points = FixedPoints;
     397                 :          0 :   else points = calc_points();
     398         [ #  # ]:          0 :   if (!gamestatusp(CHEATED)) {
     399         [ #  # ]:          0 :     if (Hiscore < points) {
     400                 :          0 :       strcpy(Hiscorer, Player.name);
     401                 :          0 :       strcpy(Hidescrip, descrip);
     402                 :          0 :       Hiscore = points;
     403                 :          0 :       Hilevel = Player.level;
     404                 :          0 :       Hibehavior = behavior;
     405                 :          0 :       save_hiscore_npc(0);
     406                 :          0 :       mprint("Yow! A new high score!");
     407                 :          0 :       morewait();
     408                 :            :     }
     409         [ #  # ]:          0 :     if (Player.alignment < Chaos) {
     410                 :          0 :       strcpy(Chaoslord, Player.name);
     411                 :          0 :       Chaoslordlevel = Player.level;
     412                 :          0 :       Chaos = Player.alignment;
     413                 :          0 :       Chaoslordbehavior = behavior;
     414                 :          0 :       save_hiscore_npc(13);
     415                 :          0 :       mprint("Criminy! A new Lord of Chaos!");
     416                 :          0 :       morewait();
     417                 :            :     }
     418         [ #  # ]:          0 :     if (Player.alignment > Law) {
     419                 :          0 :       strcpy(Lawlord, Player.name);
     420                 :          0 :       Lawlordlevel = Player.level;
     421                 :          0 :       Law = Player.alignment;
     422                 :          0 :       Lawlordbehavior = behavior;
     423                 :          0 :       save_hiscore_npc(14);
     424                 :          0 :       mprint("Gosh! A new Lord of Law!");
     425                 :          0 :       morewait();
     426                 :            :     }
     427                 :            :   }
     428                 :          0 : }
     429                 :            : 
     430                 :          0 : void extendlog(char *descrip, int lifestatus)
     431                 :            : {
     432                 :            :   FILE *fd;
     433                 :            :   int npcbehavior;
     434                 :            : 
     435 [ #  # ][ #  # ]:          0 :   if ((Player.level > 0) && (! gamestatusp(CHEATED))) {
     436                 :          0 :     change_to_game_perms();
     437                 :          0 :     npcbehavior=fixnpc(lifestatus);
     438                 :          0 :     checkhigh(descrip,npcbehavior);
     439                 :          0 :     strcpy(Str1,Omegavar);
     440                 :          0 :     strcat(Str1,"omega.log");
     441                 :          0 :     fd = checkfopen(Str1,"a");
     442                 :          0 :     fprintf(fd, " %d %d %d %s\n", lifestatus, Player.level, npcbehavior,
     443                 :            :       Player.name);
     444                 :          0 :     fclose(fd);
     445                 :            :   }
     446                 :          0 : }
     447                 :            : 
     448                 :            : 
     449                 :            : 
     450                 :            : #ifndef MSDOS
     451                 :            : /* reads a string from a file. If it is a line with more than 80 char's,
     452                 :            :    then remainder of line to \n is consumed */
     453                 :         30 : void filescanstring(FILE *fd, char *fstr)
     454                 :            : {
     455                 :         30 :   int i= -1;
     456                 :         30 :   int byte='x';
     457 [ +  - ][ +  + ]:        306 :   while ((i<80) && (byte != '\n') && (byte != EOF)) {
                 [ +  - ]
     458                 :        276 :     i++;
     459                 :        276 :     byte=fgetc(fd);
     460                 :        276 :     fstr[i] = byte;
     461                 :            :   } 
     462         [ -  + ]:         30 :   if (byte != '\n')
     463 [ #  # ][ #  # ]:          0 :     while((byte!='\n') && (byte != EOF))
     464                 :          0 :       byte=fgetc(fd);
     465                 :         30 :   fstr[i]=0;
     466                 :         30 : }
     467                 :            : #endif
     468                 :            : 
     469                 :            : #ifdef MSDOS
     470                 :            : int test_file_access(char *file_name, char mode)
     471                 :            : {
     472                 :            :     FILE *fd;
     473                 :            : 
     474                 :            :     if (mode == 'r')
     475                 :            :         fd = fopen(file_name, "r");
     476                 :            :     else
     477                 :            :         fd = fopen(file_name, "r+");
     478                 :            :     if (!fd)
     479                 :            :         return 0;
     480                 :            :     fclose(fd);
     481                 :            :     return 1;
     482                 :            : }
     483                 :            : #else
     484                 :         23 : int test_file_access(char *file_name, char mode)
     485                 :            : {
     486                 :            :     int fd;
     487                 :            : 
     488         [ +  + ]:         23 :     if (mode == 'r')
     489                 :         21 :         fd = open(file_name, O_RDONLY, 0);
     490                 :            :     else
     491                 :          2 :         fd = open(file_name, O_RDWR, 0);
     492         [ -  + ]:         23 :     if (fd < 0)
     493                 :          0 :         return 0;
     494                 :         23 :     close(fd);
     495                 :         23 :     return 1;
     496                 :            : }
     497                 :            : #endif
     498                 :            : 
     499                 :            : char *required_file_list[] =
     500                 :            : {
     501                 :            :   "maps.dat", "motd.txt",
     502                 :            :   "license.txt", NULL
     503                 :            : };
     504                 :            : 
     505                 :            : char *required_writeable[] =
     506                 :            : {
     507                 :            :   "omega.hi", "omega.log", NULL
     508                 :            : };
     509                 :            :                 
     510                 :            : char *optional_file_list[] =
     511                 :            : {
     512                 :            :   "help1.txt", "help2.txt", "help3.txt", "help4.txt", "help5.txt",
     513                 :            :   "help6.txt", "help7.txt", "help8.txt", "help9.txt", "help10.txt",
     514                 :            :   "help11.txt", "help12.txt", "help13.txt", "abyss.txt", "scroll1.txt",
     515                 :            :   "scroll2.txt", "scroll3.txt", "scroll4.txt", NULL
     516                 :            : };
     517                 :            : 
     518                 :            : /* Checks existence of omega data files */
     519                 :            : /* Returns 1 if OK, 0 if impossible to run, -1 if possible but not OK */
     520                 :          1 : int filecheck(void)
     521                 :            : {
     522                 :          1 :     int impossible=FALSE,badbutpossible=FALSE;
     523                 :            :     int endpos;
     524                 :            :     int file;
     525                 :            : 
     526                 :          1 :     strcpy(Str1, Omegavar);
     527                 :          1 :     endpos = strlen(Str1);
     528         [ +  + ]:          3 :     for (file = 0; required_writeable[file]; file++)
     529                 :            :     {
     530                 :          2 :         strcpy(&(Str1[endpos]), required_writeable[file]);
     531         [ -  + ]:          2 :         if (test_file_access(Str1, 'w') == 0)
     532                 :            :         {
     533                 :          0 :             impossible = TRUE;
     534                 :          0 :             printf("\nError! File not appendable or accessible: %s", Str1);
     535                 :            :         }
     536                 :            :     }
     537                 :            : 
     538                 :          1 :     strcpy(Str1, Omegalib);
     539                 :          1 :     endpos = strlen(Str1);
     540         [ +  + ]:          4 :     for (file = 0; required_file_list[file]; file++)
     541                 :            :     {
     542                 :          3 :         strcpy(&(Str1[endpos]), required_file_list[file]);
     543         [ -  + ]:          3 :         if (test_file_access(Str1, 'r') == 0)
     544                 :            :         {
     545                 :          0 :             impossible = TRUE;
     546                 :          0 :             printf("\nError! File not accessible: %s", Str1);
     547                 :            :         }
     548                 :            :     }
     549                 :            : 
     550         [ +  + ]:         19 :     for (file = 0; optional_file_list[file]; file++)
     551                 :            :     {
     552                 :         18 :         strcpy(&(Str1[endpos]), optional_file_list[file]);
     553         [ -  + ]:         18 :         if (test_file_access(Str1, 'r') == 0)
     554                 :            :         {
     555                 :          0 :             badbutpossible = TRUE;
     556                 :          0 :             printf("\nWarning! File not accessible: %s", Str1);
     557                 :            :         }
     558                 :            :     }
     559         [ -  + ]:          1 :     if (impossible) {
     560                 :          0 :         printf("\nFurther execution is impossible. Sorry.");
     561         [ #  # ]:          0 :         if (strcmp(Omegavar, OMEGALIB))
     562                 :          0 :           printf("\nEnvironment variable OMEGALIB badly set\n");
     563                 :            :         else {
     564                 :          0 :           printf("\nOMEGALIB may be badly #defined in defs.h\n");
     565                 :            : #ifndef FIXED_OMEGALIB
     566                 :            :           printf("\nYou can set the environment variable OMEGALIB to\n");
     567                 :            :           printf("the location of the omegavar directory.\n");
     568                 :            : #endif
     569                 :            :         }
     570                 :          0 :         return(0);
     571                 :            :     }
     572         [ -  + ]:          1 :     else if (badbutpossible) {
     573                 :          0 :         printf("\nFurther execution may cause anomalous behavior.");
     574                 :          0 :         printf("\nContinue anyhow? [yn] ");
     575         [ #  # ]:          0 :         if (getchar()=='y') return(-1);
     576                 :          0 :         else return(0);
     577                 :            :     }
     578                 :          1 :     else return(1);
     579                 :            : }
     580                 :            : 
     581                 :            : /* display a file given a string name of file */
     582                 :          4 : void displayfile(char *filestr)
     583                 :            : {
     584                 :          4 :   FILE *fd = checkfopen(filestr,"r");
     585                 :          4 :   int c,d=' ';
     586                 :            :   int x,y;
     587                 :          4 :   clear();
     588                 :          4 :   refresh();
     589                 :          4 :   c = fgetc(fd);
     590 [ +  + ][ +  - ]:      11836 :   while ((c != EOF)&&((char) d != 'q')&&((char) d!=ESCAPE)) {
                 [ +  - ]
     591 [ +  - ][ +  - ]:      11832 :     getyx(stdscr,y,x);
     592         [ -  + ]:      11832 :     if (y > ScreenLength) { 
     593         [ #  # ]:          0 :       standout();
     594                 :          0 :       printw("\n-More-");
     595         [ #  # ]:          0 :       standend();
     596                 :          0 :       refresh();
     597                 :          0 :       d = wgetch(stdscr);
     598                 :          0 :       clear();
     599                 :            :     }
     600                 :      11832 :     printw("%c",(char) c);
     601                 :      11832 :     c = fgetc(fd);
     602                 :            :   }
     603 [ +  - ][ +  - ]:          4 :   if (((char) d != 'q')&&((char) d!=ESCAPE)) {
     604         [ +  - ]:          4 :     standout();
     605                 :          4 :     printw("\n-Done-");
     606         [ +  - ]:          4 :     standend();
     607                 :          4 :     refresh();
     608                 :          4 :     getch();
     609                 :            :   }
     610                 :          4 :   clear();
     611                 :          4 :   refresh();
     612                 :          4 :   fclose(fd);
     613                 :          4 : }    
     614                 :            : 
     615                 :            : /* display a file given a string name of file */
     616                 :          0 : void displaycryptfile(char *filestr)
     617                 :            : {
     618                 :          0 :   FILE *fd = checkfopen(filestr,"rb");
     619                 :          0 :   int c,d=' ';
     620                 :            :   int x,y;
     621                 :          0 :   char key = 100;
     622                 :            : 
     623                 :          0 :   clear();
     624                 :          0 :   refresh();
     625                 :          0 :   c = fgetc(fd);
     626 [ #  # ][ #  # ]:          0 :   while ((c != EOF)&&((char) d != 'q')&&((char) d!=ESCAPE)) {
                 [ #  # ]
     627 [ #  # ][ #  # ]:          0 :     getyx(stdscr,y,x);
     628         [ #  # ]:          0 :     if (y > ScreenLength) { 
     629         [ #  # ]:          0 :       standout();
     630                 :          0 :       printw("\n-More-");
     631         [ #  # ]:          0 :       standend();
     632                 :          0 :       refresh();
     633                 :          0 :       d = wgetch(stdscr);
     634                 :          0 :       clear();
     635                 :            :     }
     636                 :          0 :     key = ((unsigned char) c)^key;
     637                 :          0 :     printw("%c", key);
     638                 :          0 :     c = fgetc(fd);
     639                 :            :   }
     640 [ #  # ][ #  # ]:          0 :   if (((char) d != 'q')&&((char) d!=ESCAPE)) {
     641         [ #  # ]:          0 :     standout();
     642                 :          0 :     printw("\n-Done-");
     643         [ #  # ]:          0 :     standend();
     644                 :          0 :     refresh();
     645                 :          0 :     getch();
     646                 :            :   }
     647                 :          0 :   clear();
     648                 :          0 :   refresh();
     649                 :          0 :   fclose(fd);
     650                 :          0 : }    
     651                 :            : 
     652                 :            : /* copy a file given a string name of file */
     653                 :          0 : void copyfile(char *srcstr)
     654                 :            : {
     655                 :            :   char deststr[80];
     656                 :            :   char buffer[STRING_LEN];
     657                 :            :   FILE *in, *out;
     658                 :            : 
     659                 :          0 :   print1("Enter name of file to create: ");
     660                 :          0 :   strcpy(deststr,msgscanstring());
     661         [ #  # ]:          0 :   if (strlen(deststr) == 0) {
     662                 :          0 :     print2("Aborting...");
     663                 :          0 :     morewait();
     664                 :          0 :     return;
     665                 :            :   }
     666                 :          0 :   in = checkfopen(srcstr, "rb");
     667                 :          0 :   change_to_user_perms();
     668                 :          0 :   out = fopen(deststr, "wb");
     669         [ #  # ]:          0 :   if (!out) {
     670                 :          0 :     sprintf(buffer, "Unable to write to file %s - Aborting.", deststr);
     671                 :          0 :     print2(buffer);
     672                 :          0 :     change_to_game_perms();
     673                 :          0 :     morewait();
     674                 :          0 :     fclose(in);
     675                 :          0 :     return;
     676                 :            :   }
     677                 :          0 :   print2("Copying file....");
     678         [ #  # ]:          0 :   while (fgets(buffer, STRING_LEN, in))
     679                 :          0 :     fputs(buffer, out);
     680                 :          0 :   fclose(in);
     681                 :          0 :   fclose(out);
     682                 :          0 :   change_to_game_perms();
     683                 :          0 :   print3("Done.");
     684                 :            : }    
     685                 :            : 

Generated by: LCOV version 1.11