Mercurial > noffle
comparison 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 |
comparison
equal
deleted
inserted
replaced
462:de7f674d1224 | 463:95697d7c97a1 |
---|---|
1 /* | 1 /* |
2 util.c | 2 util.c |
3 | 3 |
4 $Id: util.c 427 2003-01-31 15:35:14Z bears $ | 4 $Id: util.c 608 2003-07-23 09:31:01Z bears $ |
5 */ | 5 */ |
6 | 6 |
7 #if HAVE_CONFIG_H | 7 #if HAVE_CONFIG_H |
8 #include <config.h> | 8 #include <config.h> |
9 #endif | 9 #endif |
601 if ( strlen( result ) != 0 ) | 601 if ( strlen( result ) != 0 ) |
602 return TRUE; | 602 return TRUE; |
603 return getHostFQDN( result ); | 603 return getHostFQDN( result ); |
604 } | 604 } |
605 | 605 |
606 Bool | |
607 Utl_createDir( const char *dir ) | |
608 { | |
609 struct stat st; | |
610 int err; | |
611 | |
612 err = stat( dir, &st ); | |
613 if ( err == 0 && S_ISDIR( st.st_mode ) ) | |
614 return TRUE; | |
615 | |
616 err = mkdir( dir, 0755 ); | |
617 if ( err != 0 ) | |
618 { | |
619 Log_dbg( LOG_DBG_NEWSBASE, "mkdir: %s", strerror( errno ) ); | |
620 return FALSE; | |
621 } | |
622 | |
623 return TRUE; | |
624 } | |
625 | |
626 | |
627 Bool | |
628 Utl_writeFile( const char *dirPrefix, const char *dir, | |
629 const char *file, const char *text) | |
630 { | |
631 Str dirPath; | |
632 Str filePath; | |
633 FILE *f; | |
634 | |
635 if ( dirPrefix != NULL ) | |
636 { | |
637 if ( ! Utl_createDir( dirPrefix ) ) | |
638 return FALSE; | |
639 snprintf( dirPath, MAXCHAR, "%s/%s", dirPrefix, dir ); | |
640 } | |
641 else | |
642 Utl_cpyStr( dirPath, dir ); | |
643 | |
644 if ( ! Utl_createDir( dirPath ) ) | |
645 return FALSE; | |
646 | |
647 snprintf( filePath, MAXCHAR, "%s/%s", dirPath, file); | |
648 | |
649 if ( ! ( f = fopen( filePath, "w" ) ) ) | |
650 { | |
651 Log_err( "Cannot open %s", filePath ); | |
652 return FALSE; | |
653 } | |
654 fprintf( f, "%s", text ); | |
655 fclose( f ); | |
656 return TRUE; | |
657 | |
658 } | |
659 | |
660 | |
606 #if defined(UTIL_TEST) | 661 #if defined(UTIL_TEST) |
607 | 662 |
608 /* Test code borrowed from wildmat.c. Yep, still uses gets(). */ | 663 /* Test code borrowed from wildmat.c. Yep, still uses gets(). */ |
609 extern char *gets(); | 664 extern char *gets(); |
610 | 665 |