File: openw.C

package info (click to toggle)
peruser 4b33-10
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,944 kB
  • ctags: 1,064
  • sloc: cpp: 22,397; perl: 2,733; makefile: 345; sh: 335
file content (43 lines) | stat: -rw-r--r-- 813 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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "npfile.h"

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

   if ( lock( 1 ))
   {
      close();
      return 2;
   }

   if (( stream = fopen( fn, "w" )) == NULL )
   {
      snprintf( error_message, sizeof error_message,
                "NP_File: openw(): Error opening file %s for writing: %d",
                filename, errno );
      return 1;
   }

   if (( filename = strdup( fn )) == NULL )
   {
      perror( "strdup" );
      exit( 1 );
   }

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