diff src/util.c @ 463:95697d7c97a1 noffle

[svn] * src/outgoing.c,src/util.h,src/util.c: Create Utl_createDir() and Utl_writeFile() and rework outgoing.c to use them.
author bears
date Wed, 23 Jul 2003 10:31:01 +0100
parents 4426fde0a72c
children
line wrap: on
line diff
--- a/src/util.c	Wed Jul 23 10:26:49 2003 +0100
+++ b/src/util.c	Wed Jul 23 10:31:01 2003 +0100
@@ -1,7 +1,7 @@
 /*
   util.c
 
-  $Id: util.c 427 2003-01-31 15:35:14Z bears $
+  $Id: util.c 608 2003-07-23 09:31:01Z bears $
 */
 
 #if HAVE_CONFIG_H
@@ -603,6 +603,61 @@
     return getHostFQDN( result );
 }
 
+Bool
+Utl_createDir( const char *dir )
+{
+    struct stat st;
+    int err;
+
+    err = stat( dir, &st );
+    if ( err == 0 && S_ISDIR( st.st_mode ) )
+	return TRUE;
+
+    err = mkdir( dir, 0755 );
+    if ( err != 0 )
+    {
+	Log_dbg( LOG_DBG_NEWSBASE, "mkdir: %s", strerror( errno ) );
+	return FALSE;
+    }
+
+    return TRUE;
+}
+
+
+Bool
+Utl_writeFile( const char *dirPrefix, const char *dir,
+	       const char *file, const char *text)
+{
+    Str dirPath;
+    Str filePath;
+    FILE *f;
+
+    if ( dirPrefix != NULL )
+    {
+	if ( ! Utl_createDir( dirPrefix ) )
+	    return FALSE;
+	snprintf( dirPath, MAXCHAR, "%s/%s", dirPrefix, dir );
+    }
+    else
+	Utl_cpyStr( dirPath, dir );
+    
+    if ( ! Utl_createDir( dirPath ) )
+	return FALSE;
+
+    snprintf( filePath, MAXCHAR, "%s/%s", dirPath, file);
+    
+    if ( ! ( f = fopen( filePath, "w" ) ) )
+    {
+	Log_err( "Cannot open %s", filePath );
+	return FALSE;
+    }
+    fprintf( f, "%s", text );
+    fclose( f );
+    return TRUE;
+    
+}
+
+
 #if defined(UTIL_TEST)
 
 /* Test code borrowed from wildmat.c. Yep, still uses gets(). */