guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'substitute*' fails with "string contains #\nul character"


From: Nikita Karetnikov
Subject: Re: 'substitute*' fails with "string contains #\nul character"
Date: Tue, 06 Aug 2013 01:20:12 +0400

>> ice-9/boot-9.scm:106:20: string contains #\nul character:

> Yes, that is expected.  ‘substitute*’ is for textual files.

> ‘fold-port-matches’ from (guix build utils) can be used for binary files
> (see ‘remove-store-references’ for an example use.)

Here’s what I’m trying to use:

(use-modules (srfi srfi-14)
             (guix build utils)
             (rnrs bytevectors)
             (rnrs io ports))

(define (binary-substitute file old new)
  "Replace an OLD string with a NEW one in FILE."
  (with-atomic-file-replacement file
    (lambda (in out)
      (format #t "replacing '~a' with '~a' in '~a'...~%"
              old new file)

      (fold-port-matches (lambda (match result)
                           (put-bytevector out (string->utf8 new))
                           #t)
                         #f
                         old
                         in
                         (lambda (char result)
                           (put-u8 out (char->integer char))
                           result)))))

If you compile this program:

#include <stdio.h>

int main()
{
  printf("Hello, world!\n");

  return 0;
}

and try to replace “Hello” with “Hallo,” it will work.  If strings are
not equally sized, ‘a.out’ will crash with a segfault.

I’m not sure how to proceed and fail to find any howtos.  Is there a
tool that I could use?

Attachment: pgpyByoA6Lv4a.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]