dimanche 12 juillet 2009

What's in the beast?

A (simplified) du(1) utility (recursive walk of the directory structure)

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <windows.h>
/*
** CarTrek 600 a kind of du(1) utility (disk usage)
** what's in the beast...
*/
#define prog_name TEXT("du")
void msg(char *str1, char *str2)
{
static char txtbuf[512];
static wchar_t wtxtbuf[512];

sprintf(txtbuf, "%s %s (GetLastError()=%ld)", str1, str2, GetLastError());
mbstowcs(&wtxtbuf[0], &txtbuf[0], strlen(txtbuf)+1);
MessageBox(0, &wtxtbuf[0], prog_name, MB_OK);
}
/*===================================================================*/
/* LOG module
**
*/
char *LOG_fname="\\SDMMC\\du.txt";
FILE *LOG_stream;
int LOG_init()
{
if ((LOG_stream=fopen(LOG_fname, "a+")) == NULL)
{
msg("Can't fopen()", LOG_fname);
return(0);
}
fprintf(LOG_stream, "===== New log =====\n");
return(1);
}
int LOG_close()
{
return(fclose(LOG_stream));
}
int LOG_write(char *buf, int len)
{
if (fwrite(buf, 1, len, LOG_stream) != len)
msg("Can't write LOG_stream", LOG_fname);
return(len);
}
/*===================================================================*/
#define BUF_SIZE 512
int list_dir(FILE *fp, char *dir_name)
{
DIR *dirp;
struct dirent *direntp;
char buf[BUF_SIZE];
int len0;

fprintf(fp, "%s\n", dir_name);
if ((dirp = opendir(dir_name)) == NULL)
{
// fprintf(fp, "Can't opendir %s\n", dir_name);
return(0);
}
len0 = strlen(dir_name);
while ((direntp = readdir(dirp)) != NULL)
{
direntp->d_name[direntp->d_namlen] = '\0'; /* to be sure */
// fprintf(fp, "%s/%s\n", dir_name, direntp->d_name);
if (strcmp(direntp->d_name, "..") && strcmp(direntp->d_name, "."))
{
if ((len0 + strlen(direntp->d_name)) < (BUF_SIZE-2))
{
if (dir_name[0] == '/' && dir_name[1] == '\0')
sprintf(buf, "/%s", direntp->d_name); /* no double-slash */
else sprintf(buf, "%s/%s", dir_name, direntp->d_name);
list_dir(fp, buf);
}
else {
fprintf(fp, "OVERFLOW %s/%s", dir_name, direntp->d_name);
return(0);
}
}
}
closedir(dirp);
return(1);
}
/*===================================================================*/
#define MAXLOG (100)
volatile int running;
DWORD stop_box_threadId;
DWORD WINAPI stop_box(LPVOID lParam)
{
msg("Stop?", "");
running = 0;
return(0);
}
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
wchar_t argv0[80];
char buf[512];
int n, count;

GetModuleFileName (NULL, argv0, sizeof (argv0) / sizeof (argv0[0]));
MessageBox(0, argv0, L"CeGCC says...", MB_OK);

running = 1;
CreateThread (NULL, 0, stop_box, NULL, 0, &stop_box_threadId);
if (LOG_init())
{
list_dir(LOG_stream, "/");
}
LOG_close();
}

Gives something like (310 'files'):

/Network/.
/Network/..
/ResidentFlash/.
/ResidentFlash/..
/ResidentFlash/gpspara0.bin
/ResidentFlash/gpspara1.bin
/ResidentFlash/gpspara2.bin
...
/SDMMC/.
/SDMMC/..
/SDMMC/gosm_arm.exe
...
/SDMMC/gosmore.pak
/SDMMC/MobileNavigator
/SDMMC/MobileNavigator/.
/SDMMC/MobileNavigator/..
...
/Windows/Desktop
/Windows/Desktop/.
/Windows/Desktop/..
/Windows/Desktop/desktop.ini
/Windows/TempI2C.dll
/Windows/PwrButton.dll
/Windows/wavedev.dll
/Windows/usbotg.dll
/Windows/SerialUSBFn.dll
/Windows/gps.dll
...

If I just look at /Windows/*.exe :

/Windows/ceplayer.exe
/Windows/connmc.exe
/Windows/control.exe
/Windows/CPITGuider.exe
/Windows/ctlpnl.exe
/Windows/device.exe
/Windows/eventrst.exe
/Windows/explorer.exe
/Windows/filesys.exe
/Windows/GpsMC.exe
/Windows/gwes.exe
/Windows/iexplore.exe
/Windows/IrDARmtCtrl_Daemon.exe
/Windows/launch.exe
/Windows/nk.exe
/Windows/OVL_Manager.exe
/Windows/rapisrv.exe
/Windows/repllog.exe
/Windows/rnaapp.exe
/Windows/shell.exe
/Windows/udp2tcp.exe
/Windows/unload.exe
/Windows/wceload.exe

But, of course, .dll are may be more importants... There are 123 of them.