]> sjero.net Git - wget/commitdiff
Added util/trunc.
authorMicah Cowan <micah@cowan.name>
Mon, 4 Feb 2008 00:10:34 +0000 (16:10 -0800)
committerMicah Cowan <micah@cowan.name>
Mon, 4 Feb 2008 00:10:34 +0000 (16:10 -0800)
.hgignore
ChangeLog
util/Makefile.am
util/README
util/trunc.c [new file with mode: 0644]

index 3832d65e6c8392cfd55557c350f21d828df58fa9..666c5c382419fb9ad7c1e6a47c6fa927868526fe 100644 (file)
--- a/.hgignore
+++ b/.hgignore
@@ -61,5 +61,8 @@ po/remove-potcdate.sed
 po/stamp-po
 po/en@boldquot.insert-header
 po/en@quot.insert-header
+util/trunc
+util/trunc.o
+util/.deps
 **~
 **.*.swp
index cde46e57bf3e05d321eba196120abed96313321a..4d92ee2f3b9690f3ce3b3c33a10a2e8df9160751 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,9 @@
        * configure.in: Add checks for wchar.h, wcwidth function (to
        support column-counting in progress.c).
        * NEWS: Added line for 1.11.1.
+       * util/README, util/Makefile.am, util/trunc.c: Added a small
+       utility program to create files of arbitrary size (useful for
+       testing certain situations with --continue).
 
 2008-01-31  Micah Cowan  <micah@cowan.name>
 
index a4d3cfa528628f7b0017da13f36dcc10df0457d1..798665806018b182d5e0cce13194fc9170aeaeed 100644 (file)
@@ -30,3 +30,5 @@
 #
 
 EXTRA_DIST = README rmold.pl
+
+bin_PROGRAMS = trunc
index cb305907b1920497faa7c35dc4f1228a2827a7f3..8570832092f2dc7b465225f802e3622884f45d8c 100644 (file)
@@ -9,3 +9,7 @@ This Perl script is used to check which local files are no longer on
 the remote server.  You can use it to get the list of files, or
 $ rmold.pl [dir] | xargs rm
 
+trunc
+=====
+This small program may be used to create files of arbitrary size; useful
+for testing certain scenarios using wget's --continue option.
diff --git a/util/trunc.c b/util/trunc.c
new file mode 100644 (file)
index 0000000..fb52ec2
--- /dev/null
@@ -0,0 +1,132 @@
+/* trunc.c: Set the size of an existing file, or create a file of a
+ *          specified size.
+ *
+ * Copyright (C) 2008 Micah J. Cowan
+ *
+ * Copying and distribution of this file, with or without modification, 
+ * are permitted in any medium without royalty provided the copyright
+ * notice and this notice are preserved. */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#define PROGRAM_NAME  "trunc"
+
+void
+usage (FILE *f)
+{
+  fputs (
+PROGRAM_NAME " [-c] file sz\n\
+\n\
+Set the filesize of FILE to SIZE.\n\
+\n\
+  -c: create FILE if it doesn't exist.\n\
+\n\
+  Multiplier suffixes for SIZE (case-insensitive):\n\
+      k: SIZE * 1024\n\
+      m: SIZE * 1024 * 1024\n", f);
+}
+
+off_t
+get_size (const char str[])
+{
+  unsigned long val;
+  int suffix;
+  char *end;
+
+  errno = 0;
+  val = strtoul(str, &end, 10);
+  if (end == str)
+    {
+      fputs (PROGRAM_NAME ": size is not a number.\n", stderr);
+      usage (stderr);
+      exit (EXIT_FAILURE);
+    }
+  else if (errno == ERANGE
+           || (unsigned long)(off_t)val != val)
+    {
+      fputs (PROGRAM_NAME ": size is out of range.\n", stderr);
+      exit (EXIT_FAILURE);
+    }
+
+  suffix = tolower ((unsigned char) end[0]);
+  if (suffix == 'k')
+    {
+      val *= 1024;
+    }
+  else if (suffix == 'm')
+    {
+      val *= 1024 * 1024;
+    }
+  
+  return val;
+}
+
+int
+main (int argc, char *argv[])
+{
+  const char *fname;
+  const char *szstr;
+  off_t      sz;
+  int        create = 0;
+  int        option;
+  int        fd;
+
+  /* Parse options. */
+  while ((option = getopt (argc, argv, "c")) != -1)
+    {
+      switch (option) {
+        case 'c':
+          create = 1;
+          break;
+        case '?':
+          fprintf (stderr, PROGRAM_NAME ": Unrecognized option `%c'.\n\n",
+                   optopt);
+          usage (stderr);
+          exit (EXIT_FAILURE);
+        default:
+          /* We shouldn't reach here. */
+          abort();
+      }
+    }
+
+  if (argv[optind] == NULL
+      || argv[optind+1] == NULL
+      || argv[optind+2] != NULL)
+    {
+      usage (stderr);
+      exit (EXIT_FAILURE);
+    }
+
+  fname = argv[optind];
+  szstr = argv[optind+1];
+
+  sz = get_size(szstr);
+  if (create)
+    {
+      mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+      fd = open(fname, O_WRONLY | O_CREAT, mode);
+    }
+  else
+    {
+      fd = open(fname, O_WRONLY);
+    }
+
+  if (fd == -1)
+    {
+      perror (PROGRAM_NAME ": open");
+      exit (EXIT_FAILURE);
+    }
+
+  if (ftruncate(fd, sz) == -1)
+    {
+      perror (PROGRAM_NAME ": truncate");
+      exit (EXIT_FAILURE);
+    }
+
+  return 0;
+}