io.c

来自「实现树形结构」· C语言 代码 · 共 103 行

C
103
字号
// For portability issues, these functions are still written in C

#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifndef _WINDOWS
#include <unistd.h>
#endif

/*
 * Tests if a file exists
 */
int file_exists(char* filename)
{
	struct stat st;
	int err = stat(filename, &st);
	return err==0 || errno != ENOENT;
}

/*
 * Returns the file size
 */
size_t file_size(char* filename)
{
	struct stat st;
	int err = stat(filename, &st);
	if( err!=0 )
		return 0xFFFFFFFF;
	else
		return st.st_size;
}

/*
 * Returns read only status of the file
 */
int file_read_only(char* filename)
{
#ifdef _WINDOWS
	struct stat st;
	int err = stat(filename, &st);
	if( err!=0 )
		return 0;
	else
		return (st.st_mode & _S_IWRITE) == 0;
#else
  int err = access(filename, W_OK);
  return err==0 ? 0 : 1;
#endif
}

/*
 * Returns the time of last modification of file
 */
time_t file_last_modification(char* filename)
{
	struct stat st;
	int err = stat(filename, &st);
	if( err!=0 )
		return 0xFFFFFFFF;
	else
		return st.st_mtime;
}

/*
 * Put a string to stderr
 */
void fputs_stderr(char* s)
{
	fputs(s, stderr);
}

/*
 * Returns the path separator
 */
/*char path_separator()
{
#ifdef _WINDOWS
  return '\\';
#else
  return '/';
#endif
}*/

/*
 * Convert a filename with '/' to a platform compatible filename
 */
/*void convert_full_name (char* filename)
{
#ifdef _WINDOWS
  char* p = filename;
  char c;
  while (1)
  {
    c = *p;
    if (c==0) break;
    if (c=='/') *p = '\\';
    ++p;
  }
#endif
}*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?