digitalmars.com                      
Last update Sun Mar 4 12:00:58 2018

direct.h


_chdir

Header
direct.h
errno.h (for error messages)
Prototype
int _chdir(char *path);
Description
The _chdir function changes the current directory to the one specified in the path argument. path must be a valid directory. The function can change the current working directory on any drive; however, it cannot change the default drive itself. For example, if you specify a drive in the path argument, the current directory on that drive is changed. However, until you use the _chdrive function to change to the new drive, the default directory is the current directory on the current drive.

The path string is limited by DOS to 64 characters including the terminal '\0'.

Synonym
Function: chdir
Return Value
0 if successful. Otherwise, -1 is returned, _doserrno is set to the operating system error code, and errno is set to the corresponding standard error code.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
_mkdir
_rmdir
Example
/* Example for _chdir */

#include <direct.h>
#include <stdio.h>
#include <stdlib.h>

void main()
{
 char changeto[64];
 int result;

 printf("Enter directory to change to :");
 scanf("%s", changeto);

 result = _chdir(changeto);

 if (result != 0)
    perror("ERROR changing directory");
 else
    printf("Current directory now :%s\n",
            changeto);
}
Output
Enter directory to change to :c:\windows
Current directory now :c:\windows
or
Enter directory to change to :c:\junk
ERROR changing directory: No such file or
directory

_chdrive

Header
direct.h
Prototype
int _chdrive(int drive);
Description
The _chdrive function changes the current drive to the drive specified in the drive argument. To specify a drive, use an integer. Value 1 indicates drive A, 2 indicates drive B, and so on. The _chdrive function changes only the working drive; use _chdir to change the current working directory.
Return Value
If the drive is successfully changed, the function returns the value 0. Otherwise, the function returns -1.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
_chdir
_dos_getdrive
_dos_setdrive
_getdrive
Example
/* Example for _chdrive  */

#include <direct.h>
#include <stdio.h>
#include <stdlib.h>

void main()
{
  if (_chdrive(2) == 0)
    printf("Changed to drive C:\n");
  else
    perror("ERROR changing to drive C:");
}
Output
Changed to drive C:

_getcwd

Header
direct.h
Prototype
char *_getcwd(char *buffer, size_t length);
Description
The _getcwd functions get the name of the current working directory and stores the drive and path name in buffer. The length argument specifies the maximum length of the path name (including the terminating null character.) If the current directory is root, the returned string will end with a backslash. Otherwise, the string will end with the directory name, not a backslash.

If buffer is specified as NULL, _getcwd will allocate, using the malloc function, enough bytes to hold the path (including the terminating 0). At least length bytes will be allocated. To deallocate the buffer, use the free function.

Return Value
Both functions return a pointer to buffer. A NULL return value indicates an error and errno is set to ENOMEM (insufficient memory to allocate length bytes) or ERANGE (path name longer than length characters).
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
_getdcwd
Example
/* Example for _getcwd */

#include <stdio.h>
#include <stdlib.h>
#include <direct.h>

void main()
{
  char path[_MAX_DIR];

  _getcwd(path, _MAX_DIR);
  printf("Current directory is: %s", path);
}
Output
Current directory is: C:\SC\EXAMPLES

_getdrive

Header
direct.h
Prototype
int _getdrive(void);
Description
The getdrive function returns the current drive number, where 1 represents A, 2 represents B, and so on.
Synonym
Function: getdrive
Return Value
An integer representing the current drive number. Errors are not returned.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
_dos_getdrive
_dos_getdrive
_getdcwd
Example
/* Example for _getdrive */

#include <direct.h>
#include <stdio.h>

void main()
{
  int disk;

  disk = _getdrive() + 'A' -1;
  printf("The current drive is %c\n", disk);
}
Output
The current drive is C

_mkdir

Header
direct.h
Prototype
int _mkdir(const char *pathname);
Description
The _mkdir function creates a new directory as specified by the pathname argument. If pathname contains more than one directory component, only the last component may be new. All preceding components must refer to existing directories.
Synonym
Function: mkdir
Return Value
Returns a 0 if the directory was created successfully. Returns -1 if an error occurred, and errno is set to EACCES (directory not created; given name already exists) or ENOENT (path name not found).
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
_rmdir
_chdir
Example
/* Example of _mkdir */

#include <stdio.h>
#include <direct.h>
#include <stdlib.h>

void main()
{
 int result;
 result = _mkdir("\\removeme");
 if (result == 0)
   printf("Directory \"\\removeme\" created\n");
 else
   printf("Could not create directory \"\\removeme\"\n");
}
Output
Directory "\removeme" created

_rmdir

Header
direct.h
Prototype
int _rmdir(const char *pathname);
Description
The _rmdir function deletes the directory specified by the pathname argument. The directory must be empty and cannot be the root directory or the current working directory. All the intermediate directories must also exist.
Synonym
Function: rmdir
Return Value
Both functions return a 0 if the directory was deleted. A return value of -1 indicates an error and errno is set to EACCES or ENOENT.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
_mkdir
_chdir
Example
/* Example of _rmdir */

#include <stdio.h>
#include <direct.h>
#include <stdlib.h>

void main()
{
  int result;
  char dir[_MAX_PATH];

  printf("Enter the directory name: ");
  gets(dir);

  result = _rmdir(dir);
  if (result != 0)
  {
     perror("Could not remove directory");
     exit(EXIT_FAILURE);
  }

  printf("Directory removed successfully\n");
}
Output
Enter the directory name: temp
Directory removed successfully

fnmerge

Header
direct.h
Prototype
void fnmerge(char *path, const char *drive,
const char *dir, const char *name, const char *ext);
Description
The fnmerge function builds a pathname by merging the components specified in the drive, dir, name, and ext arguments. The constructed pathname is stored in the location pointed to by the path argument.

The MAXPATH constant, defined as 80 in direct.h, specifies the maximum length of the constructed path.

Return Value
None
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
fnsplit
Example
/* Example for fnmerge
   Also demonstrates fnsplit
*/

#include <direct.h>
#include <stdio.h>
#include <dos.h>

void main()
{
 char *filename,
    path[MAXPATH],
    drive[MAXDRIVE],
    dir[MAXDIR],
    file[MAXFILE],
    ext[MAXEXT];

 fnsplit("c:\\dm\\bin\\dmc.exe",
	 drive, dir, file, ext);
 printf("drive: %s\n"
    "dir: %s\n"
    "filename: %s\n"
    "ext: %s\n",
    drive, dir, file, ext);
 fnmerge(path, drive, dir, file, ext);
 printf("path: %s\n", path);
}
Output
drive: c: dir: \dm\bin\
filename: dmc ext: .exe
path: c:\dm\bin\dmc.exe

fnsplit

Header
direct.h
Prototype
int fnsplit(const char *path, char *drive, char *dir, char *name, char *ext);
Description
The fnsplit function splits the pathname pointed to by the path argument into its components. The function stores the pathname components in the locations specified by the drive, dir, name, and ext arguments. Each pathname component has a maximum size, which is set by a constant, defined in direct.h. The constants for the pathname components and their values are shown in the following table. (All lengths include the space for a null terminator.)
Argument Constant Value
path MAXPATH 80
drive MAXDRIVE 3 (includes the colon)
dir MAXDIR 66 (includes leading and trailing backslashes)
name MAXFILE 9
ext MAXEXT 5 (includes the leading dot)
Return Value
An integer that uses the following five flags to indicate the components that were present in the path argument:

EXTENSION An extension
FILENAME A filename
DIRECTORY A directory and associated subdirectories
DRIVE A drive specification
WILDCARDS Wildcards
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
fnmerge
Example
See fnmerge

searchpath

Header
direct.h
Prototype
char *searchpath(const char *file);
Description
The searchpath function searches the DOS path for the file specified in the file argument. First, the current directory is searched. Then each directory specified in the DOS path is searched. When the file is found, the full pathname is returned.
Return Value
A pointer to the string containing the filename. If the file is not found, NULL is returned.
Compatibility
DOS Windows 3.x Phar Lap DOSX Win32
See Also
findfirst
findnext
Example
/* Example for searchpath */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dir.h>

void main()
{
 char *res;

 res = searchpath("DMC.EXE");

 if (strlen(res) == 0)
    printf("DMC.EXE not found\n");
 else
    printf("DMC.EXE found in %s\n", res);
}
Output
DMC.EXE found in C:\DM\BIN\DMC.EXE
Home | Compiler & Tools | IDDE Reference | STL | Search | Download | Forums