/*
- * "$Id$"
+ * File test program for CUPS.
*
- * File test program for CUPS.
+ * Copyright © 2007-2018 by Apple Inc.
+ * Copyright © 1997-2007 by Easy Software Products.
*
- * Copyright 2007-2011 by Apple Inc.
- * Copyright 1997-2007 by Easy Software Products.
- *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law. Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file. If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- * main() - Main entry.
- * count_lines() - Count the number of lines in a file.
- * random_tests() - Do random access tests.
- * read_write_tests() - Perform read/write tests.
+ * Licensed under Apache License v2.0. See the file "LICENSE" for more
+ * information.
*/
/*
#include "file.h"
#include <stdlib.h>
#include <time.h>
-#ifdef HAVE_LIBZ
-# include <zlib.h>
-#endif /* HAVE_LIBZ */
-#ifdef WIN32
+#ifdef _WIN32
# include <io.h>
#else
# include <unistd.h>
-#endif /* WIN32 */
+#endif /* _WIN32 */
#include <fcntl.h>
int status; /* Exit status */
char filename[1024]; /* Filename buffer */
cups_file_t *fp; /* File pointer */
-#ifndef WIN32
+#ifndef _WIN32
int fds[2]; /* Open file descriptors */
cups_file_t *fdfile; /* File opened with cupsFileOpenFd() */
-#endif /* !WIN32 */
+#endif /* !_WIN32 */
int count; /* Number of lines in file */
status += random_tests();
-#ifndef WIN32
+#ifndef _WIN32
/*
* Test fdopen and close without reading...
*/
puts("PASS");
}
-#endif /* !WIN32 */
+#endif /* !_WIN32 */
/*
- * Count lines in psglyphs, rewind, then count again.
+ * Count lines in test file, rewind, then count again.
*/
- fputs("\ncupsFileOpen(\"../data/media.defs\", \"r\"): ", stdout);
+ fputs("\ncupsFileOpen(\"testfile.txt\", \"r\"): ", stdout);
- if ((fp = cupsFileOpen("../data/media.defs", "r")) == NULL)
+ if ((fp = cupsFileOpen("testfile.txt", "r")) == NULL)
{
puts("FAIL");
status ++;
puts("PASS");
fputs("cupsFileGets: ", stdout);
- if ((count = count_lines(fp)) != 208)
+ if ((count = count_lines(fp)) != 477)
{
- printf("FAIL (got %d lines, expected 208)\n", count);
+ printf("FAIL (got %d lines, expected 477)\n", count);
status ++;
}
else
puts("PASS");
fputs("cupsFileGets: ", stdout);
- if ((count = count_lines(fp)) != 208)
+ if ((count = count_lines(fp)) != 477)
{
- printf("FAIL (got %d lines, expected 208)\n", count);
+ printf("FAIL (got %d lines, expected 477)\n", count);
status ++;
}
else
*/
fputs("\ncupsFileFind: ", stdout);
-#ifdef WIN32
+#ifdef _WIN32
if (cupsFileFind("notepad.exe", "C:/WINDOWS", 1, filename, sizeof(filename)) &&
cupsFileFind("notepad.exe", "C:/WINDOWS;C:/WINDOWS/SYSTEM32", 1, filename, sizeof(filename)))
#else
if (cupsFileFind("cat", "/bin", 1, filename, sizeof(filename)) &&
cupsFileFind("cat", "/bin:/usr/bin", 1, filename, sizeof(filename)))
-#endif /* WIN32 */
+#endif /* _WIN32 */
printf("PASS (%s)\n", filename);
else
{
* Cat the filename on the command-line...
*/
- char line[1024]; /* Line from file */
+ char line[8192]; /* Line from file */
if ((fp = cupsFileOpen(argv[1], "r")) == NULL)
{
perror(argv[1]);
status = 1;
}
- else
+ else if (argc == 2)
{
status = 0;
cupsFileClose(fp);
}
+ else
+ {
+ status = 0;
+ ssize_t bytes;
+
+ while ((bytes = cupsFileRead(fp, line, sizeof(line))) > 0)
+ printf("%s: %d bytes\n", argv[1], (int)bytes);
+
+ if (cupsFileEOF(fp))
+ printf("%s: EOF\n", argv[1]);
+ else
+ perror(argv[1]);
+
+ cupsFileClose(fp);
+ }
}
return (status);
count, /* Number of records read */
record, /* Current record */
num_records; /* Number of records */
- ssize_t pos, /* Position in file */
- expected; /* Expected position in file */
+ off_t pos; /* Position in file */
+ ssize_t expected; /* Expected position in file */
cups_file_t *fp; /* File */
char buffer[512]; /* Data buffer */
* cupsFileTell()
*/
- expected = 256 * sizeof(buffer) * pass;
+ expected = 256 * (ssize_t)sizeof(buffer) * pass;
fputs("cupsFileTell(): ", stdout);
- if ((pos = cupsFileTell(fp)) != expected)
+ if ((pos = cupsFileTell(fp)) != (off_t)expected)
{
printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
CUPS_LLCAST pos, CUPS_LLCAST expected);
for (record = 0; record < 256; record ++)
{
memset(buffer, record, sizeof(buffer));
- if (cupsFileWrite(fp, buffer, sizeof(buffer)) < sizeof(buffer))
+ if (cupsFileWrite(fp, buffer, sizeof(buffer)) < (ssize_t)sizeof(buffer))
break;
}
* cupsFileTell()
*/
- expected += 256 * sizeof(buffer);
+ expected += 256 * (ssize_t)sizeof(buffer);
fputs("cupsFileTell(): ", stdout);
- if ((pos = cupsFileTell(fp)) != expected)
+ if ((pos = cupsFileTell(fp)) != (off_t)expected)
{
printf("FAIL (" CUPS_LLFMT " instead of " CUPS_LLFMT ")\n",
CUPS_LLCAST pos, CUPS_LLCAST expected);
fputs("cupsFileSeek(), cupsFileRead(): ", stdout);
- for (num_records = (pass + 1) * 256, count = (pass + 1) * 256,
- record = CUPS_RAND() % num_records;
+ for (num_records = (pass + 1) * 256, count = (pass + 1) * 256, record = ((int)CUPS_RAND() & 65535) % num_records;
count > 0;
- count --, record = (record + (CUPS_RAND() & 31) - 16 + num_records) %
- num_records)
+ count --, record = (record + ((int)CUPS_RAND() & 31) - 16 + num_records) % num_records)
{
/*
* The last record is always the first...
* contents...
*/
- expected = sizeof(buffer) * record;
+ expected = (ssize_t)sizeof(buffer) * record;
if ((pos = cupsFileSeek(fp, expected)) != expected)
{
unsigned char readbuf[8192], /* Read buffer */
writebuf[8192]; /* Write buffer */
int byte; /* Byte from file */
+ ssize_t bytes; /* Number of bytes read/written */
off_t length; /* Length of file */
static const char *partial_line = "partial line";
/* Partial line */
CUPS_SRAND((unsigned)time(NULL));
for (i = 0; i < (int)sizeof(writebuf); i ++)
- writebuf[i] = CUPS_RAND();
+ writebuf[i] = (unsigned char)CUPS_RAND();
/*
* cupsFileOpen(write)
fputs("cupsFileGetConf(): ", stdout);
- for (i = 0; i < 1000; i ++)
+ for (i = 0, value = NULL; i < 1000; i ++)
if (!cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
break;
else if (_cups_strcasecmp(line, "TestLine") || !value || atoi(value) != i ||
fputs("cupsFileGetChar(): ", stdout);
- for (i = 0; i < 256; i ++)
+ for (i = 0, byte = 0; i < 256; i ++)
if ((byte = cupsFileGetChar(fp)) != i)
break;
fputs("cupsFileRead(): ", stdout);
- for (i = 0; i < 10000; i ++)
- if ((byte = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0)
+ for (i = 0, bytes = 0; i < 10000; i ++)
+ if ((bytes = cupsFileRead(fp, (char *)readbuf, sizeof(readbuf))) < 0)
break;
else if (memcmp(readbuf, writebuf, sizeof(readbuf)))
break;
if (i >= 10000)
puts("PASS");
- else if (byte > 0)
+ else if (bytes > 0)
{
printf("FAIL (Pass %d, ", i);
* Remove the test file...
*/
- unlink(compression ? "testfile.dat.gz" : "testfile.dat");
+ if (!status)
+ unlink(compression ? "testfile.dat.gz" : "testfile.dat");
/*
* Return the test status...
return (status);
}
-
-
-/*
- * End of "$Id$".
- */