File: amidos.c

package info (click to toggle)
glhack 1.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 24,604 kB
  • ctags: 18,992
  • sloc: ansic: 208,570; cpp: 13,139; yacc: 2,005; makefile: 1,161; lex: 377; sh: 321; awk: 89; sed: 11
file content (490 lines) | stat: -rw-r--r-- 10,738 bytes parent folder | download | duplicates (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*    SCCS Id: @(#)amidos.c     3.2    2000/01/12
/* Copyright (c) Olaf Seibert, Nijmegen, The Netherlands, 1988,1990.    */
/* Copyright (c) Kenneth Lorber, Bethesda, Maryland, 1991,1992,1993,1996.  */
/* NetHack may be freely redistributed.  See license for details.	*/

/*
 * An assortment of imitations of cheap plastic MSDOS and Unix functions.
 */

#include "hack.h"
#include "winami.h"

/* Defined in config.h, let's undefine it here (static function below) */
#undef strcmpi

#include <libraries/dos.h>
#include <exec/execbase.h>
#include <intuition/intuition.h>

#undef COUNT
#if defined(__SASC_60) || defined(__GNUC__)
# include <proto/exec.h>
# include <proto/dos.h>
#endif

#ifdef AZTEC_50
# include <functions.h>
# undef strcmpi
#endif

/* Prototypes */
#include "NH:sys/amiga/winami.p"
#include "NH:sys/amiga/amiwind.p"
#include "NH:sys/amiga/amidos.p"

extern char Initialized;
extern struct window_procs amii_procs;

#ifndef __SASC_60
int Enable_Abort = 0;   /* for stdio package */
#endif

/* Initial path, so we can find NetHack.cnf */
char PATH[PATHLEN] = "NetHack:";

static boolean record_exists(void);

void
flushout()
{
    (void) fflush(stdout);
}

#ifndef getuid
getuid()
{
    return 1;
}
#endif

#ifndef getlogin
char *
getlogin()
{
    return ((char *) NULL);
}
#endif

#ifndef AZTEC_50
int
abs(x)
int x;
{
    return x < 0? -x: x;
}
#endif

#ifdef SHELL
int
dosh()
{
    int i;
    char buf[ BUFSZ ];
    extern struct ExecBase *SysBase;

    /* Only under 2.0 and later ROMs do we have System() */
    if( SysBase->LibNode.lib_Version >= 37 && !amibbs)
    {
	getlin("Enter CLI Command...", buf );
	if (buf[0] != '\033')
	    i = System( buf, NULL );
    }
    else
    {
	i = 0;
	pline("No mysterious force prevented you from using multitasking.");
    }
    return i;
}
#endif /* SHELL */

#ifdef MFLOPPY
# include <ctype.h>

# define Sprintf (void) sprintf

#define EXTENSION   72

/*
 *  This routine uses an approximation of the free bytes on a disk.
 *  How large a file you can actually write depends on the number of
 *  extension blocks you need for it.
 *  In each extenstion block there are maximum 72 pointers to blocks,
 *  so every 73 disk blocks have only 72 available for data.
 *  The (necessary) file header is also good for 72 data block pointers.
 */
/* TODO: update this for FFS */
long
freediskspace(path)
char *path;
{
    register long freeBytes = 0;
    register struct InfoData *infoData; /* Remember... longword aligned */
    char fileName[32];

    /*
     *  Find a valid path on the device of which we want the free space.
     *  If there is a colon in the name, it is an absolute path
     *  and all up to the colon is everything we need.
     *  Remember slashes in a volume name are allowed!
     *  If there is no colon, it is relative to the current directory,
     *  so must be on the current device, so "" is enough...
     */
    {
    register char *colon;

    strncpy(fileName, path, sizeof(fileName) - 1);
    fileName[31] = 0;
    if (colon = index(fileName, ':'))
	colon[1] = '\0';
    else
	fileName[0] = '\0';
    }
    {
    BPTR fileLock;
    infoData = (struct InfoData *) alloc(sizeof(struct InfoData));
    if (fileLock = Lock(fileName, SHARED_LOCK)) {
	if (Info(fileLock, infoData)) {
		/* We got a kind of DOS volume, since we can Lock it. */
		/* Calculate number of blocks available for new file */
		/* Kludge for the ever-full VOID: (oops RAM:) device */
	    if (infoData->id_UnitNumber == -1 &&
	      infoData->id_NumBlocks == infoData->id_NumBlocksUsed) {
		freeBytes = AvailMem(0L) - 64 * 1024L;
		    /* Just a stupid guess at the */
		    /* Ram-Handler overhead per block: */
		freeBytes -= freeBytes/16;
	    } else {
		/* Normal kind of DOS file system device/volume */
		freeBytes = infoData->id_NumBlocks - infoData->id_NumBlocksUsed;
		freeBytes -= (freeBytes + EXTENSION) / (EXTENSION + 1);
		freeBytes *= infoData->id_BytesPerBlock;
	    }
	    if (freeBytes < 0)
		freeBytes = 0;
	}
	UnLock(fileLock);
    }
    free(infoData);
    return freeBytes;
    }
}


long
filesize(file)
char *file;
{
    register BPTR fileLock;
    register struct FileInfoBlock *fileInfoBlock;
    register long size = 0;

    fileInfoBlock = (struct FileInfoBlock *)alloc(sizeof(struct FileInfoBlock));
    if (fileLock = Lock(file, SHARED_LOCK)) {
	if (Examine(fileLock, fileInfoBlock)) {
	    size = fileInfoBlock->fib_Size;
	}
	UnLock(fileLock);
    }
    free(fileInfoBlock);
    return size;
}

#if 0
void
eraseall(path, files)
const char *path, *files;
{
    BPTR dirLock, dirLock2;
    struct FileInfoBlock *fibp;
    int chklen;
#ifdef BETA
    if(files != alllevels)panic("eraseall");
#endif
    chklen=(int)index(files,'*')-(int)files;

    if (dirLock = Lock( (char *)path ,SHARED_LOCK)) {
	dirLock2=DupLock(dirLock);
	dirLock2= CurrentDir(dirLock2);
	fibp=AllocMem(sizeof(struct FileInfoBlock),0);
	if(fibp){
	    if(Examine(dirLock,fibp)){
		while(ExNext(dirLock,fibp)){
		    if(!strncmp(fibp->fib_FileName,files,chklen)){
			DeleteFile(fibp->fib_FileName);
		    }
		}
	    }
	    FreeMem(fibp,sizeof(struct FileInfoBlock));
	}
	UnLock(dirLock);
	UnLock(CurrentDir(dirLock2));
    }
}
#endif

/* This size makes that most files can be copied with two Read()/Write()s */

#if 0 /* Unused */
#define COPYSIZE    4096

char *CopyFile(from, to)
const char *from, *to;
{
    register BPTR fromFile, toFile;
    register char *buffer;
    register long size;
    char *error = NULL;

    buffer = (char *) alloc(COPYSIZE);
    if (fromFile = Open( (char *)from, MODE_OLDFILE)) {
	if (toFile = Open( (char *)to, MODE_NEWFILE)) {
	    while (size = Read(fromFile, buffer, (long)COPYSIZE)) {
		if (size == -1){
		    error = "Read error";
		    break;
		}
		if (size != Write(toFile, buffer, size)) {
		    error = "Write error";
		    break;
		}
	    }
	    Close(toFile);
	} else
	    error = "Cannot open destination";
	Close(fromFile);
    } else
	error = "Cannot open source (this should not occur)";
    free(buffer);
    return error;
}
#endif

/* this should be replaced */
saveDiskPrompt(start)
{
    char buf[BUFSIZ], *bp;
    BPTR fileLock;

    if (flags.asksavedisk) {
	    /* Don't prompt if you can find the save file */
	if (fileLock = Lock(SAVEF, SHARED_LOCK)) {
	    UnLock(fileLock);
#if defined(TTY_GRAPHICS)
	    if(windowprocs.win_init_nhwindows!=amii_procs.win_init_nhwindows)
		clear_nhwindow( WIN_MAP );
#endif
#if defined(AMII_GRAPHICS)
	    if(windowprocs.win_init_nhwindows==amii_procs.win_init_nhwindows)
		clear_nhwindow( WIN_BASE );
#endif
	    return 1;
	}
	pline( "If save file is on a SAVE disk, put that disk in now." );
	if( strlen( SAVEF ) > QBUFSZ - 25 - 22 )
	    panic( "not enough buffer space for prompt" );
/* THIS IS A HACK */
#if defined(TTY_GRAPHICS)
	if(windowprocs.win_init_nhwindows!=amii_procs.win_init_nhwindows){
	    getlin("File name ?",buf);
	    clear_nhwindow( WIN_MAP );
	}
#endif
#if defined(AMII_GRAPHICS)
	if(windowprocs.win_init_nhwindows==amii_procs.win_init_nhwindows){
	    getlind("File name ?", buf, SAVEF);
	    clear_nhwindow( WIN_BASE );
	}
#endif
	clear_nhwindow( WIN_MESSAGE);
	if (!start && *buf == '\033')
	    return 0;

    /* Strip any whitespace. Also, if nothing was entered except
     * whitespace, do not change the value of SAVEF.
     */
	for (bp = buf; *bp; bp++) {
	    if (!isspace(*bp)) {
	    strncpy(SAVEF, bp, PATHLEN);
	    break;
	    }
	}
    }
    return 1;
}

/* Return 1 if the record file was found */
static boolean
record_exists()
{
    FILE *file;

    if (file = fopenp(RECORD, "r")) {
	fclose(file);
	return TRUE;
    }
    return FALSE;
}

#ifdef MFLOPPY
/*
 * Under MSDOS: Prompt for game disk, then check for record file.
 * For Amiga: do nothing, but called from restore.c
 */
void
gameDiskPrompt(){}
#endif

/*
 * Add a slash to any name not ending in / or :.  There must
 * be room for the /.
 */
void
append_slash(name)
char *name;
{
    char *ptr;

    if (!*name)return;

    ptr = eos(name) - 1;
    if (*ptr != '/' && *ptr != ':') {
	*++ptr = '/';
	*++ptr = '\0';
    }
}


void
getreturn(str)
const char *str;
{
    int ch;

    raw_printf("Hit <RETURN> %s.", str);
    while ((ch = nhgetch()) != '\n' && ch != '\r' )
	continue;
}

/* Follow the PATH, trying to fopen the file.
 */
#define PATHSEP    ';'

FILE *
fopenp(name, mode)
register const char *name, *mode;
{
    register char *bp, *pp, lastch;
    register FILE *fp;
    register BPTR theLock;
    char buf[BUFSIZ];

    /* Try the default directory first.  Then look along PATH.
     */
    if (strlen(name) >= BUFSIZ) return( NULL );
    strcpy(buf, name);
    if (theLock = Lock(buf, SHARED_LOCK)) {
	UnLock(theLock);
	if (fp = fopen(buf, mode))
	    return fp;
    }
    pp = PATH;
    while (pp && *pp) {
	bp = buf;
	while (*pp && *pp != PATHSEP){
	    if( bp > buf + BUFSIZ - 1 ) return( NULL );
	    lastch = *bp++ = *pp++;
	}
	if (lastch != ':' && lastch != '/' && bp != buf)
	    *bp++ = '/';
	if (bp + strlen(name) > buf + BUFSIZ - 1) return( NULL );
	strcpy(bp, name);
	if (theLock = Lock(buf, SHARED_LOCK)) {
	    UnLock(theLock);
	    if (fp = fopen(buf, mode)) return fp;
	}
	if (*pp)
	    pp++;
    }
    return NULL;
}
#endif /* MFLOPPY */

#ifdef CHDIR

/*
 *  A not general-purpose directory changing routine.
 *  Assumes you want to return to the original directory eventually,
 *  by chdir()ing to orgdir (which is defined in pcmain.c).
 *  Assumes -1 is not a valid lock, since 0 is valid.
 */

#define NO_LOCK     ((BPTR) -1)

static BPTR OrgDirLock = NO_LOCK;

chdir(dir)
char *dir;
{
    extern char orgdir[];

    if (dir == orgdir) {
	    /* We want to go back to where we came from. */
	if (OrgDirLock != NO_LOCK) {
	    UnLock(CurrentDir(OrgDirLock));
	    OrgDirLock = NO_LOCK;
	}
    } else {
	    /*
	     * Go to some new place. If still at the original
	     * directory, save the FileLock.
	     */
	BPTR newDir;

	if (newDir = Lock( (char *)dir, SHARED_LOCK)) {
	    if (OrgDirLock == NO_LOCK) {
		OrgDirLock = CurrentDir(newDir);
	    } else {
		UnLock(CurrentDir(newDir));
	    }
	} else {
	    return -1;  /* Failed */
	}
    }
    /* CurrentDir always succeeds if you have a lock */
    return 0;
}

#endif /* CHDIR */

/* Chdir back to original directory
 */
#undef exit
void
nethack_exit(code)
{
#ifdef CHDIR
    extern char orgdir[];
#endif

#ifdef CHDIR
    chdir(orgdir);      /* chdir, not chdirx */
#endif

#ifdef AMII_GRAPHICS
    if(windowprocs.win_init_nhwindows==amii_procs.win_init_nhwindows)
	CleanUp();
#endif
    exit(code);
}

void
regularize(s)    /* normalize file name - we don't like :'s or /'s */
register char *s;
{
    register char *lp;

    while((lp = index(s, ':')) || (lp = index(s, '/')))
	*lp = '_';
}