Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

wraptest.c 3.51 KB
Newer Older
1 2
/* wraptest.c */

3 4
/* Verification of cross-platform development wrappers */

5 6 7 8 9 10 11 12 13
#include <time.h>	/* ctime */

#include "genwrap.h"
#include "conwrap.h"
#include "dirwrap.h"
#include "filewrap.h"
#include "sockwrap.h"
#include "threadwrap.h"

14
static void getkey(void);
rswindell's avatar
rswindell committed
15
static void thread_test(void* arg);
16 17 18 19 20 21

typedef struct {
	sem_t parent_sem;
	sem_t child_sem;
} thread_data_t;

22 23
int main()
{
24
	char	str[128];
25
	char	compiler[128];
rswindell's avatar
rswindell committed
26
	char	fullpath[MAX_PATH+1];
27 28
	char*	path = ".";
	char*	glob_pattern = "*wrap*";
29
	int		i;
30
	int		ch;
31 32 33 34 35
	uint	u;
	time_t	t;
	glob_t	g;
	DIR*	dir;
	DIRENT*	dirent;
36
	thread_data_t thread_data;
37

38 39
	/* Show platform details */
	DESCRIBE_COMPILER(compiler);
40 41 42 43
	printf("%-15s: %s\n","Platform",PLATFORM_DESC);
	printf("%-15s: %s\n","Version",os_version(str));
	printf("%-15s: %s\n","Compiler"	,compiler);
	printf("%-15s: %d\n","Random Number",xp_random(1000));
44

45 46 47 48 49
	printf("\ngetch() test (ESC to continue)\n");
	do {
		ch=getch();
		printf("getch() returned %d\n",ch);
	} while(ch!=ESC);
50

51 52 53
	printf("\nkbhit() test (any key to continue)\n");
	while(!kbhit()) {
		printf(".");
rswindell's avatar
rswindell committed
54 55
		fflush(stdout);
		SLEEP(500);
56 57 58
	}
	getch();	/* remove character from keyboard buffer */

59
#if 1
60 61 62 63
	/* BEEP test */
	printf("\nBEEP() test\n");
	getkey();
	for(i=750;i>250;i-=5)
rswindell's avatar
rswindell committed
64
		BEEP(i,15);
65
	for(;i<1000;i+=5)
rswindell's avatar
rswindell committed
66
		BEEP(i,15);
67

68
	/* SLEEP test */
rswindell's avatar
rswindell committed
69
	printf("\nSLEEP(5 second) test\n");
70 71
	getkey();
	t=time(NULL);
72 73
	printf("sleeping... ");
	fflush(stdout);
74
	SLEEP(5000);
rswindell's avatar
rswindell committed
75
	printf("slept %ld seconds\n",time(NULL)-t);
76 77

	/* glob test */
rswindell's avatar
rswindell committed
78
	printf("\nglob(%s) test\n",glob_pattern);
79
	getkey();
80 81 82 83 84 85 86 87
	i=glob(glob_pattern,GLOB_MARK,NULL,&g);
	if(i==0) {
		for(u=0;u<g.gl_pathc;u++)
			printf("%s\n",g.gl_pathv[u]);
		globfree(&g);
	} else
		printf("glob(%s) returned %d\n",glob_pattern,i);

88
	/* opendir (and other directory functions) test */
rswindell's avatar
rswindell committed
89
	printf("\nopendir(%s) test\n",path);
90
	getkey();
rswindell's avatar
rswindell committed
91 92
	printf("\nDirectory of %s\n\n",FULLPATH(fullpath,path,sizeof(fullpath)));
	dir=opendir(path);
93 94
	while(dir!=NULL && (dirent=readdir(dir))!=NULL) {
		t=fdate(dirent->d_name);
rswindell's avatar
rswindell committed
95
		printf("%.24s %10lu  %06o  %s%c\n"
96
			,ctime(&t)
97 98 99 100
			,flength(dirent->d_name)
			,getfattr(dirent->d_name)
			,dirent->d_name
			,isdir(dirent->d_name) ? '/':0
101 102 103 104
			);
	}
	if(dir!=NULL)
		closedir(dir);
rswindell's avatar
rswindell committed
105
	printf("\nFree disk space: %lu bytes\n",getfreediskspace(path));
106 107
#endif

108
#if 1
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	/* Thread (and inter-process communication) test */
	printf("\nThread test\n");
	getkey();
	sem_init(&thread_data.parent_sem
		,0 /* shared between processes */
		,0 /* initial count */
		);
	sem_init(&thread_data.child_sem
		,0	/* shared between processes */
		,0	/* initial count */
		);
	if(_beginthread(
		  thread_test	/* entry point */
		 ,0				/* stack size (0=auto) */
		 ,&thread_data	/* data */
124
		 )==(unsigned long)-1)
125 126 127 128 129 130 131 132 133 134 135 136
		printf("_beginthread failed\n");
	else {
		sem_wait(&thread_data.child_sem);	/* wait for thread to begin */
		for(i=0;i<10;i++) {
			printf("<parent>");
			sem_post(&thread_data.parent_sem);
			sem_wait(&thread_data.child_sem);
		}
		sem_wait(&thread_data.child_sem);	/* wait for thread to end */
	}
	sem_destroy(&thread_data.parent_sem);
	sem_destroy(&thread_data.child_sem);
137
#endif
138
	return 0;
139
}
140 141 142

static void getkey(void)
{
143
	printf("Hit any key to continue...");
144 145
	fflush(stdout);
	getch();
146
	printf("\r%30s\r","");
147 148
	fflush(stdout);
}
rswindell's avatar
rswindell committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

static void thread_test(void* arg)
{
	ulong i;
	thread_data_t* data = (thread_data_t*)arg;

	printf("thread_test entry\n");
	sem_post(&data->child_sem);

	for(i=0;i<10;i++) {
		sem_wait(&data->parent_sem);
		printf(" <child>\n");
		sem_post(&data->child_sem);
	}

	printf("thread_test exit\n");
	sem_post(&data->child_sem);
}