Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 87 lines (74 sloc) 2.096 kb
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
1 /* oswin32/ostext.c */
2
3 #include "elvis.h"
4 #include <sys/types.h>
5 #include <dos.h>
6 #include <fcntl.h>
7 #include <io.h>
8 #include <errno.h>
9
97d8998 @mbert Import Elvis 2.1_4 (written by Steve Kirkendall)
authored
10 /* the following is defined in oswin32\osdir.c */
11 extern char *dirnormalize(char *path);
12
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
13 #ifdef GUI_WIN32
14 #ifndef O_BINARY
15 #define O_BINARY _O_BINARY
16 #endif
17 #ifndef O_TEXT
18 #define O_TEXT _O_TEXT
19 #endif
20 #endif
21
22 /* This is the fd of the open file */
23 static int fd;
24
25
26 /* Open a text file for reading (if rwa is 'r') or create/overwrite
27 * a file for writing (if rwa is 'w') or appending (if rwa is 'a').
28 * When overwriting an existing file, the file's original permissions
29 * should be preserved. Returns 0 if successful, -1 if no permission,
30 * -2 if not a regular file (e.g., a directory), or -3 for other errors.
31 */
9f1c6f0 @mbert Import Elvis 2.2_0 (written by Steve Kirkendall)
authored
32 int txtopen(char *filename, _char_ rwa, ELVBOOL binary)
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
33 {
34 int flags;
35
36 /* try to open the file */
37 flags = (binary ? O_BINARY : O_TEXT);
38 switch (rwa)
39 {
40 case 'r': flags |= _O_RDONLY; break;
41 case 'w': flags |= _O_WRONLY|_O_CREAT|_O_TRUNC; break;
42 case 'a': flags |= _O_WRONLY|_O_APPEND; break;
43 }
97d8998 @mbert Import Elvis 2.1_4 (written by Steve Kirkendall)
authored
44 fd = _open(dirnormalize(filename), flags, 0666);
cf92e3b @mbert Import Elvis 2.0 (written by Steve Kirkendall)
authored
45
46 /* return a code indicating the success or reason for failure */
47 if (fd >= 0)
48 return 0;
49 else if (errno == EACCES)
50 return -1;
51 else if (errno == EISDIR)
52 return -2;
53 else
54 return -3;
55
56 #if 0
57 /* if supposed to append, then seek to the end of the file */
58 if (rwa == 'a')
59 _lseek(fd, 0L, 2);
60 #endif
61 }
62
63 /* Close the file that was opened by txtopen(). */
64 void txtclose(void)
65 {
66 _close(fd);
67 }
68
69 /* Append text to a file which has been opened for writing.
70 * Returns nbytes if successful, or 0 if the disk is full.
71 * Should perform any necessary translations for converting
72 * elvis' idea of text into the local OS's idea of text.
73 */
74 int txtwrite(CHAR *buf, int nbytes)
75 {
76 return _write(fd, buf, nbytes);
77 }
78
79 /* Read the next chunk of text from a file. nbytes is the maximum
80 * number to read. Returns the number of characters actually read
81 * after any conversions such as CRLF->LF translation.
82 */
83 int txtread(CHAR *buf, int nbytes)
84 {
85 return _read(fd, buf, nbytes);
86 }
Something went wrong with that request. Please try again.