@@ -115,48 +115,57 @@ static inline int max( int a, int b )
115115 */
116116int establishDir ( char * path , int permissions )
117117{
118- int result = mkdir ( path , permissions );
119- if ( result == -1 )
120- {
121- char * lastSlash ;
122-
123- result = errno ;
124- switch (errno )
125- {
126- case ENOENT :
127- lastSlash = strrchr ( path , '/' );
128- if ( lastSlash != NULL )
129- {
130- * lastSlash = '\0' ;
131- result = establishDir ( path , permissions );
132- if ( result == 0 )
133- {
134- * lastSlash = '/' ;
135- result = mkdir ( path , permissions );
136- if ( result == -1 )
137- {
138- result = errno ;
139- }
140- }
141- }
142- break ;
143-
144- case EEXIST :
145- /* this 'error' is normal and expected */
146- result = 0 ;
147- /* just double-check that we can modify the existing directory */
148- if ( access ( path , W_OK | X_OK ) == -1 )
149- {
150- result = errno ;
151- fprintf ( stderr , "### unable to modify directory \"%s\" (%d: %s)\n" , path , errno , strerror (errno ));
152- }
153- break ;
154-
155- default :
156- fprintf ( stderr , "### unable to create directory \"%s\" (%d: %s)\n" , path , errno , strerror (errno ));
157- break ;
158- }
159- }
118+ int result = 0 ;
119+
120+ char * lastSlash ;
121+
122+ result = mkdir ( path , permissions );
123+ if ( result == -1 )
124+ {
125+ result = errno ;
126+ switch ( errno )
127+ {
128+ /* path doesn't exist, so lop off the last element and recurse until
129+ * it does, then create the missing elements as we unwind */
130+ case ENOENT :
131+ lastSlash = strrchr ( path , '/' );
132+ if ( lastSlash != NULL ) {
133+ * lastSlash = '\0' ;
134+ result = establishDir ( path , permissions );
135+ if ( result == 0 ) {
136+ * lastSlash = '/' ;
137+ result = mkdir ( path , permissions );
138+ if ( result == -1 ) {
139+ result = errno ;
140+ }
141+ }
142+ }
143+ break ;
144+
145+ case 0 : /* just created it successfully */
146+ case EEXIST : /* already exists - this 'error' is normal and expected */
147+ /* either one will unwind recursion */
148+ result = 0 ;
149+ /* just double-check that we can modify the existing directory */
150+ if ( access ( path , W_OK | X_OK ) == -1 ) {
151+ result = errno ;
152+ fprintf ( stderr ,
153+ "### unable to modify directory \"%s\" (%d: %s)\n" ,
154+ path ,
155+ errno ,
156+ strerror (errno ));
157+ }
158+ break ;
159+
160+ default : /* Anything else is a problem we're not expecting */
161+ fprintf ( stderr ,
162+ "### unable to create directory \"%s\" (%d: %s)\n" ,
163+ path ,
164+ errno ,
165+ strerror (errno ));
166+ break ;
167+ }
168+ }
160169
161170 return result ;
162171}
@@ -588,7 +597,7 @@ int processKeywords( config_t * config )
588597 if ( globals .reverseUnsetEntry == NULL )
589598 {
590599 fprintf ( globals .outputFile ,
591- " [ k%sUnset ] = \"Unknown \",\n" ,
600+ " [ k%sUnset ] = \"Unset \",\n" ,
592601 globals .prefix );
593602 }
594603 else
@@ -827,7 +836,6 @@ int main( int argc, char * argv[] )
827836 else
828837 {
829838 result = 0 ;
830- int i = 0 ;
831839
832840 globals .outputFile = NULL ;
833841
@@ -837,25 +845,21 @@ int main( int argc, char * argv[] )
837845 extension = * gOption .extn -> sval ;
838846 }
839847
840- while ( i < gOption .file -> count && result == 0 )
848+ for ( int i = 0 ; i < gOption .file -> count && result == 0 ; ++ i )
841849 {
842850 char output [FILENAME_MAX ];
843851 output [ 0 ] = '\0' ;
844852
845- const char * filename ;
846- if ( gOption .output -> count > 0 )
847- {
848- filename = gOption .output -> filename [ 0 ];
849- }
850- else
851- {
852- filename = gOption .file -> filename [ i ];
853- }
854-
855- result = establishDir ((char * )filename , kDirPerms );
853+ fprintf ( stderr , " input: %s\n" , gOption .file -> filename [ i ] );
854+ char * filename = strdup ( gOption .file -> filename [ i ] );
855+ char * path = dirname ( filename );
856+ if ( path != NULL && path [0 ] != '\0' && path [0 ] != '.' && path [0 ] != '/' && path [1 ] != '\0' )
857+ {
858+ result = establishDir ( path , kDirPerms );
859+ }
856860
857- char * base = strdup ( gOption .file -> basename [ i ] );
858- char * period = NULL ;
861+ char * base = strdup ( gOption .file -> basename [ i ] );
862+ char * period = NULL ;
859863 for ( char * p = base ; * p != '\0' ; ++ p )
860864 {
861865 switch ( * p )
@@ -868,9 +872,12 @@ int main( int argc, char * argv[] )
868872 }
869873 if ( period != NULL ) * period = '\0' ;
870874
871- snprintf ( output , sizeof ( output ), "%s/%s%s" , filename , base , extension );
875+ snprintf ( output , sizeof ( output ), "%s/%s%s" , path , base , extension );
872876 fprintf ( stderr , "output: %s\n" , output );
873877
878+ free ( filename );
879+ free ( base );
880+
874881 globals .outputFile = fopen ( output , "w" );
875882 if ( globals .outputFile == NULL )
876883 {
@@ -883,7 +890,6 @@ int main( int argc, char * argv[] )
883890 {
884891 result = processHashFile ( gOption .file -> filename [ i ] );
885892 }
886- i ++ ;
887893
888894 fclose ( globals .outputFile );
889895 }
0 commit comments