Skip to content

Instantly share code, notes, and snippets.

@chm0815
Created February 4, 2016 20:16
Show Gist options
  • Save chm0815/d2d2fddfc15a268aff80 to your computer and use it in GitHub Desktop.
Save chm0815/d2d2fddfc15a268aff80 to your computer and use it in GitHub Desktop.
windows commandline tool to list (removable) drives
/* list drives */
/* Christoph Maurer */
/* 22.05.2009 */
#include <windows.h>
#include <stdio.h>
char * driveTypeText(char drive)
{
char *rv = NULL;
int drive_type = 0;
char root_path[4];
root_path[0] = drive;
root_path[1] = ':';
root_path[2] = '\\';
root_path[3] = '\0';
drive_type = GetDriveTypeA(root_path);
switch (drive_type) {
case DRIVE_UNKNOWN:
rv = "UNKNOWN";
break;
case DRIVE_NO_ROOT_DIR:
rv = "NO_ROOT_DIR";
break;
case DRIVE_REMOVABLE:
rv = "REMOVEABLE";
break;
case DRIVE_FIXED:
rv = "FIXED";
break;
case DRIVE_REMOTE:
rv = "REMOTE";
break;
case DRIVE_CDROM:
rv = "CDROM";
break;
case DRIVE_RAMDISK:
rv = "RAMDISK";
break;
default:
rv = "UNKNOWN";
break;
}
return rv;
}
void usage(void)
{
puts("Usage: lsd [/type]");
puts("lsd - list drives\n");
exit(0);
}
int main(int argc,char **argv)
{
unsigned int drives_mask = GetLogicalDrives();
unsigned int act_bit = 0;
char drive_letter = 'A';
int type = FALSE;
if (argc==2) {
if (!strcmp(argv[1],"/type")) {
type = TRUE;
} else {
usage();
}
} else if (argc>2) {
usage();
}
if (drives_mask) {
for (;act_bit<26;drive_letter++,act_bit++) {
if (drives_mask & (1<<act_bit)) {
if (type)
printf("%c:\\\t%s\n",drive_letter,driveTypeText(drive_letter));
else
printf("%c:\\\n",drive_letter);
}
}
} else {
fprintf(stderr,"Error calling GetLogicalDrives: GetLastError()=%d\n",GetLastError());
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment