Lumiera  0.pre.03
»edit your freedom«
luidgen.c
Go to the documentation of this file.
1 /*
2  Luidgen - generate and replace Lumiera UIDs for source files
3 
4  Copyright (C)
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
14 
30 #include "lib/tmpbuf.h"
31 #include "lib/luid.h"
32 
33 #include <unistd.h>
34 #include <stdint.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <nobug.h>
39 
40 
41 
42 int
43 main (int argc, char** argv)
44 {
45  NOBUG_INIT;
46  lumiera_uid luid;
47 
48  if (argc == 1)
49  {
50  lumiera_uid_gen (&luid);
51  printf ("\"");
52  for (int i = 0; i < 16; ++i)
53  printf ("\\%.3hho", *(((char*)&luid)+i));
54  printf ("\"\n");
55  }
56  else
57  {
58  for (int i = 1; i < argc; ++i)
59  {
60  FILE* in = fopen (argv[i], "r");
61  if (!in)
62  {
63  fprintf (stderr, "Failed to open file %s for reading: %s\n", argv[i], strerror (errno));
64  continue;
65  }
66 
67  char* outname = lumiera_tmpbuf_snprintf (SIZE_MAX, "%s.luidgen", argv[i]);
68  FILE* out = fopen (outname, "wx");
69  if (!out)
70  {
71  fprintf (stderr, "Failed to open file %s for writing: %s\n", outname, strerror (errno));
72  fclose (in);
73  continue;
74  }
75 
76  char buf[4096];
77  char luidbuf[67];
78 
79  printf ("Luidgen %s ", argv[i]); fflush (stdout);
80 
81  while (fgets (buf, 4096, in))
82  {
83  char* pos;
84  while ((pos = strstr(buf, "LUIDGEN")))
85  {
86  memmove (pos+66, pos+7, strlen (pos+7)+1);
87  lumiera_uid_gen (&luid);
88  sprintf (luidbuf, "\""LUMIERA_UID_FMT"\"", LUMIERA_UID_ELEMENTS(luid));
89  memcpy (pos, luidbuf, 66);
90  putchar ('.'); fflush (stdout);
91  }
92  fputs (buf, out);
93  }
94 
95  fclose (out);
96  fclose (in);
97 
98  char* backup = lumiera_tmpbuf_snprintf (SIZE_MAX, "%s~", argv[i]);
99  unlink (backup);
100 
101  if (!!rename (argv[i], backup))
102  {
103  fprintf (stderr, "Failed to create backupfile %s: %s\n", backup, strerror (errno));
104  continue;
105  }
106 
107  if (!!rename (outname, argv[i]))
108  {
109  fprintf (stderr, "Renaming %s to %s failed: %s\n", outname, argv[i], strerror (errno));
110  rename (backup, argv[i]);
111  continue;
112  }
113 
114  printf (" done\n");
115  }
116  }
117 
118  return 0;
119 }
120 
121 /*
122 // Local Variables:
123 // mode: C
124 // c-file-style: "gnu"
125 // indent-tabs-mode: nil
126 // End:
127 */
int main(int argc, const char *argv[])
run all tests or any single test specified in the first command line argument.
Definition: testrunner.cpp:37
Round robin temporary buffers.
Lumiera unique object identifier.
void lumiera_uid_gen(lumiera_uid *luid)
Generate a new luid.
Definition: luid.c:48
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:40
char * lumiera_tmpbuf_snprintf(size_t size, const char *fmt,...)
Construct a string in a tmpbuf.
Definition: tmpbuf.c:116