Various functions for drive, directory, file and permission
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Various functions for drive, directory, file and permission
To show: Playing with drive, directory, file and permission
/* Playing with drive, directory, file and permission */
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>
#include <io.h>
#include <time.h>
#include <sys.h>
#define SIZE 26
int main(void)
{
int curdrive;
char buffer[_MAX_PATH];
wchar_t buf[SIZE];
/* file name, the file is there */
char fname[20] = "namedpipeclient.doc";
/* path, the path is available */
char dir[50] = "e:\\testfolder";
struct _finddata_t c_file;
/* file handle */
long hFile;
/* ---------------------------------------------- */
printf("Firstly, get the current drive...\n");
curdrive = _getdrive();
printf("Current drive is %c:\n\n", curdrive + 'A'-1);
/* ----------------------------------------------- */
printf("Next, get the current working directory...\n");
printf("Current working directory is: \n");
if(_getcwd(buffer, _MAX_PATH) == NULL)
perror("_getcwd error lol!");
else
printf("%s\n\n", buffer);
/* ----------------------------------------------- */
printf("Change the current working directory...\n");
_chdir(dir);
printf("\n");
/* ------------------------------------------------ */
printf("Current working directory is: \n");
if(_getcwd(buffer, _MAX_PATH) == NULL)
perror("_getcwd() error");
else
printf("%s\n\n", buffer);
/* ------------------------------------------------ */
hFile = (long)_findfirst("*.*", &c_file);
{
printf("Listing of all files\n\n");
printf("\nRDO HID SYS ARC TYPE FILE DATE %19c SIZE\n", ' ');
printf("--- --- --- --- ---- ---- ---- %19c ----\n", ' ');
printf((c_file.attrib & _A_RDONLY) ? " Y " : " N ");
printf((c_file.attrib & _A_HIDDEN) ? " Y " : " N ");
printf((c_file.attrib & _A_SYSTEM) ? " Y " : " N ");
printf((c_file.attrib & _A_ARCH) ? " Y " : " N ");
printf((c_file.attrib & _A_NORMAL) ? " Y " : " N ");
/* ctime(&(c_file.time_write)); */
ctime_s((char *)buf, SIZE, &(c_file.time_write));
printf(" %-15s %.24s %6d\n", c_file.name, buf, c_file.size );
/* Find the rest of the files */
while(_findnext(hFile, &c_file) == 0)
{
printf((c_file.attrib & _A_RDONLY) ? " Y " : " N ");
printf((c_file.attrib & _A_HIDDEN) ? " Y " : " N ");
printf((c_file.attrib & _A_SYSTEM) ? " Y " : " N ");
printf((c_file.attrib & _A_ARCH) ? " Y " : " N ");
printf((c_file.attrib & _A_NORMAL) ? " Y " : " N ");
ctime_s((char *)buf, SIZE, &(c_file.time_write));
printf(" %-15s %.24s %6d\n", c_file.name, buf, c_file.size);
}
_findclose(hFile);
}
/* Check the file existence... */
if((_access(fname, 0)) != -1)
printf("\n%s file exists\n", fname);
else
{
printf("\n%s file not found lor!\n", fname);
/* just exit */
exit(1);
}
/* Check the file permission */
printf("\nTest the permission for %s file\n", fname);
/* If write, then change to read. Must check the write first
because file that can be write can also be read but not vice versa */
if((_access(fname, 2)) == 0)
{
printf("\n%s file has write permission!\n", fname);
printf("\nChange to read only permission...\n");
_chmod(fname, _S_IREAD);
printf("\nRetest the read only permission...\n");
if((_access(fname, 4)) == 0)
printf("%s file is READ ONLY!\n", fname);
exit(0);
}
/* if not write it should be read permission, then change
to write permission */
else
{
printf("\n%s file is READ ONLY!\n", fname);
printf("\nChange to write permission...\n");
_chmod(fname, _S_IWRITE);
printf("\nRetest for write permission...");
if((_access(fname, 2)) == 0)
printf("\n%s has write permission!\n", fname);
exit(0);
}
return 0;
}
Output example:
Firstly, get the current drive...
Current drive is F:
Next, get the current working directory...
Current working directory is:
f:\vc2005project\cplus\cplus
Change the current working directory...
Current working directory is:
e:\testfolder
Listing of all files
RDO HID SYS ARC TYPE FILE DATE SIZE
--- --- --- --- ---- ---- ---- ----
N N N N N . Tue Dec 12 22:10:48 2006 0
N N N N N .. Tue Dec 12 22:10:48 2006 0
Y N N Y N NamedPipeClient.doc Sat Nov 12 21:29:10 2005 111104
N N N Y N Pipes.doc Sun Oct 29 09:06:39 2006 221696
N N N Y N robots.txt Tue Dec 12 21:50:45 2006 0
Y N N Y N test.txt Tue Dec 12 21:23:43 2006 0
namedpipeclient.doc file exists
Test the permission for namedpipeclient.doc file
namedpipeclient.doc file is READ ONLY!
Change to write permission...
Retest for write permission...
namedpipeclient.doc has write permission!
Press any key to continue . . .