some diffs
Peter Jay Salzman
p at dirac.org
Fri Aug 9 18:32:58 EDT 2002
i wrote a function which may be nicer to debug with. to see it in
action, move ~/.twilight to something else, run twilight and quit it.
the code that complains "can't write config.cfg" now gives the file,
function and line number that generated the warning. let me know what
you think.
i also trap errors in Sys_mkdir(). it now provides a return value to
indicate success (which also includes failure because the directory
exists already) and "true" failure. :)
please let me know if you find this remotely useful.
pete
========= begin snip: sys.h
--- cvs-twilight-0.1.0/nq/sys.h 2001-09-08 23:50:06.000000000 -0700
+++ my-cvs-twilight-0.1.0/nq/sys.h 2002-08-09 15:16:07.000000000 -0700
@@ -46,7 +46,7 @@
int Sys_FileRead (int handle, void *dest, int count);
int Sys_FileWrite (int handle, void *data, int count);
int Sys_FileTime (char *path);
-void Sys_mkdir (char *path);
+int Sys_mkdir (char *path);
//
// system IO
@@ -55,6 +55,9 @@
void Sys_Error (char *error, ...);
+void Log_System_Error (int err, const char *file, const char *function,
+ const int line, const char *fmt, ...);
+
// an error will cause the entire program to exit
void Sys_Printf (char *fmt, ...);
========= end snip: sys.h
========= begin snip: sys_linux.c
--- cvs-twilight-0.1.0/nq/sys_linux.c 2001-09-12 23:50:05.000000000 -0700
+++ my-cvs-twilight-0.1.0/nq/sys_linux.c 2002-08-09 15:21:04.000000000 -0700
@@ -122,6 +122,21 @@
}
+
+
+void Log_System_Error (int err, const char *file, const char *function,
+ const int line, const char *fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ fprintf(stderr, "System error:\n file %s, function %s(), line %d\n ",
+ file, function, line);
+ vfprintf(stderr, fmt, args);
+ fprintf(stderr, "\n errno = %d: %s\n", err, strerror(err));
+}
+
+
+
void
Sys_Warn (char *warning, ...)
{
@@ -153,12 +168,24 @@
}
-void
+
+int
Sys_mkdir (char *path)
{
- mkdir (path, 0777);
+ int retval = mkdir (path, 0777);
+
+ /* Don't complain if it already exists */
+ if (retval == -1 && errno != EEXIST) {
+ Log_System_Error (errno, __FILE__, __FUNCTION__, __LINE__,
+ "Couldn't make new directory: %s", path);
+
+ return retval;
+ }
+
+ return 0;
}
+
int
Sys_FileOpenRead (char *path, int *handle)
{
@@ -187,7 +214,8 @@
handle = open (path, O_RDWR | O_CREAT | O_TRUNC, 0666);
if (handle == -1)
- Sys_Error ("Error opening %s: %s", path, strerror (errno));
+ Log_System_Error (errno, __FILE__, __FUNCTION__, __LINE__,
+ "Error opening %s", path);
return handle;
}
More information about the twilight-devel
mailing list