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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*
* files.c -- allows you to read/write files. Wow.
*
* (C) 1995 Jeremy Nelson (ESL)
* See the COPYRIGHT file for more information
*/
#include "irc.h"
#include "ircaux.h"
/* Here's the plan.
* You want to open a file.. you can READ it or you can WRITE it.
* unix files can be read/written, but its not the way you expect.
* so we will only alllow one or the other. If you try to write to
* read only file, it punts, and if you try to read a writable file,
* you get a null.
*
* New functions: open(FILENAME <type>)
* <type> is 0 for read, 1 for write, 0 is default.
* Returns fd of opened file, -1 on error
* read (fd)
* Returns line for given fd, as long as fd is
* opened via the open() call, -1 on error
* write (fd text)
* Writes the text to the file pointed to by fd.
* Returns the number of bytes written, -1 on error
* close (fd)
* closes file for given fd
* Returns 0 on OK, -1 on error
* eof (fd)
* Returns 1 if fd is at EOF, 0 if not. -1 on error
*/
struct FILE___ {
FILE *file;
struct FILE___ *next;
};
typedef struct FILE___ File;
static File *FtopEntry = (File *) 0;
File *new_file _((void))
{
File *tmp = FtopEntry;
File *tmpfile = (File *)new_malloc(sizeof(File));
if (FtopEntry == (File *) 0)
FtopEntry = tmpfile;
else
{
while (tmp->next)
tmp = tmp->next;
tmp->next = tmpfile;
}
return tmpfile;
}
#ifdef __STDC__
static void remove_file (File *file)
#else
static void remove_file (file)
File *file;
#endif
{
File *tmp = FtopEntry;
if (file == FtopEntry)
FtopEntry = file->next;
else
{
while (tmp->next && tmp->next != file)
tmp = tmp->next;
if (tmp->next)
tmp->next = tmp->next->next;
}
fclose(file->file);
new_free((char **)&file);
}
#ifdef __STDC__
int open_file_for_read (char *filename)
#else
int open_file_for_read (filename)
char *filename;
#endif
{
char *dummy_filename = (char *) 0;
FILE *file;
/* XXXX - this looks like a memory leak */
malloc_strcpy(&dummy_filename, filename);
file = uzfopen(&dummy_filename, ".");
new_free(&dummy_filename);
if (file)
{
File *nfs = new_file();
nfs->file = file;
nfs->next = (File *) 0;
return fileno(file);
}
else
return -1;
}
#ifdef __STDC__
int open_file_for_write (char *filename)
#else
int open_file_for_write (filename)
char *filename;
#endif
{
/* patch by Scott H Kilau so expand_twiddle works */
char *expand = NULL;
FILE *file;
if (!(expand = expand_twiddle(filename)))
malloc_strcpy(&expand, filename);
file = fopen(expand, "a");
new_free(&expand);
if (file)
{
File *nfs = new_file();
nfs->file = file;
nfs->next = (File *) 0;
return fileno(file);
}
else
return -1;
}
#ifdef __STDC__
static File *lookup_file (int fd)
#else
static File *lookup_file (fd)
int fd;
#endif
{
File *ptr = FtopEntry;
while (ptr)
{
if (fileno(ptr->file) == fd)
return ptr;
else
ptr = ptr -> next;
}
return (File *) 0;
}
#ifdef __STDC__
int file_write (int fd, char *stuff)
#else
int file_write (fd, stuff)
int fd;
char *stuff;
#endif
{
File *ptr = lookup_file(fd);
if (!ptr)
return -1;
else
return fprintf(ptr->file, "%s\n", stuff);
}
#ifdef __STDC__
int file_writeb (int fd, char *stuff)
#else
int file_writeb (fd, stuff)
int fd;
char *stuff;
#endif
{
File *ptr = lookup_file(fd);
if (!ptr)
return -1;
else
return fwrite(stuff, 1, strlen(stuff), ptr->file);
}
#ifdef __STDC__
char *file_read (int fd)
#else
char *file_read (fd)
int fd;
#endif
{
File *ptr = lookup_file(fd);
if (!ptr)
return m_strdup(empty_string);
else
{
char blah[10240];
fgets(blah, 10240, ptr->file);
/* XXX - probably should be done by chop(). */
if (strlen(blah) && blah[strlen(blah)-1] == '\n')
blah[strlen(blah)-1] = 0;
/*
* Return empty string if we're at EOF.
* This makes more sense.
*/
if (feof(ptr->file))
strcpy(blah, empty_string);
return m_strdup(blah);
}
}
#ifdef __STDC__
char *file_readb (int fd, int numb)
#else
char *file_readb (fd, numb)
int fd, numb;
#endif
{
File *ptr = lookup_file(fd);
if (!ptr)
return m_strdup(empty_string);
else
{
char *blah = (char *)new_malloc(numb+1);
fread(blah, 1, numb, ptr->file);
blah[numb] = 0;
return blah;
}
}
#ifdef __STDC__
int file_eof (int fd)
#else
int file_eof (fd)
int fd;
#endif
{
File *ptr = lookup_file (fd);
if (!ptr)
return -1;
else
return feof(ptr->file);
}
#ifdef __STDC__
int file_close (int fd)
#else
int file_close (fd)
int fd;
#endif
{
File *ptr = lookup_file (fd);
if (!ptr)
return -1;
else
remove_file (ptr);
return 0;
}
#ifdef __STDC__
int file_valid (int fd)
#else
int file_valid (fd)
int fd;
#endif
{
if (lookup_file(fd))
return 1;
return 0;
}
|