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) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
39 #include "lib/tmpbuf.h"
40 #include "lib/luid.h"
41 
42 #include <unistd.h>
43 #include <stdint.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <nobug.h>
48 
49 
50 
51 int
52 main (int argc, char** argv)
53 {
54  NOBUG_INIT;
55  lumiera_uid luid;
56 
57  if (argc == 1)
58  {
59  lumiera_uid_gen (&luid);
60  printf ("\"");
61  for (int i = 0; i < 16; ++i)
62  printf ("\\%.3hho", *(((char*)&luid)+i));
63  printf ("\"\n");
64  }
65  else
66  {
67  for (int i = 1; i < argc; ++i)
68  {
69  FILE* in = fopen (argv[i], "r");
70  if (!in)
71  {
72  fprintf (stderr, "Failed to open file %s for reading: %s\n", argv[i], strerror (errno));
73  continue;
74  }
75 
76  char* outname = lumiera_tmpbuf_snprintf (SIZE_MAX, "%s.luidgen", argv[i]);
77  FILE* out = fopen (outname, "wx");
78  if (!out)
79  {
80  fprintf (stderr, "Failed to open file %s for writing: %s\n", outname, strerror (errno));
81  fclose (in);
82  continue;
83  }
84 
85  char buf[4096];
86  char luidbuf[67];
87 
88  printf ("Luidgen %s ", argv[i]); fflush (stdout);
89 
90  while (fgets (buf, 4096, in))
91  {
92  char* pos;
93  while ((pos = strstr(buf, "LUIDGEN")))
94  {
95  memmove (pos+66, pos+7, strlen (pos+7)+1);
96  lumiera_uid_gen (&luid);
97  sprintf (luidbuf, "\""LUMIERA_UID_FMT"\"", LUMIERA_UID_ELEMENTS(luid));
98  memcpy (pos, luidbuf, 66);
99  putchar ('.'); fflush (stdout);
100  }
101  fputs (buf, out);
102  }
103 
104  fclose (out);
105  fclose (in);
106 
107  char* backup = lumiera_tmpbuf_snprintf (SIZE_MAX, "%s~", argv[i]);
108  unlink (backup);
109 
110  if (!!rename (argv[i], backup))
111  {
112  fprintf (stderr, "Failed to create backupfile %s: %s\n", backup, strerror (errno));
113  continue;
114  }
115 
116  if (!!rename (outname, argv[i]))
117  {
118  fprintf (stderr, "Renaming %s to %s failed: %s\n", outname, argv[i], strerror (errno));
119  rename (backup, argv[i]);
120  continue;
121  }
122 
123  printf (" done\n");
124  }
125  }
126 
127  return 0;
128 }
129 
130 /*
131 // Local Variables:
132 // mode: C
133 // c-file-style: "gnu"
134 // indent-tabs-mode: nil
135 // End:
136 */
int main(int argc, const char *argv[])
run all tests or any single test specified in the first command line argument.
Definition: testrunner.cpp:47
Round robin temporary buffers.
Lumiera unique object identifier.
void lumiera_uid_gen(lumiera_uid *luid)
Generate a new luid.
Definition: luid.c:57
unsigned char lumiera_uid[16]
storage for a Lumiera unique ID, based on a 128bit random number
Definition: hash-value.h:45
char * lumiera_tmpbuf_snprintf(size_t size, const char *fmt,...)
Construct a string in a tmpbuf.
Definition: tmpbuf.c:125