[sane-devel] [frontends] scanadf no-overwrite patch
Paul Walmsley
paul@booyaka.com
Tue, 6 Jul 2004 01:35:29 -0600 (MDT)
scanadf happily overwrites existing image files. This patch adds a
command-line flag that will prevent scanadf from doing this.
- Paul
diff -U 5 -r sane-frontends-1.0.12/doc/scanadf.man sane-frontends-1.0.12-patched/doc/scanadf.man
--- sane-frontends-1.0.12/doc/scanadf.man 2003-09-24 09:56:17.000000000 -0600
+++ sane-frontends-1.0.12-patched/doc/scanadf.man 2004-07-06 01:33:32.998215936 -0600
@@ -10,10 +10,11 @@
.RB [ -L | --list-devices ]
.RB [ -v | --verbose ]
.RB [ -V | --version ]
.RB [ -o | --output-file
.IR name ]
+.RB [ -N | --no-overwrite ]
.RB [ -S | --scan-script
.IR name ]
.RB [ -s | --start-count
.IR num ]
.RB [ -e | --end-count
@@ -116,10 +117,19 @@
name; this will be replaced with the current page number. The default
format string is image-%04d.
.PP
The
+.B -N
+or
+.B --no-overwrite
+option prevents
+.B scanadf
+from overwriting existing image files.
+
+.PP
+The
.B -S
or
.B --scan-script
option specifies the name of script to run after each scanned image
is acquired. The script receives the name of the image output file
diff -U 5 -r sane-frontends-1.0.12/src/scanadf.c sane-frontends-1.0.12-patched/src/scanadf.c
--- sane-frontends-1.0.12/src/scanadf.c 2002-01-16 14:16:29.000000000 -0700
+++ sane-frontends-1.0.12-patched/src/scanadf.c 2004-07-06 01:24:57.033654472 -0600
@@ -36,10 +36,11 @@
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
+#include <sys/stat.h>
#include "sane/sane.h"
#include "sane/sanei.h"
#include "sane/saneopts.h"
@@ -93,20 +94,21 @@
{"device-name", required_argument, NULL, 'd'},
{"list-devices", no_argument, NULL, 'L'},
{"help", no_argument, NULL, 'h'},
{"verbose", no_argument, NULL, 'v'},
{"version", no_argument, NULL, 'V'},
+ {"no-overwrite", no_argument, NULL, 'N'},
{ "output-file", required_argument, 0, 'o' },
{ "start-count", required_argument, 0, 's' },
{ "end-count", required_argument, 0, 'e' },
{ "scan-script", required_argument, 0, 'S' },
{ "raw", no_argument, 0, 'r' },
{0, }
};
-#define BASE_OPTSTRING "d:hLvVTo:s:e:S:r"
+#define BASE_OPTSTRING "d:hLvVNTo:s:e:S:r"
#define STRIP_HEIGHT 256 /* # lines we increment image height */
static struct option * all_options;
static int option_number_len;
static int * option_number;
@@ -128,10 +130,11 @@
[ -d | --device-name <device> ] use a given scanner device.\n\
[ -h | --help ] display this help message and exit.\n\
[ -L | --list-devices ] show available scanner devices.\n\
[ -v | --verbose ] give even more status messages.\n\
[ -V | --version ] print version information.\n\
+ [ -N | --no-overwrite ] don't overwrite existing files.\n\
\n\
[ -o | --output-file <name> ] name of file to write image data\n\
(%%d replacement in output file name).\n\
[ -S | --scan-script <name> ] name of script to run after every scan.\n\
[ -s | --start-count <num> ] page count of first scanned image.\n\
@@ -1208,22 +1211,41 @@
return status;
}
static SANE_Int
-scan_docs (int start, int end, SANE_Bool raw, const char *outfmt, const char *script)
+scan_docs (int start, int end, int no_overwrite, SANE_Bool raw, const char *outfmt, const char *script)
{
SANE_Status status = SANE_STATUS_GOOD;
SANE_Int scannedPages = 0;
SANE_Char fname[PATH_MAX];
+ struct stat statbuf;
+ int res;
+ int skip_scan = 0;
while (end < 0 || start <= end)
{
/*!!! buffer overflow; need protection */
sprintf(fname, outfmt, start);
- status = scan_it_raw(fname, raw, script);
+ /* does the filename already exist? */
+ if (no_overwrite)
+ {
+ res = stat (fname, &statbuf);
+ if (res == 0)
+ {
+ status = SANE_STATUS_INVAL;
+ fprintf (stderr, "Filename %s already exists; will not overwrite\n", fname);
+ skip_scan = 1;
+ }
+ }
+
+ /* Scan the document */
+ if (status == SANE_STATUS_GOOD)
+ status = scan_it_raw(fname, raw, script);
+
+ /* Any scan errors? */
if (status == SANE_STATUS_NO_DOCS)
{
/* out of paper in the hopper; this is our normal exit */
status = SANE_STATUS_GOOD;
break;
@@ -1261,10 +1283,11 @@
char *full_optstring;
SANE_Bool raw = SANE_FALSE;
const char *scanScript = NULL; /* script to run at end of scan */
const char *outputFile = "image-%04d"; /* file name(format) to write output to */
int startNum = 1, endNum = -1; /* start/end numbers of pages to scan */
+ int no_overwrite = 0;
atexit (sane_exit);
prog_name = strrchr (argv[0], '/');
if (prog_name)
@@ -1287,10 +1310,11 @@
case '?':
break; /* may be an option that we'll parse later on */
case 'd': devname = optarg; break;
case 'h': help = 1; break;
+ case 'N': no_overwrite = 1; break;
case 'v': ++verbose; break;
case 'L':
{
int i;
@@ -1545,11 +1569,11 @@
signal (SIGHUP, sighandler);
signal (SIGINT, sighandler);
signal (SIGPIPE, sighandler);
signal (SIGTERM, sighandler);
- status = scan_docs (startNum, endNum, raw, outputFile, scanScript);
+ status = scan_docs (startNum, endNum, no_overwrite, raw, outputFile, scanScript);
sane_cancel (device);
sane_close (device);
return (status == SANE_STATUS_GOOD) ? 0 : 1;