/
main.c
650 lines (622 loc) · 24.7 KB
/
main.c
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//
// main.c
//
// ImageInfo
//
// A command line tool to display info about image files
// Copyright (c) 2012-2016 BitBank Software, Inc.
// Written by Larry Bank
//
// Project started 4/12/2012
// Version 1.0 released 4/15/2012
// 5/18/12 - added support for PBM/PGM/PPM and Targa
// 9/5/13 - added support for OS/2 BMP, JEDMICS, CALS
// Version 1.1 released 9/5/2013
// 12/5/14 - modified for Linux/Mac
// Version 1.2 released 12/5/2014
// Version 1.3 released 4/6/2016
// 3/10/17 added support for PCX files
// Version 1.4 released 3/10/2017
//
// Copyright 2012 BitBank Software, Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//===========================================================================
#include "my_windows.h"
#include <stdio.h>
#include <string.h>
#include "pil_io.h"
#define TEMP_BUF_SIZE 4096
#define DEFAULT_READ_SIZE 256
#define MAX_TAGS 256
#define TIFF_TAGSIZE 12
#ifdef _WIN32
#define PILIO_SLASH_CHAR '\\'
#else
#define PILIO_SLASH_CHAR '/'
#endif
typedef unsigned int uint32_t;
const char *szJPEGTypes[] = {"BASELINE", "EXTENDED", "PROGRESSIVE", "LOSSLESS"};
const char *szType[] = {"Unknown", "PNG","JFIF","Win BMP","OS/2 BMP","TIFF","GIF","Portable Pixmap","Targa","JEDMICS","CALS","PCX"};
const char *szComp[] = {"Unknown", "Flate","JPEG","None","RLE","LZW","G3","G4","Packbits","Modified Huffman","Thunderscan RLE","JBIG (T.85)"};
const char *szPhotometric[] = {"WhiteIsZero","BlackIsZero","RGB","Palette Color","Transparency Mask","CMYK","YCbCr","Unknown"};
const char *szPlanar[] = {"Unknown","Chunky","Planar"};
enum
{
FILETYPE_UNKNOWN = 0,
FILETYPE_PNG,
FILETYPE_JPEG,
FILETYPE_BMP,
FILETYPE_OS2BMP,
FILETYPE_TIFF,
FILETYPE_GIF,
FILETYPE_PPM,
FILETYPE_TARGA,
FILETYPE_JEDMICS,
FILETYPE_CALS,
FILETYPE_PCX
};
enum
{
COMPTYPE_UNKNOWN = 0,
COMPTYPE_FLATE,
COMPTYPE_JPEG,
COMPTYPE_NONE,
COMPTYPE_RLE,
COMPTYPE_LZW,
COMPTYPE_G3,
COMPTYPE_G4,
COMPTYPE_PACKBITS,
COMPTYPE_HUFFMAN,
COMPTYPE_THUNDERSCAN,
COMPTYPE_JBIG
};
/****************************************************************************
* *
* FUNCTION : TIFFSHORT(char *, BOOL) *
* *
* PURPOSE : Retrieve a short value from a TIFF tag. *
* *
****************************************************************************/
unsigned short TIFFSHORT(unsigned char *p, BOOL bMotorola)
{
unsigned short s;
if (bMotorola)
s = *p * 0x100 + *(p+1);
else
s = *p + *(p+1)*0x100;
return s;
} /* TIFFSHORT() */
/****************************************************************************
* *
* FUNCTION : TIFFLONG(char *, BOOL) *
* *
* PURPOSE : Retrieve a long value from a TIFF tag. *
* *
****************************************************************************/
uint32_t TIFFLONG(unsigned char *p, BOOL bMotorola)
{
uint32_t l;
if (bMotorola)
l = *p * 0x1000000 + *(p+1) * 0x10000 + *(p+2) * 0x100 + *(p+3);
else
l = *p + *(p+1) * 0x100 + *(p+2) * 0x10000 + *(p+3) * 0x1000000;
return l;
} /* TIFFLONG() */
/****************************************************************************
* *
* FUNCTION : TIFFVALUE(char *, BOOL) *
* *
* PURPOSE : Retrieve the value from a TIFF tag. *
* *
****************************************************************************/
int TIFFVALUE(unsigned char *p, BOOL bMotorola)
{
int i, iType;
iType = TIFFSHORT(p+2, bMotorola);
/* If pointer to a list of items, must be a long */
if (TIFFSHORT(p+4, bMotorola) > 1)
iType = 4;
switch (iType)
{
case 3: /* Short */
i = TIFFSHORT(p+8, bMotorola);
break;
case 4: /* Long */
case 7: // undefined (treat it as a long since it's usually a multibyte buffer)
i = TIFFLONG(p+8, bMotorola);
break;
case 6: // signed byte
i = (signed char)p[8];
break;
case 2: /* ASCII */
case 5: /* Unsigned Rational */
case 10: /* Signed Rational */
i = TIFFLONG(p+8, bMotorola);
break;
default: /* to suppress compiler warning */
i = 0;
break;
}
return i;
} /* TIFFVALUE() */
/****************************************************************************
* *
* FUNCTION : ParseNumber(char *, int *) *
* *
* PURPOSE : Read the ascii string and convert to a number. *
* *
****************************************************************************/
int ParseNumber(unsigned char *buf, int *iOff, int iLength)
{
int i, iOffset;
i = 0;
iOffset = *iOff;
while (iOffset < iLength && buf[iOffset] >= '0' && buf[iOffset] <= '9')
{
i *= 10;
i += (int)(buf[iOffset++] - '0');
}
*iOff = iOffset+1; /* Skip ending char */
return i;
} /* ParseNumber() */
/****************************************************************************
* *
* FUNCTION : ProcessFile(char *, int) *
* *
* PURPOSE : Gather and display information about a specific file. *
* *
****************************************************************************/
void ProcessFile(char *szFileName, int iFileSize)
{
int i, j, k;
void * iHandle;
int iBytes;
int iFileType = FILETYPE_UNKNOWN;
int iCompression = COMPTYPE_UNKNOWN;
unsigned char cBuf[TEMP_BUF_SIZE]; // small buffer to load header info
int iBpp = 0;
int iWidth = 0;
int iHeight = 0;
int iOffset;
int iMarker;
int iPhotoMetric;
int iPlanar;
int iCount;
unsigned char ucSubSample;
BOOL bMotorola;
char szOptions[256];
// Detect the file type by its header
iHandle = PILIOOpenRO(szFileName);
if (iHandle == (void *)-1)
{
return;
}
iBytes = PILIORead(iHandle, cBuf, DEFAULT_READ_SIZE);
if (iBytes != DEFAULT_READ_SIZE)
return; // too small
if (MOTOLONG(cBuf) == 0x89504e47) // PNG
iFileType = FILETYPE_PNG;
else if (cBuf[0] == 'B' && cBuf[1] == 'M') // BMP
{
if (cBuf[14] == 0x28) // Windows
iFileType = FILETYPE_BMP;
else
iFileType = FILETYPE_OS2BMP;
}
else if (cBuf[0] == 0x0a && cBuf[1] < 0x6 && cBuf[2] == 0x01)
{
iFileType = FILETYPE_PCX;
}
else if (INTELLONG(cBuf) == 0x80 && (cBuf[36] == 4 || cBuf[36] == 6))
{
iFileType = FILETYPE_JEDMICS;
}
else if (INTELLONG(cBuf) == 0x64637273)
{
iFileType = FILETYPE_CALS;
}
else if ((MOTOLONG(cBuf) & 0xffffff00) == 0xffd8ff00) // JPEG
iFileType = FILETYPE_JPEG;
else if (MOTOLONG(cBuf) == 0x47494638 /*'GIF8'*/) // GIF
iFileType = FILETYPE_GIF;
else if ((cBuf[0] == 'I' && cBuf[1] == 'I') || (cBuf[0] == 'M' && cBuf[1] == 'M'))
iFileType = FILETYPE_TIFF;
else
{
i = MOTOLONG(cBuf) & 0xffff8080;
if (i == 0x50360000 || i == 0x50350000 || i == 0x50340000) // Portable bitmap/graymap/pixmap
iFileType = FILETYPE_PPM;
}
// Check for Truvision Targa
i = cBuf[1] & 0xfe;
j = cBuf[2];
// make sure it is not a MPEG file (starts with 00 00 01 BA)
if (MOTOLONG(cBuf) != 0x1ba && MOTOLONG(cBuf) != 0x1b3 && i == 0 && (j == 1 || j == 2 || j == 3 || j == 9 || j == 10 || j == 11))
iFileType = FILETYPE_TARGA;
if (iFileType == FILETYPE_UNKNOWN)
{
printf("%s - unknown file type\n", szFileName);
goto process_exit;
}
szOptions[0] = '\0'; // info specific to each file type
// Get info specific to each type of file
switch (iFileType)
{
case FILETYPE_PCX:
iWidth = 1 + INTELSHORT(&cBuf[8]) - INTELSHORT(&cBuf[4]);
iHeight = 1 + INTELSHORT(&cBuf[10]) - INTELSHORT(&cBuf[6]);
iCompression = COMPTYPE_PACKBITS;
iBpp = cBuf[3] * cBuf[65];
break;
case FILETYPE_PNG:
if (MOTOLONG(&cBuf[12]) == 0x49484452/*'IHDR'*/)
{
iWidth = MOTOLONG(&cBuf[16]);
iHeight = MOTOLONG(&cBuf[20]);
iCompression = COMPTYPE_FLATE;
i = cBuf[24]; // bits per pixel
j = cBuf[25]; // pixel type
switch (j)
{
case 0: // grayscale
case 3: // palette image
iBpp = i;
break;
case 2: // RGB triple
iBpp = i * 3;
break;
case 4: // grayscale + alpha channel
iBpp = i * 2;
break;
case 6: // RGB + alpha
iBpp = i * 4;
break;
}
if (cBuf[28] == 1) // interlace flag
strcpy(szOptions, ", Interlaced");
else
strcpy(szOptions, ", Not interlaced");
}
break;
case FILETYPE_TARGA:
iWidth = INTELSHORT(&cBuf[12]);
iHeight = INTELSHORT(&cBuf[14]);
iBpp = cBuf[16];
if (cBuf[2] == 3 || cBuf[2] == 11) // monochrome
iBpp = 1;
if (cBuf[2] < 9)
iCompression = COMPTYPE_NONE;
else
iCompression = COMPTYPE_RLE;
break;
case FILETYPE_PPM:
if (cBuf[1] == '4')
iBpp = 1;
else if (cBuf[1] == '5')
iBpp = 8;
else if (cBuf[1] == '6')
iBpp = 24;
j = 2;
while ((cBuf[j] == 0xa || cBuf[j] == 0xd) && j<DEFAULT_READ_SIZE)
j++; // skip newline/cr
while (cBuf[j] == '#' && j < DEFAULT_READ_SIZE) // skip over comments
{
while (cBuf[j] != 0xa && cBuf[j] != 0xd && j < DEFAULT_READ_SIZE)
j++;
while ((cBuf[j] == 0xa || cBuf[j] == 0xd) && j<DEFAULT_READ_SIZE)
j++; // skip newline/cr
}
// get width and height
iWidth = ParseNumber(cBuf, &j, DEFAULT_READ_SIZE);
iHeight = ParseNumber(cBuf, &j, DEFAULT_READ_SIZE);
iCompression = COMPTYPE_NONE;
break;
case FILETYPE_BMP:
iCompression = COMPTYPE_NONE;
iWidth = INTELSHORT(&cBuf[18]);
iHeight = INTELSHORT(&cBuf[22]);
if (iHeight & 0x8000) // upside down
iHeight = 65536 - iHeight;
iBpp = cBuf[28]; /* Number of bits per plane */
iBpp *= cBuf[26]; /* Number of planes */
if (cBuf[30] && (iBpp == 4 || iBpp == 8)) // if biCompression is non-zero (2=4bit rle, 1=8bit rle,4=24bit rle)
iCompression = COMPTYPE_RLE; // windows run-length
break;
case FILETYPE_OS2BMP:
iCompression = COMPTYPE_NONE;
if (cBuf[14] == 12) // version 1.2
{
iWidth = INTELSHORT(&cBuf[18]);
iHeight = INTELSHORT(&cBuf[20]);
iBpp = cBuf[22]; /* Number of bits per plane */
iBpp *= cBuf[24]; /* Number of planes */
}
else
{
iWidth = INTELSHORT(&cBuf[18]);
iHeight = INTELSHORT(&cBuf[22]);
iBpp = cBuf[28]; /* Number of bits per plane */
iBpp *= cBuf[26]; /* Number of planes */
}
if (iHeight & 0x8000) // upside down
iHeight = 65536 - iHeight;
if (cBuf[30] == 1 || cBuf[30] == 2 || cBuf[30] == 4) // if biCompression is non-zero (2=4bit rle, 1=8bit rle,4=24bit rle)
iCompression = COMPTYPE_RLE; // windows run-length
break;
case FILETYPE_JEDMICS:
iBpp = 1;
iWidth = INTELSHORT(&cBuf[6]);
iWidth <<= 3; // convert byte width to pixel width
iHeight = INTELSHORT(&cBuf[4]);
iCompression = COMPTYPE_G4;
break;
case FILETYPE_CALS:
iBpp = 1;
iCompression = COMPTYPE_G4;
PILIOSeek(iHandle, 750, 0); // read some more
iBytes = PILIORead(iHandle, cBuf, 1);
if (cBuf[0] == '1') // type 1 file
{
PILIOSeek(iHandle, 1033, 0); // read some more
iBytes = PILIORead(iHandle, cBuf, 256);
i = 0;
iWidth = ParseNumber(cBuf, &i, 256);
iHeight = ParseNumber(cBuf, &i, 256);
}
else // type 2
{
PILIOSeek(iHandle, 1024, 0); // read some more
iBytes = PILIORead(iHandle, cBuf, 128);
if (MOTOLONG(cBuf) == 0x7270656c && MOTOLONG(&cBuf[4]) == 0x636e743a) // "rpelcnt:"
{
i = 9;
iWidth = ParseNumber(cBuf, &i, 256);
iHeight = ParseNumber(cBuf, &i, 256);
}
}
break;
case FILETYPE_JPEG:
iCompression = COMPTYPE_JPEG;
i = j = 2; /* Start at offset of first marker */
iMarker = 0; /* Search for SOF (start of frame) marker */
while (i < 32 && iMarker != 0xffc0 && j < iFileSize)
{
iMarker = MOTOSHORT(&cBuf[i]) & 0xfffc;
if (iMarker < 0xff00) // invalid marker, could be generated by "Arles Image Web Page Creator" or Accusoft
{
i += 2;
continue; // skip 2 bytes and try to resync
}
if (iMarker == 0xffe0 && cBuf[i+4] == 'E' && cBuf[i+5] == 'x') // EXIF, check for thumbnail
{
unsigned char cTemp[1024];
// int iOff;
memcpy(cTemp, cBuf, 32);
iBytes = PILIORead(iHandle, &cTemp[32], 1024-32);
bMotorola = (cTemp[i+10] == 'M');
// Future - do something with the thumbnail
// iOff = PILTIFFLONG(&cTemp[i+14], bMotorola); // get offset to first IFD (info)
// PILTIFFMiniInfo(pFile, bMotorola, j + 10 + iOff, TRUE);
}
if (iMarker == 0xffc0) // the one we're looking for
break;
j += 2 + MOTOSHORT(&cBuf[i+2]); /* Skip to next marker */
if (j < iFileSize) // need to read more
{
PILIOSeek(iHandle, j, 0); // read some more
iBytes = PILIORead(iHandle, cBuf, 32);
i = 0;
}
} // while
if (iMarker != 0xffc0)
goto process_exit; // error - invalid file?
else
{
iBpp = cBuf[i+4]; // bits per sample
iHeight = MOTOSHORT(&cBuf[i+5]);
iWidth = MOTOSHORT(&cBuf[i+7]);
iBpp = iBpp * cBuf[i+9]; /* Bpp = number of components * bits per sample */
ucSubSample = cBuf[i+11];
iMarker = MOTOSHORT(&cBuf[i]);
sprintf(szOptions, ", type = %s, color subsampling = %d:%d", szJPEGTypes[iMarker & 3], (ucSubSample>>4),(ucSubSample & 0xf));
}
break;
case FILETYPE_GIF:
iCompression = COMPTYPE_LZW;
iWidth = INTELSHORT(&cBuf[6]);
iHeight = INTELSHORT(&cBuf[8]);
iBpp = (cBuf[10] & 7) + 1;
if (cBuf[10] & 64) // interlace flag
strcpy(szOptions, ", Interlaced");
else
strcpy(szOptions, ", Not interlaced");
break;
case FILETYPE_TIFF:
bMotorola = (cBuf[0] == 'M'); // determine endianness of TIFF data
i = TIFFLONG(&cBuf[4], bMotorola); // get first IFD offset
PILIOSeek(iHandle, i, 0); // read the entire tag directory
iBytes = PILIORead(iHandle, cBuf, MAX_TAGS*TIFF_TAGSIZE);
j = TIFFSHORT(cBuf, bMotorola); // get the tag count
iOffset = 2; // point to start of TIFF tag directory
// Some TIFF files don't specify everything, so set up some default values
iBpp = 1;
iPlanar = 1;
iCompression = COMPTYPE_NONE;
iPhotoMetric = 7; // if not specified, set to "unknown"
// Each TIFF tag is made up of 12 bytes
// byte 0-1: Tag value (short)
// byte 2-3: data type (short)
// byte 4-7: number of values (long)
// byte 8-11: value or offset to list of values
for (i=0; i<j; i++) // search tags for the info we care about
{
iMarker = TIFFSHORT(&cBuf[iOffset], bMotorola); // get the TIFF tag
switch (iMarker) // only read the tags we care about...
{
case 256: // image width
iWidth = TIFFVALUE(&cBuf[iOffset], bMotorola);
break;
case 257: // image length
iHeight = TIFFVALUE(&cBuf[iOffset], bMotorola);
break;
case 258: // bits per sample
iCount = TIFFLONG(&cBuf[iOffset+4], bMotorola); /* Get the count */
if (iCount == 1)
iBpp = TIFFVALUE(&cBuf[iOffset], bMotorola);
else // need to read the first value from the list (they should all be equal)
{
k = TIFFLONG(&cBuf[iOffset+8], bMotorola);
if (k < iFileSize)
{
PILIOSeek(iHandle, k, 0);
iBytes = PILIORead(iHandle, &cBuf[iOffset], 2); // okay to overwrite the value we just used
iBpp = iCount * TIFFSHORT(&cBuf[iOffset], bMotorola);
}
}
break;
case 259: // compression
k = TIFFVALUE(&cBuf[iOffset], bMotorola);
if (k == 1)
iCompression = COMPTYPE_NONE;
else if (k == 2)
iCompression = COMPTYPE_HUFFMAN;
else if (k == 3)
iCompression = COMPTYPE_G3;
else if (k == 4)
iCompression = COMPTYPE_G4;
else if (k == 5)
iCompression = COMPTYPE_LZW;
else if (k == 6 || k == 7)
iCompression = COMPTYPE_JPEG;
else if (k == 8 || k == 32946)
iCompression = COMPTYPE_FLATE;
else if (k == 9)
iCompression = COMPTYPE_JBIG;
else if (k == 32773)
iCompression = COMPTYPE_PACKBITS;
else if (k == 32809)
iCompression = COMPTYPE_THUNDERSCAN;
else
iCompression = COMPTYPE_UNKNOWN;
break;
case 262: // photometric value
iPhotoMetric = TIFFVALUE(&cBuf[iOffset], bMotorola);
if (iPhotoMetric > 6)
iPhotoMetric = 7; // unknown
break;
case 284: // planar/chunky
iPlanar = TIFFVALUE(&cBuf[iOffset], bMotorola);
if (iPlanar < 1 || iPlanar > 2) // unknown value
iPlanar = 0; // unknown
break;
} // switch on tiff tag
iOffset += TIFF_TAGSIZE;
} // for each tag
sprintf(szOptions, ", Photometric = %s, Planar config = %s", szPhotometric[iPhotoMetric], szPlanar[iPlanar]);
break;
} // switch
printf("%s: Type=%s, Compression=%s, Size: %d x %d, %d-Bpp%s\n", szFileName, szType[iFileType], szComp[iCompression], iWidth, iHeight, iBpp, szOptions);
process_exit:
PILIOClose(iHandle);
} /* ProcessFile() */
/****************************************************************************
* *
* FUNCTION : main(int, char**) *
* *
****************************************************************************/
int main( int argc, char *argv[ ])
{
#ifdef _WIN32_bad
PILIOFINDFILE ff;
void * iHandle;
BOOL bMoreFiles;
int iFileCount = 0;
#endif
char szDir[256], szFile[256];
int i, iLen;
if (argc != 2)
{
printf("Image Info 1.4 Copyright (c) 2012-2017 BitBank Software, Inc.\n");
printf("Usage: IMAGEINFO <pathname>\n");
printf("Supports: TIFF,GIF,JPEG,BMP,PNG,PBM,PGM,PPM,TGA,JEDMICS,CALS,PCX\n");
return 0;
}
// Find the source dir since FindFirstFile only returns leaf names
iLen = (int)strlen(argv[1]);
for (i=iLen-1; i>0; i--)
{
if (argv[1][i] == PILIO_SLASH_CHAR) // Search backwards for first slash
break;
}
if (i==0) // Leaf name provided, so current directory must have been referenced
{
PILIOGetCurDir(256, szDir);
strcpy(szFile, szDir);
iLen = strlen(szFile);
if (szFile[iLen-1] != PILIO_SLASH_CHAR) // add missing slash
{
szFile[iLen] = PILIO_SLASH_CHAR;
szFile[iLen+1] = '\0';
}
strcat(szFile, argv[1]); // create a complete pathname
}
else // Extract the directory from the pathname passed
{
strcpy(szFile, argv[1]);
szDir[i+1] = '\0';
for (;i>=0; i--)
{
szDir[i] = argv[1][i];
}
}
if (strcspn(szFile, "*?") == strlen(szFile)) // no wildcard characters, use the pathname as-is
{
void * iHandle;
int iSize;
iHandle = PILIOOpenRO(szFile);
if (iHandle != (void *)-1)
{
iSize = (int)PILIOSize(iHandle);
PILIOClose(iHandle);
ProcessFile(szFile, iSize);
return 0;
}
else
{
printf("%s - file not found\n", argv[1]);
return -1; // none found, leave
}
}
#ifdef _WIN32_bad
iHandle = PILIOFindFirst(szFile, &ff);
if (iHandle == -1)
{
printf("%s - file not found\n", argv[1]);
return -1; // none found, leave
}
bMoreFiles = TRUE;
// Search for matching filenames which aren't directories
while (bMoreFiles)
{
if ((ff.ucFlags & PIL_ATTR_DIR) == 0)
{
iFileCount++;
strcpy(szFile, szDir);
strcat(szFile, ff.szLeafName);
ProcessFile(szFile, ff.ulFileSize);
}
bMoreFiles = PILIOFindNext(iHandle, &ff);
} // while more files to read
PILIOFindClose(iHandle);
myprintf("%d file(s) found\n", iFileCount);
#endif
return 0;
} /* main() */