File: file.cxx

package info (click to toggle)
curves 0.8.7
  • links: PTS
  • area: main
  • in suites: slink
  • size: 704 kB
  • ctags: 1,001
  • sloc: cpp: 6,197; ansic: 519; makefile: 270; sh: 192; fortran: 149
file content (168 lines) | stat: -rw-r--r-- 4,301 bytes parent folder | download
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* file.cxx
     $Id: file.cxx,v 1.8 1998/02/23 06:35:46 elf Exp $

   written by Marc Singer
   25 October 1996

   This file is part of the project CurVeS.  See the file README for
   more information.

   Copyright (C) 1996 Marc Singer

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   in a file called COPYING along with this program; if not, write to
   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
   02139, USA.

   -----------
   DESCRIPTION
   -----------

   Code to do file handling.  This, presently, includes editing
   filesystem files.

*/

#include "std.h"
#include "preferences.h"	// Everyone MUST get into the pref scene

#include <sys/stat.h>
#include "file.h"
#include "mman.h"
#include "path.h"

#define CB_COPY_BUFFER		(65536)
#define CB_GUESS_BUFFER		(4096) 	// 4K, one page?  8K, two pages?

/* file_copy

   copies the contents of the source file to the destination file.
   The return value is the number of bytes copies, or -1 if there is
   an error.

*/

long file_copy (const char* szFileDst, const char* szFileSrc)
{
  LPath pathSrc (szFileSrc);
  LPath pathDst (szFileDst);

  int fhSrc = -1;
  int fhDst = -1;
  void* pv = NULL;
  struct stat stat;
  long result = -1;

  if (!::stat (pathSrc, &stat)
      && stat.st_size
      && ((fhSrc = open (pathSrc, O_RDONLY)) != -1)
      && ((fhDst = open (pathDst, O_WRONLY | O_TRUNC | O_CREAT, 0644)) != -1)
      && (pv = malloc (CB_COPY_BUFFER))) {
    result = 0;
    while (stat.st_size) {
      long cb = stat.st_size;
      if (cb > CB_COPY_BUFFER)
	cb = CB_COPY_BUFFER;
      read (fhSrc, pv, cb);
      result += write (fhDst, pv, cb);
      stat.st_size -= cb;
    }
  }
  if (fhSrc != -1)
    close (fhSrc);
  if (fhDst != -1)
    close (fhDst);
  if (pv)
    free (pv);
  return result;
}

/* file_edit

   runs the user's preferred editor on the named file.  We copy the
   file to a temporary before running the editor, run it, and return
   the temporary filename if the user edited the file.  DO NOT delete
   the memory for this temporary filename.  Copy it if you need to
   hang onto it.

*/

const char* file_edit (const char* szFile)
{
  LPath path (szFile);

  const char* szEditor = g_preferences.fetch ("Editor");
  if (!szEditor)
    szEditor = getenv ("EDITOR");
  if (!szEditor)
    szEditor = getenv ("VISUAL");
  if (!szEditor)
    return NULL;

  char* szFileTemp = tmpnam (NULL); 	// FIXME: this returns static buffer
  file_copy (szFileTemp, path);
  
  struct stat stat;
  ::stat (szFileTemp, &stat);
  long time = stat.st_mtime;

  char sz[256];
  sprintf (sz, "%s %s", szEditor, szFileTemp);
  system (sz);

  ::stat (szFileTemp, &stat);
  if (stat.st_mtime == time) {
    unlink (szFileTemp);
    szFileTemp = NULL;
  }
  return szFileTemp;
}

bool file_guess_binary (const char* szFile)
{
  bool fResult = false;

  int fh = -1;
  char* pbMap = NULL;
  struct stat stat;
  long cb = CB_GUESS_BUFFER;
  if (!::stat (szFile, &stat) && stat.st_size
      && ((fh = open (szFile, O_RDONLY)) != -1)
      && (cb = ((stat.st_size < cb) ? stat.st_size : cb))
      && (pbMap = (char*) mmap (NULL, CB_GUESS_BUFFER, PROT_READ, 
				MAP_FILE | MAP_PRIVATE, fh, 0))) {
    int cNul       = 0;
    int cPrintable = 0;
    int cBinary    = 0;
    for (char* pb = pbMap; cb-- > 0; ++pb) {
      if (!*pb)
	++cNul;
      else if (isprint (*pb) || isspace (*pb))
	++cPrintable;
      else
	++cBinary;
    }    
    if (cNul > 1 || cBinary > cPrintable/3)
      fResult = true;
    {
      LPath path (szFile);
      TRACE((T_FILE_TRACE, "'%-15s' cPrintable %4d  cBinary %4d  %s", 
	 path.tail (), cPrintable, cBinary, fResult ? "Binary" : "Text"));
    }
  }
  if (pbMap)
    munmap (pbMap, CB_GUESS_BUFFER);
  if (fh != -1)
    close (fh);
  return fResult;
}