home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Black Box 4
/
BlackBox.cdr
/
batch
/
cutils10.arj
/
MKPATH.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-10-17
|
7KB
|
219 lines
/*+M, MkPath.c */
/************************************************************************/
/************************************************************************/
/* */
/* COPYRIGHT (C) G. KLYNE, 1991. All rights reserved. */
/* */
/* Permission to freely distribute this software, or use it */
/* for any purpose, is granted provided that no charge is */
/* levied for its distribution and this notice is retained */
/* in all copies of the source code. */
/* */
/************************************************************************/
/************************************************************************/
/* */
/* */
/* Module name : MkPath */
/* File name : MkPath.c */
/* */
/* */
/* AMENDMENT RECORD */
/* ================ */
/* */
/* 1. V01.0A 18-Feb-1991 Graham Klyne */
/* Program initially created. */
/* */
/* */
/* PROGRAM FUNCTION */
/* ================ */
/* */
/* This program is a simple DOS utility which creates directories */
/* required for a supplied file name. It checks each directory */
/* along the path to the specified file and creates them if they */
/* do not exist. */
/* */
/* The exit code is: */
/* 0 if no directory was named in the file specifier. */
/* 1 if the directory already existed. */
/* 2 if the directory was succesfully created. */
/* 4 if the directory could not be created for any reason. */
/* */
/* This program assumes that the current default directory does */
/* exist. */
/* */
/* */
/************************************************************************/
/************************************************************************/
/*-M*/
/************************************************************************/
/* External declarations used */
/************************************************************************/
#define LINT_ARGS
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
#include <io.h>
/************************************************************************/
/* External entry points declared in this module */
/************************************************************************/
/* No external entry points */
/************************************************************************/
/* Local definitions */
/************************************************************************/
/* No local definitions */
/************************************************************************/
/* Local type declarations */
/************************************************************************/
/* No local types */
/************************************************************************/
/* Local variable declarations */
/************************************************************************/
/* No local variables */
/*+F*/
/************************************************************************/
/* *-CheckDirectory, Create directory if it does not already exist */
/************************************************************************/
/*
/* This function tests to see if a directory with the supplied name
/* exists, and attempts to create it if it does not.
/*
/* PARAMETERS
/* ----------
/* Path points to a string containing the name of the
/* required directory.
/*
/* RESULT
/* ------
/* The result returned is:
/* 1 if the directory exists.
/* 2 if the directory was created.
/* 4 if the directory could not be created.
/*
/*-F*/
int CheckDirectory( char *Path )
{
/*--------------------------------------------------------------*/
/* Entry point here */
/*--------------------------------------------------------------*/
if ( access( Path, 0 ) == 0 )
{
return 1 ;
}
else
{
if ( mkdir( Path ) == 0 ) return 2 ;
printf( "MkPath: Failed to create directory '%s'\n", Path ) ;
}
return 4 ;
} ; // End of function CheckDirectory
/*+T*/
/************************************************************************/
/* *-main, Main Program */
/************************************************************************/
/*
/* This program reads commands from standard input and submits
/* them for execution, each in its own command shell.
/*
/*-T*/
void main( int argc, char *(argv[]), char *(envp[]) )
{
int Status ;
int PathNxt ;
int PathLen ;
char PathChr ;
char Path[128] ;
/*--------------------------------------------------------------*/
/* Entry point here */
/*--------------------------------------------------------------*/
Status = 0 ;
if ( argc < 2 )
{
printf( "Usage: MkPath file\n"
" Create all directories on path to 'file'.\n"
" Only the directories are created: any file\n"
" name or extension specified is ignored.\n"
"\n"
"Exit status: 0: file does not specify directory\n"
" 1: directory already exists\n"
" 2: directory created succesfully\n"
" 4: failed to create directory\n" ) ;
printf( "\n"
"Copyright (C) 1991 Graham Klyne. All rights reserved.\n" ) ;
exit( 0 ) ;
}
/*--------------------------------------------------------------*/
/* Isolate drive and root directory specifiers */
/*--------------------------------------------------------------*/
PathNxt = 0 ;
PathLen = 0 ;
PathChr = argv[1][PathNxt++] ;
if ( isalpha( PathChr ) && ( argv[1][PathNxt] == ':' ) )
{
Path[PathLen++] = PathChr ;
Path[PathLen++] = ':' ;
PathNxt++ ;
PathChr = argv[1][PathNxt++] ;
}
if ( ( PathChr == '\\' ) || ( PathChr == '/' ) )
{
Path[PathLen++] = PathChr ;
PathChr = argv[1][PathNxt++] ;
}
/*--------------------------------------------------------------*/
/* Isolate all directories on path, create as required */
/*--------------------------------------------------------------*/
while ( ( PathChr != '\0' ) && ( Status <= 2 ) )
{
if ( ( PathChr == '\\' ) || ( PathChr == '/' ) )
{
Path[PathLen] = '\0' ;
Status = CheckDirectory( Path ) ;
}
Path[PathLen++] = PathChr ;
PathChr = argv[1][PathNxt++] ;
}
/*--------------------------------------------------------------*/
/* Exit */
/*--------------------------------------------------------------*/
exit( Status ) ;
} ; /* End of procedure main */
/************************************************************************/
/* End of module MkPath */
/************************************************************************/