Commit 468b5a52 authored by Colomban Wendling's avatar Colomban Wendling

Initial import

parents
*.o
/aclocal.m4
/autom4te.cache/
/build/
/config.*
/configure
.deps/
Makefile
Makefile.in
wrap
EXTRA_DIST = autogen.sh
bin_PROGRAMS = wrap
wrap_SOURCES = wrap.c
#!/bin/sh
set -e
[ -d build ] || mkdir build
autoreconf -vfi
AC_PREREQ([2.64])
AC_INIT([wrap], [0.1], [ban@herbesfolles.org])
AC_CONFIG_SRCDIR([wrap.c])
AC_CONFIG_AUX_DIR([build])
AM_INIT_AUTOMAKE([1.11.1 foreign])
AC_PROG_CC
AC_CHECK_FUNCS([getopt])
AC_CHECK_HEADERS_ONCE([assert.h unistd.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
/*
* Copyright 2015 Colomban Wendling <ban@herbesfolles.org>
*
* 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.
*/
#define _POSIX_C_SOURCE 2 /* for getopt() */
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int
wrap (FILE *const fp,
size_t const width,
size_t const tabwidth)
{
char buf[BUFSIZ];
size_t n;
size_t w = 0;
assert (tabwidth > 0);
do
{
size_t i;
n = fread (buf, 1, sizeof buf, fp);
for (i = 0; i < n; i++)
{
if (buf[i] == '\n')
w = 0;
else
{
if (w >= width)
{
if (fputc ('\n', stdout) == EOF)
return -1;
w = 0;
}
if (buf[i] == '\t')
w = w + tabwidth - (w % tabwidth);
else
w++;
}
if (fputc (buf[i], stdout) == EOF)
return -1;
}
}
while (n == sizeof buf);
return ferror (fp) ? -1 : 0;
}
int
main (int argc,
char **argv)
{
size_t columns = 72;
size_t tabwidth = 8;
int ret = 0;
int opt;
int i;
while ((opt = getopt (argc, argv, "c:t:h")) != -1)
{
char *endptr;
switch (opt)
{
case 'c':
columns = strtoul (optarg, &endptr, 0);
if (*endptr || columns < 1)
{
fprintf (stderr, "invalid argument '%s' for option '%c'\n",
optarg, opt);
return 1;
}
break;
case 't':
tabwidth = strtoul (optarg, &endptr, 0);
if (*endptr || tabwidth < 1)
{
fprintf (stderr, "invalid argument '%s' for option '%c'\n",
optarg, opt);
return 1;
}
break;
case 'h':
default:
fprintf (opt == 'h' ? stdout : stderr,
"USAGE: %s [-c COLUMNS] [-t TABWIDTH] [FILE...]\n",
argv[0]);
return opt == 'h' ? 0 : 1;
}
}
if (argc <= optind)
{
if (wrap (stdin, columns, tabwidth) != 0)
ret = 1;
}
for (i = optind; ret == 0 && i < argc; i++)
{
FILE *fp = fopen (argv[i], "rb");
if (! fp)
{
fprintf (stderr, "failed to open file '%s': %s\n",
argv[i], strerror (errno));
ret = 1;
}
else
{
if (wrap (fp, columns, tabwidth) != 0)
ret = 1;
if (fclose (fp) != 0)
ret = 1;
}
}
return ret;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment