📄 file_hnd.c
字号:
#endif
/***************************************************************/
/* get + put file attributes, set write permission */
/***************************************************************/
static int mode;
static int uid;
static int gid;
static int get_file_attributes (char *filename)
{
static struct stat file_status;
mode = 0;
uid = 0;
gid = 0;
if (stat (filename, &file_status))
{
return -1; /* error */
}
else
{ /* o.k. */
mode = file_status.st_mode;
uid = file_status.st_uid;
gid = file_status.st_gid;
}
#if ((ACT_OP_SYSTEM == OS_9) && (0)) /* 28.10.96 - Braun */
mode = stat_2_modes (mode);
#endif
return mode; /* -1 : error */
/* else: mode */
} /* get_file_attributes */
static int put_file_attributes (char *filename)
{
int res_1, res_2;
res_1 = chmod (filename, mode); /* -1 : error */
res_2 = chown (filename, uid, gid); /* -1 : error */
return (res_1 | res_2);
} /* put_file_attributes */
static int file_without_write_permission (char *filename)
{
return access (filename, W_OK); /* 0 : write allowed */
/* -1 : write forbidden */
} /* file_without_write_permission */
static int set_write_permission (char *filename)
{
#if ((ACT_OP_SYSTEM != OS_9) || (1)) /* @@ ?? 04.02.99 Check it ! */
mode |= S_IWRITE; /* = 0200 */
#else
mode |= _WRITE; /* = 0x02 */
#endif
return chmod (filename, mode); /* 0 : o.k. */
/* -1 : error */
} /* set_write_permission */
#endif
/* -FF- */
/***************************************************************/
/* write datafile */
/***************************************************************/
static char *create_bak_name (char *filename)
{
#if (ACT_OP_SYSTEM == MS_DOS)
#define LONG_BAK_NAMES 0 /* always 0 here ! */
#else
#define LONG_BAK_NAMES 1 /* for unix + os-9.
set to 0, if long bak names are not desired */
#endif
#define BAK_EXT ".bak" /* 4 chars */
static char bak_name [BUF_256 + 4 + 1];
int name_len, namind;
#if (!LONG_BAK_NAMES)
int ii;
#endif
/* generates a backupname from the filename. */
/* note the special cases in the examples ! */
/* filename bak_name
-----------------------------------
"test.c" "test.bak" unix + os-9: test.c.bak
"test.h" "test.bak" unix + os-9: test.h.bak
".profile" ".profile.bak"
"demo" "demo.bak"
"./demo" "./demo.bak"
"../demo" "../demo.bak"
*/
/* build name of backup file */
name_len = strlen (filename);
namind = name_len;
#if (!LONG_BAK_NAMES)
/* attention ! ii == 0 is not allowed, because */
/* ".bak" is no valid filename in ms-dos + os/9 */
for (ii = (name_len-1) ; ii >= 1 ; ii--) /* rueckwaerts */
{
/* search for 1st occurence of point */
if (filename [ii] == '.')
{
namind = ii;
break;
}
/* abort search, if a separator is found before */
if ((filename [ii] == '/') || /* unix, os/9 */
(filename [ii] == '\\')) /* ms/dos */
{
break;
}
} /* for ii */
#endif
strncpy (bak_name, filename, BUF_256);
strcpy (&bak_name [namind], ".bak"); /* 4 chars, plus '\0' */
return bak_name;
} /* create_bak_name */
/* -FF- */
/***************************************************************/
/* write datafile */
/***************************************************************/
int write_datafile (char *filename, char FA_HU *buff_0,
long byte_anz , int overwrite_allowed,
int backup_file)
{
char FA_HU * buffer;
FILE *fp;
int ii;
int err_flag, key, file_exists, do_it;
size_t num, count;
char *bak_name;
#if (SET_ATTRIBUTES)
int key_2;
#endif
/* default */
err_flag = 0;
/* check, if filename available */
if (!*filename)
{ /* no: get one ! */
do_it = get_line_2_string ("Enter Filename:",
filename, C_R, ID_FILE);
if (!do_it)
{
return -10; /* aborted with ^C (silent) */
}
else
{
if (*filename)
{ /* got a valid name */
overwrite_allowed = 0;
backup_file = 0;
}
else
{ /* no: don't try to write, it doesn't work */
err_message (INVALID_NULL_NAME);
return -4;
}
}
}
/* remove illegal spaces in filename */
#if (ACT_OP_SYSTEM == WIN_32)
ii = strlen(filename) - 1;
while (ii && (filename [ii] == ' ')) /* remove trailing spaces */
{
filename [ii] = '\0';
ii--;
}
while (*filename == ' ') /* remove leading spaces */
{
strcpy(filename, filename+1);
}
#else
for (ii = 0 ; ii < (int) strlen(filename) ; ii++)
{
if (filename [ii] == ' ')
{
filename [ii] = '\0'; /* forced end of string */
break;
}
}
#endif
/* check, if file exists */
file_exists = 0;
fp = fopen (filename, READ_FORMAT);
if (fp != NULL)
{
if (!isatty(fileno(fp))) /* not a device ? */
file_exists = 1;
fclose (fp);
}
/* overwrite file ? */
if ((file_exists) && (!overwrite_allowed))
{
err_message (FILE_EXISTS_OVERWRITE);
key = get_1_key (0);
if (toupper (key) != 'Y')
return -5; /* abort */
}
#if (SET_ATTRIBUTES)
/* read file attributes */
if (file_exists)
{
get_file_attributes (filename);
if (file_without_write_permission (filename))
{
err_message (FILE_IS_READ_ONLY);
key_2 = get_1_key (0);
switch (toupper (key_2))
{
case 'Y':
if (set_write_permission (filename))
{
err_message (COULDN_T_SET_ACCESS_RIGHTS);
}
break;
default:
return -6; /* abort */
break;
}
}
}
#endif
/* generate backup file ? */
if ((file_exists) && (backup_file))
{
show_status_line_2 ("*** writing backup file ***", 0, -2, 0);
bak_name = create_bak_name (filename);
/* save file to BAK-file */
err_flag = gen_backup_file (filename, bak_name);
switch (err_flag)
{
case 0:
show_status_line_2 ("*** backup file written ***", 0, -2, 0);
break;
case -1:
err_message (ERROR_READ_FILE);
break;
case -2:
err_message (ERROR_WRITE_FILE);
break;
case -3:
err_message (ERROR_BACKUP_FILE);
break;
default:
err_message (FATAL_ERROR);
break;
}
} /* backup_file */
/* open datafile */
show_status_line_2 ("*** writing source file ***", 0, -2, 0);
#if (ACT_OP_SYSTEM == MS_DOS)
{
int tmphndl;
/* this sequence is necessary under ms/dos, because we must be sure */
/* to open our file on this directory, not in the append path ! */
if (_dos_creat (filename, 0, &tmphndl))
{
err_message (ERROR_CREATE_FILE);
return -4; /* error : abort --> */
}
_dos_close(tmphndl);
}
#endif
fp = fopen (filename, WRITE_FORMAT);
if (fp == NULL)
{
err_message (ERROR_WRITE_FILE);
return -3; /* error : abort --> */
}
/* write datafile */
buffer = buff_0;
while (byte_anz > 0)
{
count = (size_t)(min (byte_anz, (long) RD_WR_SIZE));
num = fwrite (buffer, 1, count, fp);
buffer += num;
byte_anz -= num;
if (num != count)
{
fclose (fp);
err_message (ERROR_WRITE_FILE);
return -6; /* error : abort --> */
}
} /* end while */
/* close datafile */
if (fclose (fp) != 0)
{
err_message (ERROR_WRITE_FILE);
return -6; /* error : abort --> */
}
/* restore file attributes */
#if (SET_ATTRIBUTES)
if ((file_exists) && (backup_file))
{
if (put_file_attributes (filename))
{
err_message (COULDN_T_SET_ACCESS_RIGHTS);
}
}
#endif
show_status_line_2 ("*** source file written ***", 0, -2, 0);
return err_flag; /* from generation of BAK-file */
} /* write_datafile */
/* -FF- */
/* Modification History */
/* 21.10.92 - file erzeugt */
/* 25.11.92 - return -3, wenn file zu gross */
/* 01.12.92 - loc_malloc (), loc_free () */
/* 03.12.92 - loc_malloc (), loc_free () public gemacht */
/* 03.12.92 - loc_malloc (), loc_free () public in memo_hnd.c ausgelagert */
/* 06.12.92 - generate backup file */
/* 08.12.92 - generate backup file with local function copy_file */
/* 09.12.92 - err_message () */
/* 19.12.92 - FA_HU (far / huge) */
/* 20.12.92 - get_file_length */
/* 28.07.93 - fopen (file, "rb") bzw. "wb" */
/* 28.07.93 - fopen (file, READ_FORMAT) bzw. WRITE_FORMAT */
/* 28.08.93 - create_bak_name () */
/* 29.08.93 - get- put_file_attributes () */
/* 31.08.93 - set_write_permission () */
/* 02.09.93 - put_file_atributes (): only if bak-file */
/* 02.09.93 - delete_datafile () */
/* 03.09.93 - bug fix: file_length(): OS9 --> OS_9 */
/* 12.09.93 - show_status_line_2 (..., ignore_batch) */
/* 16.09.93 - create_bak_name: special case OS_9 */
/* 05.10.93 - rename_datafile() */
/* 10.10.93 - ID_FILE */
/* 19.11.93 - S_IWRITE / _WRITE */
/* 29.11.93 - mb_ctype.h */
/* 06.12.93 - bugfix: error write file --> error message */
/* 22.02.94 - "Enter Filename:" aborted with ^C --> no error message */
/* 27.09.94 - type cast(s) wg. compiler warnings (microsoft c 6.0) */
/* 03.11.95 - stat_2_modes(): for setting of file attributes in os_9 */
/* 10.12.95 - read_datafile(): FILE_SIZE_CHANGED */
/* 23.01.96 - write_datafile(): check return value fclose() */
/* 28.10.96 - stat_2_modes() stillgelegt */
/* 26.02.97 - os9: rename() with full path */
/* 04.02.99 - get_ / put_file_attributes: with uid + gid */
/* 04.03.99 - LONG_BAK_NAMES */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -