view toys/posix/uuencode.c @ 1531:3ff823086c99 draft

Teach ln -f to leave original target alone if link creation fails. Suggested by Ashwini Sharma, I wound up implementing it by creating the new link at a temporary name and renaming it over the old one instead of renaming the old file out of the way and putting it back if it failed. (Because "mkdir -p one/one/blah && ln -sf /bin/one one" would otherwise rename one/one out of the way and only notice it can't delete it way at the end when recovery's darn awkward, vs create new thing and if rename fails (including EISDIR) that's the main error path. And yes the temporary name is in the same directory as the destination so we never rename between mounts.) link over the old one instead of renaming the old file and renaming it back.
author Rob Landley <rob@landley.net>
date Wed, 22 Oct 2014 17:11:06 -0500
parents 357b31ae2f04
children c4c9267467f8
line wrap: on
line source

/* uuencode.c - uuencode / base64 encode
 *
 * Copyright 2013 Erich Plondke <toybox@erich.wreck.org>
 *
 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/uuencode.html

USE_UUENCODE(NEWTOY(uuencode, "<1>2m", TOYFLAG_USR|TOYFLAG_BIN))

config UUENCODE
  bool "uuencode"
  default y 
  help
    usage: uuencode [-m] [file] encode-filename

    Uuencode stdin (or file) to stdout, with encode-filename in the output.

    -m	base64-encode
*/

#define FOR_uuencode
#include "toys.h"

void uuencode_main(void)
{
  char *p, *name = toys.optargs[toys.optc-1], buf[(76/4)*3];

  int i, m = toys.optflags & FLAG_m, fd = 0;

  if (toys.optc > 1) fd = xopen(toys.optargs[0], O_RDONLY);

  // base64 table

  p = toybuf;
  for (i = 'A'; i != ':'; i++) {
    if (i == 'Z'+1) i = 'a';
    if (i == 'z'+1) i = '0';
    *(p++) = i;
  }
  *(p++) = '+';
  *(p++) = '/';

  xprintf("begin%s 744 %s\n", m ? "-base64" : "", name);
  for (;;) {
    char *in;

    if (!(i = xread(fd, buf, m ? sizeof(buf) : 45))) break;

    if (!m) xputc(i+32);
    in = buf;

    for (in = buf; in-buf < i; ) {
      int j, x, bytes = i - (in-buf);

      if (bytes > 3) bytes = 3;

      for (j = x = 0; j<4; j++) {
        int out;

        if (j < bytes) x |= (*(in++) & 0x0ff) << (8*(2-j));
        out = (x>>((3-j)*6)) & 0x3f;
        xputc(m ? (j > bytes ? '=' : toybuf[out]) : (out ? out + 0x20 : 0x60));
      } 
    }
    xputc('\n');
  }
  xputs(m ? "====" : "end");
}