file.c 4.16 KB
Newer Older
kollo's avatar
kollo committed
1
/* file.c  (c) Markus Hoffmann   */
2 3
/* Extensions for the standard file i/o operations.
   Erweiterungen fuer die Datei Ein- und Ausgabe ....   */
kollo's avatar
kollo committed
4

kollo's avatar
kollo committed
5 6

/* This file is part of X11BASIC, the basic interpreter for Unix/X
7
 * ================================================================
kollo's avatar
kollo committed
8 9 10
 * X11BASIC is free software and comes with NO WARRANTY - read the file
 * COPYING for details
 */
11

kollo's avatar
kollo committed
12 13 14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
kollo's avatar
kollo committed
15
#include <errno.h>
kollo's avatar
kollo committed
16
#include <sys/types.h>
kollo's avatar
kollo committed
17
#include <sys/stat.h>
kollo's avatar
kollo committed
18
#include <unistd.h>
kollo's avatar
kollo committed
19 20
#include <fcntl.h>

kollo's avatar
kollo committed
21 22
#include "file.h"

kollo's avatar
kollo committed
23
#ifndef FALSE
kollo's avatar
kollo committed
24 25
#define FALSE 0
#define TRUE (!FALSE)
kollo's avatar
kollo committed
26
#endif
kollo's avatar
kollo committed
27

kollo's avatar
kollo committed
28 29
void io_error(int,char *);

kollo's avatar
kollo committed
30
char *lineinput( FILE *n, char *line,int size) {   /* liest eine ganze Zeile aus einem ASCII-File ein */
kollo's avatar
kollo committed
31
  int c; int i=0;
kollo's avatar
kollo committed
32
  while((c=fgetc(n))!=EOF) {
kollo's avatar
kollo committed
33 34
      if(c==(int)'\n') {
	  line[i]=(int)'\0';
kollo's avatar
kollo committed
35 36
	  return line;
	}
kollo's avatar
kollo committed
37
	else line[i++]=(char)c;
kollo's avatar
kollo committed
38
	if(i>=size-1) break;
kollo's avatar
kollo committed
39 40 41 42
    }
    line[i]='\0';
    return line;
}
kollo's avatar
kollo committed
43 44

char *input( FILE *n, char *line,int size) {   /* liest bis Komma oder Zeilenende aus einem ASCII-File ein */
kollo's avatar
kollo committed
45
  int c; 
kollo's avatar
kollo committed
46 47
  int  i=0,ff=0;
  while((c=fgetc(n))!=EOF) {
kollo's avatar
kollo committed
48
      if(c==(int)'\n' || (c==(int)',' && ff==0)) {
kollo's avatar
kollo committed
49 50 51
	  line[i]='\0';
	  return line;
	}
kollo's avatar
kollo committed
52 53
	else if(c==(int)'\"') ff=!ff;
	else line[i++]=(char)c;
kollo's avatar
kollo committed
54
	if(i>=size-1) break;
kollo's avatar
kollo committed
55 56 57 58 59
    }
    line[i]='\0';
    return line;
}

kollo's avatar
kollo committed
60 61 62
/* Returns the length of the open file n */

size_t lof(FILE *n) {	
63 64 65 66
  long position=ftell(n);
  if(position==-1) {
    io_error(errno,"lof");
    return(0);
kollo's avatar
kollo committed
67
  }
68 69 70 71 72 73 74
  if(fseek(n,0,SEEK_END)==0) {
    long laenge=ftell(n);
    if(laenge<0) io_error(errno,"ftell");
    if(fseek(n,position,0)<0) io_error(errno,"fseek"); 
    return(laenge);
  } else io_error(errno,"fseek");
  return(0);
kollo's avatar
kollo committed
75 76 77
}


kollo's avatar
kollo committed
78 79
/* Returns the eof condition of an open file n */

kollo's avatar
kollo committed
80
int myeof(FILE *n) {
kollo's avatar
kollo committed
81
  int c=fgetc(n);
kollo's avatar
kollo committed
82 83 84
  ungetc(c,n);
  return c==EOF;
}
kollo's avatar
kollo committed
85 86 87 88 89 90
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef S_IRGRP
#define S_IRGRP 0
#endif
kollo's avatar
kollo committed
91

kollo's avatar
kollo committed
92 93 94 95 96

/* Saves an area in memory starting at adr with length len to a file
   with filename name.
   RETURNS: 0 on success and -1 on error */

kollo's avatar
kollo committed
97
int bsave(const char *name, char *adr, size_t len) { 
kollo's avatar
kollo committed
98
  int fdis=open(name,O_CREAT|O_BINARY|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP);
kollo's avatar
kollo committed
99 100 101
  if(fdis==-1) return(-1);
  if(write(fdis,adr,len)==-1) io_error(errno,"write");
  return(close(fdis));
kollo's avatar
kollo committed
102 103
}

kollo's avatar
kollo committed
104 105 106 107 108
/* loads the file with filename name to a memory location at address adr.
   a maximum of len bytes are read. if len==-1 the whole file is read. 
   RETURNS the number of read bytes or 0 on error.
   */

kollo's avatar
kollo committed
109
size_t bload(const char *name, char *adr, size_t len) {	
110
  FILE *fdis=fopen(name,"rb");
kollo's avatar
kollo committed
111 112 113 114 115
  if(fdis==NULL) return(0);
  if(len==-1) len=lof(fdis);
  if(len>0) len=fread(adr,1,len,fdis);
  fclose(fdis);
  return(len);
kollo's avatar
kollo committed
116
}
kollo's avatar
kollo committed
117 118 119

/* Checks if a give file name exists */

kollo's avatar
kollo committed
120
#if 0
121 122
int exist(const char *name ) {	/* This is a safe but slow implementation */
  int fdis=open(name,0x8000);
kollo's avatar
kollo committed
123 124 125
  if (fdis==-1) return(FALSE);
  close(fdis);
  return(TRUE);
kollo's avatar
kollo committed
126
}
kollo's avatar
kollo committed
127
#else
kollo's avatar
kollo committed
128
int exist(const char *filename) {
kollo's avatar
kollo committed
129
  struct stat fstats;
kollo's avatar
kollo committed
130
  int retc=stat(filename, &fstats);
kollo's avatar
kollo committed
131 132 133 134
  if(retc==-1) return(FALSE);
  return(TRUE);
}
#endif
kollo's avatar
kollo committed
135

kollo's avatar
kollo committed
136
int stat_device(const char *filename) {
kollo's avatar
kollo committed
137 138 139 140 141
  struct stat fstats;
  int retc=stat(filename, &fstats);
  if(retc==-1) io_error(errno,"stat");
  return(fstats.st_dev);  
}
kollo's avatar
kollo committed
142
int stat_inode(const char *filename) {
kollo's avatar
kollo committed
143 144 145 146 147
  struct stat fstats;
  int retc=stat(filename, &fstats);
  if(retc==-1) io_error(errno,"stat");
  return(fstats.st_ino);  
}
kollo's avatar
kollo committed
148
int stat_mode(const char *filename) {
kollo's avatar
kollo committed
149 150 151 152 153
  struct stat fstats;
  int retc=stat(filename, &fstats);
  if(retc==-1) io_error(errno,"stat");
  return(fstats.st_mode);  
}
kollo's avatar
kollo committed
154
int stat_nlink(const char *filename) {
kollo's avatar
kollo committed
155 156 157 158 159
  struct stat fstats;
  int retc=stat(filename, &fstats);
  if(retc==-1) io_error(errno,"stat");
  return(fstats.st_nlink);  
}
kollo's avatar
kollo committed
160
int stat_uid(const char *filename) {
kollo's avatar
kollo committed
161 162 163 164 165
  struct stat fstats;
  int retc=stat(filename, &fstats);
  if(retc==-1) io_error(errno,"stat");
  return(fstats.st_uid);  
}
kollo's avatar
kollo committed
166
int stat_gid(const char *filename) {
kollo's avatar
kollo committed
167 168 169 170 171
  struct stat fstats;
  int retc=stat(filename, &fstats);
  if(retc==-1) io_error(errno,"stat");
  return(fstats.st_gid);  
}
kollo's avatar
kollo committed
172
int stat_size(const char *filename) {
kollo's avatar
kollo committed
173 174 175 176 177
  struct stat fstats;
  int retc=stat(filename, &fstats);
  if(retc==-1) io_error(errno,"stat");
  return(fstats.st_size);  
}