File: openr.C

package info (click to toggle)
peruser 4b33-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,952 kB
  • ctags: 1,064
  • sloc: cpp: 22,367; perl: 2,733; makefile: 345; sh: 335
file content (50 lines) | stat: -rw-r--r-- 1,034 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "npfile.h"

int NP_File::openr( const char *fn )
{
   if ( is_open )
   {
      snprintf( error_message, sizeof error_message,
                "NP_File: openr():" 
                "File %s is still open for this object.", filename );
      return 1;
   }
   
   if ( lock( 1 ))
   {
      close();
      return 2;
   }

   if (( filename = strdup( fn )) == NULL )
   {
      perror( "strdup" );
      exit( 1 );
   }
            
   if (( stream = fopen( fn, "r" )) == NULL )
   {
      if ( errno == ENOENT )
      {
         snprintf( error_message, sizeof error_message,
                   "NP_File: openr(): File %s does not exist", filename );
         return 2;
      }
      else
         snprintf( error_message, sizeof error_message,
                   "NP_File: openr(): fopen() returned error: %s",
                   strerror( errno ));
      return 1;
   }

   is_writable = 0;
   is_readable = 1;
   is_open = 1;
   
   return 0;
}