Last active
August 29, 2015 14:07
-
-
Save FedericoPonzi/9c2bfe99cc07e801dd4a to your computer and use it in GitHub Desktop.
This is a simple ls version using the win32api.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <windows.h> | |
#include <direct.h> | |
int main(int argc, char **argv) | |
{ | |
HANDLE handleFind; | |
WIN32_FIND_DATA findFileData; | |
char *dir; | |
//Se ho un argomento, allora prendo come dir la wd: | |
if (argc == 1) | |
{ | |
if ((dir = _getcwd(NULL, 0)) == NULL ) | |
{ | |
printf("_getcwd error: %x \n", GetLastError()); | |
} | |
} | |
// Altrimenti devo fare l'ls della path indicata dall' argomento | |
else if (argc == 2) | |
{ | |
dir = argv[1]; | |
} | |
if (argc > 2) | |
{ | |
printf("Usage: %s [path]", argv[0]); | |
return 2; | |
} | |
int pathSize = strlen(dir) + 3; //Trovo la lunghezza della path, + la wildcard | |
char path[pathSize]; // Mi creo un buffer, | |
memset(path, 0, pathSize); // E lo pulisco | |
strcat(path, dir); // Aggiungo la path | |
strcat(path, "/*"); // E la wildcard | |
handleFind = FindFirstFile(path, &findFileData); //Creo il findHandle | |
if (INVALID_HANDLE_VALUE == handleFind) | |
{ | |
printf("Error on HandleFile: %d\n", GetLastError()); | |
return 3; | |
} | |
//Stampo le path finche' ho files. | |
do | |
{ | |
printf(" %s\n", findFileData.cFileName); | |
} while (FindNextFile(handleFind, &findFileData) != 0); | |
if (!FindClose(handleFind)) | |
{ | |
printf("Error: %d", GetLastError()); | |
return 3; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment