📄 file_hnd.c
字号:
/* file_hnd.c 04.03.99 */
/*!
/ --------------------------------------------------------------
/ Copyright (C) 1993: Michael Braun
/ Kaetinger Muehlenweg 103 A
/ D-28816 Stuhr
/ --------------------------------------------------------------
/
/ file handler (read / write datafiles)
/
*/
/****************************************************************
* *
* BENUTZTE UNTERPROGRAMME (C LIBRARY) *
* *
****************************************************************/
#include "config.h"
#include "global.h"
#include "standard.h"
#include "file_hnd.h"
#include "history.h"
#include "disp_hnd.h"
#include "kb_input.h"
#include "err_mess.h"
#include "mb_ctype.h"
/****************************************************************
* *
* BENUTZTE UNTERPROGRAMME / GLOBALE VARIABLEN *
* *
****************************************************************/
#if (ACT_OP_SYSTEM == OS_9)
#ifndef fileno
#define fileno(p) ((p)->_fd)
#endif
#endif
#if ((UNIX) || (ACT_OP_SYSTEM == OS_9))
#define SET_ATTRIBUTES 1
#else
#define SET_ATTRIBUTES 0
#endif
#define FAST_BACKUP 1 /* <== select here ! */
#if ((BINARY_MODE) && (ACT_OP_SYSTEM != OS_9))
#define FAST_FILE_LENGTH 1 /* <== select here ! */
#else
#define FAST_FILE_LENGTH 0 /* must be 0 !! */
#endif
#if ((!FAST_BACKUP) || (!FAST_FILE_LENGTH))
#define BUFFER_SIZE 0x0400 /* sinnvoll: 2-er-potenz < 64 k */
static char help_buff [BUFFER_SIZE];
#endif
#define RD_WR_SIZE 0x4000 /* sinnvoll: 2-er-potenz < 64 k */
/****************************************************************
* *
* ENDE DER DEKLARATIONEN *
* *
****************************************************************/
/* -FF- */
int delete_datafile (char *filename)
{
#if (ACT_OP_SYSTEM == UNIX_SVR4)
return unlink (filename);
#else
return remove (filename);
#endif
} /* delete_datafile */
int rename_datafile (char *name_inp, char *name_outp)
{
#if (ACT_OP_SYSTEM == UNIX_SVR4)
/* link input- to bak-file */
if (link (name_inp, name_outp) != 0)
return (-3); /* error */
/* remove input-file */
if (unlink (name_inp) == 0)
return (0); /* o.k. */
else
return (-3); /* error */
#else
int first_ind = 0;
#define OS9_WITH_BERKLIB 0 /* 0 rename with full path in <new name> */
/* 1 rename without full path in <new name> */
#if ((ACT_OP_SYSTEM == OS_9) && (OS9_WITH_BERKLIB))
int ii, name_len;
/* In the Berklib, the rename function works different in os/9 : */
/* name_outp is only the filename, not the entire path (as in ms/dos + unix). */
/* Thus, we remove the path up to the begin of the filename. */
name_len = strlen (name_outp);
for (ii = (name_len-1) ; ii >= 1 ; ii--) /* rueckwaerts */
{
/* abort search, if a separator is found before */
if (name_outp [ii] == '/') /* path separator */
{
first_ind = ii + 1;
break;
}
} /* for ii */
#endif
/* rename input-file to bak-file */
if (rename (name_inp, &name_outp[first_ind]) == 0)
return (0); /* o.k. */
else
return (-3); /* error */
#endif
} /* rename_datafile */
/* -FF- */
#if FAST_BACKUP /* neue (schnelle) version */
static int gen_backup_file (char *name_inp, char *name_outp)
{
delete_datafile (name_outp);
return rename_datafile (name_inp, name_outp);
} /* gen_backup_file */
/* -FF- */
#else /* alte version fuer den notfall */
static int gen_backup_file (char *name_inp, char *name_outp)
{
/************************/
/* local definitions */
/************************/
static FILE *fp_inp;
static FILE *fp_out;
size_t num;
/************************/
/* open datafiles */
/************************/
fp_inp = fopen (name_inp, READ_FORMAT);
if (fp_inp == NULL)
{
return (-1); /* error */
}
fp_out = fopen (name_outp, WRITE_FORMAT);
if (fp_out == NULL)
{
fclose (fp_inp);
return (-2); /* error */
}
/* loop for input file */
while ((num = fread (help_buff, 1, BUFFER_SIZE, fp_inp)) > 0)
{
fwrite (help_buff, 1, num, fp_out);
} /* end while */
/************************/
/* end of program */
/************************/
fclose (fp_inp);
fclose (fp_out);
return (0);
} /* gen_backup_file */
#endif
/* -FF- */
/***************************************************************/
/* get file length */
/***************************************************************/
long get_file_length (char *filename, int file_must_exist)
{
FILE *fp;
long index;
/* ACHTUNG !! bei files > 0x7fff laenge wird bei os/9 st_size negativ, */
/* obwohl in <stat.h> als long definiert */
#if (FAST_FILE_LENGTH)
static struct stat filestat;
#else
size_t num;
#endif
show_status_line_2 ("*** counting source file ***", 0, -2, 0);
/* open datafile */
fp = fopen (filename, READ_FORMAT); /* ACHTUNG! im ASCII-Mode: ^Z (0x1a) ==> EOF */
if (fp == NULL) /* file doesn't exist */
{
if (file_must_exist)
{
err_message (FILE_DOESN_T_EXIST);
return -2; /* error : abort --> */
}
else
{
return 0; /* no bytes read */
}
}
#if (FAST_FILE_LENGTH)
/* close datafile */
fclose (fp);
if (stat (filename, &filestat))
index = 0; /* error */
else
index = filestat.st_size; /* o.k. */
#else
/* read datafile */
index = 0;
while ((num = fread (help_buff, 1, BUFFER_SIZE, fp)) > 0)
{
index += num;
} /* end while */
/* close datafile */
fclose (fp);
#endif
/* o.k. */
return (index + 1); /* number of bytes read + EOF */
} /* get_file_length */
/* -FF- */
/***************************************************************/
/* read datafile */
/***************************************************************/
long read_datafile (char *filename, char FA_HU *buff_0,
long max_len, int file_must_exist)
{
char FA_HU * buffer;
FILE *fp;
long index;
size_t num;
show_status_line_2 ("*** reading source file ***", 0, -2, 0);
/* valid buffer ? */
if (buff_0 == NULL)
{
err_message (OUT_OF_MEMORY);
return -1; /* error : abort --> */
}
/* open datafile */
fp = fopen (filename, READ_FORMAT);
if (fp == NULL) /* file doesn't exist */
{
if (file_must_exist)
{
err_message (FILE_DOESN_T_EXIST);
return -2; /* error : abort --> */
}
else
{
*buff_0 = EOF;
return 0; /* no bytes read */
}
}
/* read datafile */
buffer = buff_0;
index = 0;
while ((num = fread (buffer, 1,
(size_t)(min (max_len, (long) RD_WR_SIZE)), fp)) > 0)
{
buffer += num;
index += num;
max_len -= num;
if (max_len <= 0) break;
} /* end while */
/* close datafile */
fclose (fp);
/* is file to large for memory buffer ? */
if (max_len <= 0)
{
if (max_len == 0)
{
err_message (FILE_SIZE_CHANGED); /* file was increased during read */
index--; /* skip this (new) character */
}
else
{
err_message (BUFFER_OVERFLOW);
return (-3); /* error : abort --> */
}
}
/* o.k. */
return index; /* number of bytes read */
} /* read_datafile */
/* -FF- */
#if (SET_ATTRIBUTES)
#if (ACT_OP_SYSTEM == OS_9)
static int stat_2_modes (int inp_mode)
{
/* The handling of the file attributes is a little bit confused in os_9. */
/* Get attributes with "stat" , corresponding header file "inet/stat.h". */
/* Set attributes with "chmod", corresponding header file "modes.h". */
/* */
/* The definitions of the access rights (bit masks) are different for */
/* "get" and "set". Totally absurd !! */
/* */
/* Both header files exclude each other !! */
/* We have included "inet/stat.h" here. */
/* */
/* This function translates between both worlds. */
/* translation table */
struct TRANSLATE
{
int inet_stat_h;
int modes_h;
};
static const struct TRANSLATE translate [] =
{
0400, 0x01, /* S_IREAD , S_IREAD */
0200, 0x02, /* S_IWRITE, S_IWRITE */
0100, 0x04, /* S_IEXEC , S_IEXEC */
0004, 0x08, /* S_IROTH , S_IOREAD */
0002, 0x10, /* S_IWOTH , S_IOWRITE */
0001, 0x20 /* S_IXOTH , S_IOEXEC */
};
int ii;
int outp_mode;
/* set the particular bits */
outp_mode = 0x0000;
for (ii = 0 ; ii < lengthof(translate) ; ii++)
{
if (inp_mode & translate[ii].inet_stat_h)
outp_mode |= translate[ii].modes_h;
} /* for ii */
return outp_mode;
} /* stat_2_modes */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -