2019-11-23 03:40:23 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2019 by John D. Robertson *
|
|
|
|
* john@rrci.com *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
2019-11-29 22:23:16 +00:00
|
|
|
#define _GNU_SOURCE
|
2019-11-23 03:40:23 +00:00
|
|
|
#include <stdlib.h>
|
2019-12-03 12:55:15 +00:00
|
|
|
#include <sys/file.h>
|
2019-11-23 03:40:23 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
2019-11-29 14:00:39 +00:00
|
|
|
#include "ez_libc.h"
|
2019-11-23 03:40:23 +00:00
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_fputs (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *s,
|
|
|
|
FILE *stream
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= fputs (s, stream);
|
|
|
|
if (EOF == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fputs(\"%s\") failed", s);
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_fputc (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
int c,
|
|
|
|
FILE *stream
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= fputc (c, stream);
|
|
|
|
if (EOF == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fputc('%c') failed", (unsigned char)c);
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_fprintf (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
FILE *stream,
|
|
|
|
const char *fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
/* Pass on doctored format string and varargs to vfprintf() */
|
|
|
|
va_start(args, fmt);
|
|
|
|
int rtn= vfprintf(stream, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (0 > rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "vfprintf() failed");
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
FILE* _ez_popen (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *command,
|
|
|
|
const char *type
|
|
|
|
)
|
|
|
|
{
|
2019-12-04 02:38:51 +00:00
|
|
|
errno= 0;
|
2019-11-23 03:40:23 +00:00
|
|
|
FILE *rtn= popen (command, type);
|
2019-12-04 02:38:51 +00:00
|
|
|
if (!rtn || errno) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "popen(\"%s\", \"%s\") failed", command, type);
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
FILE* _ez_fopen (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *pathname,
|
|
|
|
const char *mode
|
|
|
|
)
|
|
|
|
{
|
|
|
|
FILE *rtn= fopen (pathname, mode);
|
|
|
|
if (!rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fopen(\"%s\", \"%s\") failed", pathname, mode);
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_fclose (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
FILE *stream
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= fclose (stream);
|
|
|
|
if (EOF == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fclose() failed");
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:19:22 +00:00
|
|
|
/***************************************************/
|
|
|
|
int _ez_fflush (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-02 15:19:22 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-02 15:19:22 +00:00
|
|
|
FILE *stream
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= fflush (stream);
|
|
|
|
if (EOF == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fflush() failed");
|
2019-12-02 15:19:22 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:40:23 +00:00
|
|
|
/***************************************************/
|
|
|
|
size_t _ez_fread (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
void *ptr,
|
|
|
|
size_t size,
|
|
|
|
size_t nmemb,
|
|
|
|
FILE *stream
|
|
|
|
)
|
|
|
|
{
|
|
|
|
size_t rtn= fread (ptr, size, nmemb, stream);
|
|
|
|
if (ferror(stream)) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fread() failed");
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
size_t _ez_fwrite (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
const void *ptr,
|
|
|
|
size_t size,
|
|
|
|
size_t nmemb,
|
|
|
|
FILE *stream
|
|
|
|
)
|
|
|
|
{
|
|
|
|
size_t rtn= fwrite (ptr, size, nmemb, stream);
|
|
|
|
if (ferror(stream)) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fwrite() failed");
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_pclose (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
FILE *stream
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= pclose (stream);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "pclose() failed");
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
char* _ez_fgets (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
char *s,
|
|
|
|
int size,
|
|
|
|
FILE *stream
|
|
|
|
)
|
|
|
|
{
|
|
|
|
char *rtn= fgets (s, size, stream);
|
|
|
|
if (!rtn && !feof(stream)) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fgets() failed");
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_remove (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *pathname
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= remove (pathname);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "remove(\"%s\") failed", pathname);
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_rename (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-23 03:40:23 +00:00
|
|
|
const char *oldpath,
|
|
|
|
const char *newpath
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= rename (oldpath, newpath);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "rename(\"%s\", \"%s\") failed", oldpath, newpath);
|
2019-11-23 03:40:23 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
2019-11-29 14:00:39 +00:00
|
|
|
/***************************************************/
|
|
|
|
DIR* _ez_opendir (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *name
|
|
|
|
)
|
|
|
|
{
|
|
|
|
DIR *rtn= opendir (name);
|
|
|
|
if (!rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "opendir(\"%s\") failed", name);
|
2019-11-29 14:00:39 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_closedir (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 14:00:39 +00:00
|
|
|
DIR *dirp
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= closedir (dirp);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "closedir() failed");
|
2019-11-29 14:00:39 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
struct dirent* _ez_readdir (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 14:00:39 +00:00
|
|
|
DIR *dirp
|
|
|
|
)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Pass on doctored format string and varargs to vfprintf() */
|
|
|
|
errno= 0;
|
|
|
|
struct dirent* rtn= readdir(dirp);
|
|
|
|
|
|
|
|
if (!rtn && errno) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "readdir() failed");
|
2019-11-29 14:00:39 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_close (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 14:00:39 +00:00
|
|
|
int fd
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= close (fd);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "close(%d) failed", fd);
|
2019-11-29 14:00:39 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
ssize_t _ez_write (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 14:00:39 +00:00
|
|
|
int fd,
|
|
|
|
const void *buf,
|
|
|
|
size_t count
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ssize_t rtn= write (fd, buf, count);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "write(fd= %d) failed", fd);
|
2019-11-29 14:00:39 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_stat (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *pathname,
|
|
|
|
struct stat *statbuf
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= stat (pathname, statbuf);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "stat(\"%s\") failed", pathname);
|
2019-11-29 14:00:39 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_mkdir (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *pathname,
|
|
|
|
mode_t mode
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= mkdir (pathname, mode);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "mkdir(\"%s\", %04x) failed", pathname, (unsigned)mode);
|
2019-11-29 14:00:39 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_rmdir (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *pathname
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= rmdir (pathname);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "rmdir(\"%s\") failed", pathname);
|
2019-11-29 14:00:39 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_unlink (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 14:00:39 +00:00
|
|
|
const char *pathname
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= unlink (pathname);
|
|
|
|
if (-1 == rtn) {
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "unlink(\"%s\") failed", pathname);
|
2019-11-29 14:00:39 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return rtn;
|
|
|
|
}
|
2019-11-29 22:23:16 +00:00
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_getaddrinfo(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 22:23:16 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 22:23:16 +00:00
|
|
|
const char *node,
|
|
|
|
const char *service,
|
|
|
|
const struct addrinfo *hints,
|
|
|
|
struct addrinfo **res
|
|
|
|
)
|
|
|
|
{
|
|
|
|
errno= 0;
|
|
|
|
int rtn= getaddrinfo (node, service, hints, res);
|
|
|
|
switch(rtn) {
|
|
|
|
case 0:
|
|
|
|
case EAI_AGAIN:
|
|
|
|
case EAI_FAIL:
|
|
|
|
case EAI_NODATA:
|
2019-11-30 19:14:42 +00:00
|
|
|
case EAI_NONAME:
|
2019-11-29 22:23:16 +00:00
|
|
|
return rtn;
|
|
|
|
|
|
|
|
case EAI_SYSTEM:
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "getaddrinfo(\"%s:%s\") failed", node, service);
|
2019-11-29 22:23:16 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* _sys_eprintf() will pass errno to gai_sterror */
|
|
|
|
errno= rtn;
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf(gai_strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "getaddrinfo(\"%s:%s\") failed", node, service);
|
2019-11-29 22:23:16 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_getnameinfo(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-11-29 22:23:16 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-11-29 22:23:16 +00:00
|
|
|
const struct sockaddr *addr,
|
|
|
|
socklen_t addrlen,
|
|
|
|
char *host,
|
|
|
|
socklen_t hostlen,
|
|
|
|
char *serv,
|
|
|
|
socklen_t servlen,
|
|
|
|
int flags
|
|
|
|
)
|
|
|
|
{
|
|
|
|
errno= 0;
|
|
|
|
int rtn= getnameinfo (addr, addrlen, host, hostlen, serv, servlen, flags);
|
|
|
|
switch(rtn) {
|
|
|
|
case 0:
|
|
|
|
case EAI_AGAIN:
|
|
|
|
case EAI_FAIL:
|
|
|
|
case EAI_NONAME:
|
|
|
|
return rtn;
|
|
|
|
|
|
|
|
case EAI_SYSTEM:
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "getnameinfo() failed");
|
2019-11-29 22:23:16 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* _sys_eprintf() will pass errno to gai_sterror */
|
|
|
|
errno= rtn;
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf(gai_strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "getnameinfo() failed", rtn);
|
2019-11-29 22:23:16 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-12-03 12:55:15 +00:00
|
|
|
/***************************************************/
|
|
|
|
int _ez_flock (
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-03 12:55:15 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-03 12:55:15 +00:00
|
|
|
int fd,
|
|
|
|
int operation
|
|
|
|
)
|
|
|
|
{
|
|
|
|
errno= 0;
|
|
|
|
int rtn= flock (fd, operation);
|
|
|
|
if(0 == rtn) return 0;
|
|
|
|
|
|
|
|
switch(errno) {
|
|
|
|
case EINTR:
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
return rtn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "flock() failed");
|
2019-12-03 12:55:15 +00:00
|
|
|
abort();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_open(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-03 12:55:15 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-03 12:55:15 +00:00
|
|
|
const char *pathname,
|
|
|
|
int flags,
|
|
|
|
mode_t mode
|
|
|
|
)
|
|
|
|
{
|
|
|
|
errno= 0;
|
|
|
|
int rtn= open (pathname, flags, mode);
|
|
|
|
if(0 <= rtn) return rtn;
|
|
|
|
|
|
|
|
switch(errno) {
|
|
|
|
case EINTR:
|
|
|
|
case EWOULDBLOCK:
|
|
|
|
return rtn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "open(\"%s\") failed", pathname);
|
2019-12-03 12:55:15 +00:00
|
|
|
abort();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-04 02:38:51 +00:00
|
|
|
/***************************************************/
|
2019-12-03 12:55:15 +00:00
|
|
|
int _ez_access(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-03 12:55:15 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-03 12:55:15 +00:00
|
|
|
const char *pathname,
|
|
|
|
int mode
|
|
|
|
)
|
|
|
|
{
|
|
|
|
errno= 0;
|
|
|
|
int rtn= access (pathname, mode);
|
|
|
|
if(0 == rtn) return rtn;
|
|
|
|
|
|
|
|
switch(errno) {
|
|
|
|
case ENOENT:
|
|
|
|
return rtn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "access(\"%s\") failed", pathname);
|
2019-12-03 12:55:15 +00:00
|
|
|
abort();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-04 02:38:51 +00:00
|
|
|
/***************************************************/
|
|
|
|
char *_ez_strptime(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *s,
|
|
|
|
const char *format,
|
|
|
|
struct tm *tm
|
|
|
|
)
|
|
|
|
{
|
|
|
|
char *rtn= strptime (s, format, tm);
|
|
|
|
if(rtn) return rtn;
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_eprintf(
|
|
|
|
#ifdef DEBUG
|
|
|
|
fileName, lineNo, funcName,
|
|
|
|
#endif
|
|
|
|
"strptime(\"%s\", \"%s\") failed", s, format);
|
2019-12-04 02:38:51 +00:00
|
|
|
abort();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_seteuid(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-04 02:38:51 +00:00
|
|
|
uid_t euid
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= seteuid (euid);
|
|
|
|
if(0 == rtn) return 0;
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "seteuid(%d) failed", (int)euid);
|
2019-12-04 02:38:51 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_setegid(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-04 02:38:51 +00:00
|
|
|
gid_t egid
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= setegid (egid);
|
|
|
|
if(0 == rtn) return 0;
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "setegid(%d) failed", (int)egid);
|
2019-12-04 02:38:51 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
struct group* _ez_getgrnam(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *name
|
|
|
|
)
|
|
|
|
{
|
|
|
|
errno= 0;
|
|
|
|
struct group *rtn= getgrnam (name);
|
|
|
|
|
|
|
|
if(rtn) return rtn;
|
|
|
|
|
|
|
|
switch(errno) {
|
|
|
|
case EINTR:
|
|
|
|
case EIO:
|
|
|
|
case EMFILE:
|
|
|
|
case ENFILE:
|
|
|
|
case ENOMEM:
|
|
|
|
case ERANGE:
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "getgrnam(\"%s\") failed", name);
|
2019-12-04 02:38:51 +00:00
|
|
|
abort();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_chown(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *pathname,
|
|
|
|
uid_t owner,
|
|
|
|
gid_t group
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= chown (pathname, owner, group);
|
|
|
|
if(0 == rtn) return rtn;
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "chown(\"%s\", %d, %d) failed", pathname, (int)owner, (int)group);
|
2019-12-04 02:38:51 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_fchown(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-04 02:38:51 +00:00
|
|
|
int fd,
|
|
|
|
uid_t owner,
|
|
|
|
gid_t group
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= fchown (fd, owner, group);
|
|
|
|
if(0 == rtn) return rtn;
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fchown() failed");
|
2019-12-04 02:38:51 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_fchmod(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-04 02:38:51 +00:00
|
|
|
int fd,
|
|
|
|
mode_t mode
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= fchmod (fd, mode);
|
|
|
|
if(0 == rtn) return rtn;
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "fchmod() failed");
|
2019-12-04 02:38:51 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_setuid(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-04 02:38:51 +00:00
|
|
|
uid_t uid
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= setuid (uid);
|
|
|
|
if(0 == rtn) return 0;
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "setuid(%d) failed", (int)uid);
|
2019-12-04 02:38:51 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
int _ez_setgid(
|
2019-12-06 13:44:51 +00:00
|
|
|
#ifdef DEBUG
|
2019-12-04 02:38:51 +00:00
|
|
|
const char *fileName,
|
|
|
|
int lineNo,
|
|
|
|
const char *funcName,
|
2019-12-06 13:44:51 +00:00
|
|
|
#endif
|
2019-12-04 02:38:51 +00:00
|
|
|
gid_t gid
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int rtn= setgid (gid);
|
|
|
|
if(0 == rtn) return 0;
|
|
|
|
|
2019-12-04 14:11:39 +00:00
|
|
|
_sys_eprintf((const char*(*)(int))strerror
|
|
|
|
#ifdef DEBUG
|
|
|
|
, fileName, lineNo, funcName
|
|
|
|
#endif
|
|
|
|
, "setgid(%d) failed", (int)gid);
|
2019-12-04 02:38:51 +00:00
|
|
|
abort();
|
|
|
|
}
|