dir.c File Reference

Genericised directory functions. More...

#include "ctl/ctldef.h"
#include "ctl/dir.h"
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include "ctl/ctlstring.h"
#include "unit/unit.h"

Include dependency graph for dir.c:

Go to the source code of this file.

Functions

char * ctl_cwd (char *buffer, size_t cMax)
 Get current working directory.
bool ctl_chdir (const char *path)
 Change working directory.
void ctl_move (const char *src, const char *dst)
 Change an existing file's name and/or location.
void ctl_copy (const char *src, const char *dst)
 Copy a file.
void ctl_unlink (const char *path)
 Remove a file.
void ctl_rmdir (const char *path)
 Remove an EMPTY directory.
void ctl_mkdir (const char *path)
 Make a directory.


Detailed Description

Genericised directory functions.

Author:
David Mace

Definition in file dir.c.


Function Documentation

bool ctl_chdir ( const char *  path  ) 

Change working directory.

Parameters:
path New path to change app's working directory to

Definition at line 65 of file dir.c.

References assertconst.

00066 {
00067     assertconst(path,1);
00068 #ifdef WIN32
00069     return (bool)SetCurrentDirectory(path);
00070 #else
00071     return (bool)(0 != chdir(path));
00072 #endif
00073 }

void ctl_copy ( const char *  src,
const char *  dst 
)

Copy a file.

Parameters:
src Path to original file
dst Path to destination, including the file name

Definition at line 98 of file dir.c.

References assertconst.

00099 {
00100     assertconst(src,1);
00101     assertconst(dst,1);
00102 #ifdef WIN32
00103     CopyFile( src, dst, FALSE );
00104 #else
00105     int srcfd = open ( src, O_RDONLY|O_BINARY );
00106     if (srcfd >= 0)
00107     {
00108         int destfd = open( dst, O_CREAT|O_TRUNC|O_RDWR|O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
00109         if (destfd >= 0)
00110         {
00111             char buff[32768];
00112             ssize_t nread = read(srcfd, buff, sizeof (buff));
00113             ssize_t nwrote;
00114             while (nread > 0)
00115             {
00116                 nwrote = write(destfd, buff, nread);
00117                 if( nwrote != nread )
00118                     break;
00119                 nread = read(srcfd, buff, sizeof (buff));
00120             }
00121             close(destfd);
00122         }
00123         close(srcfd);
00124     }
00125 #endif
00126 }

char* ctl_cwd ( char *  buffer,
size_t  cMax 
)

Get current working directory.

Parameters:
buffer Character string to receive working folder
cMax Maximum count of characters

Definition at line 35 of file dir.c.

References assertptr.

00036 {
00037     char* result;
00038     assertptr(buffer,cMax);
00039 #ifdef WIN32
00040     GetCurrentDirectory( (DWORD)cMax, buffer );
00041 #else
00042     result = getcwd( buffer, cMax );
00043 #endif
00044     if( NULL != result )
00045     {
00046         char* p = ctl_eattrailingslash( buffer );
00047 #ifdef WIN32
00048         if( (size_t)(p - buffer) < cMax )
00049             *p++ = '\\';
00050 #else
00051         if( (size_t)(p - buffer) < cMax )
00052             *p++ = '/';
00053 #endif
00054         if( (size_t)(p - buffer) < cMax )
00055             *p = 0;
00056     }
00057     return result;
00058 }

void ctl_mkdir ( const char *  path  ) 

Make a directory.

Parameters:
path Path to create

Definition at line 163 of file dir.c.

References assertconst.

00164 {
00165     assertconst(path,1);
00166 #ifdef WIN32
00167     CreateDirectory(path,NULL);
00168 #else
00169     /* Let this user and his whole group play with it (assuming umask permits it) */
00170     mkdir( path, S_IRWXU | S_IRWXG );
00171 #endif
00172 }

void ctl_move ( const char *  src,
const char *  dst 
)

Change an existing file's name and/or location.

Parameters:
src Path to original file
dst Path to destination, including the file name

Definition at line 81 of file dir.c.

References assertconst.

00082 {
00083     assertconst(src,1);
00084     assertconst(dst,1);
00085 #ifdef WIN32
00086     MoveFileEx( src, dst, MOVEFILE_COPY_ALLOWED|MOVEFILE_REPLACE_EXISTING );
00087 #else
00088     rename( src, dst );
00089 #endif
00090 }

void ctl_rmdir ( const char *  path  ) 

Remove an EMPTY directory.

Parameters:
path Path to remove (won't remove it if path isn't empty)

Definition at line 148 of file dir.c.

References assertconst.

00149 {
00150     assertconst(path,1);
00151 #ifdef WIN32
00152     RemoveDirectory(path);
00153 #else
00154     rmdir(path);
00155 #endif
00156 }

void ctl_unlink ( const char *  path  ) 

Remove a file.

Parameters:
path Path to destroy

Definition at line 133 of file dir.c.

References assertconst.

00134 {
00135     assertconst(path,1);
00136 #ifdef WIN32
00137     DeleteFile( path );
00138 #else
00139     unlink( path );
00140 #endif
00141 }


Generated on Fri Jan 2 15:28:35 2009 for Squat by  doxygen 1.5.6