struct temp_file {
+ char *name, *path;
FILE *fp;
- char name[1];
};
FILE *scc_tmpfile (void) {
- char *temp_path, *template;
+ char *temp_path, *template, *name, *p;
struct hashtab_name *key;
struct temp_file *tmp;
}
template = xmalloc (strlen (temp_path) + 1 + 10 + 1);
+
+again:
+
sprintf (template, "%s/sccXXXXXX", temp_path);
if ((fd = mkstemp (template)) < 0) {
return 0;
}
- if ((fp = fdopen (fd, "w+b"))) {
+ if ((p = strrchr (template, '/'))) {
+ name = xstrdup (p + 1);
+ } else {
+ name = xstrdup (template);
+ }
- if (!(key = hashtab_get_key (&hashtab_tmpfiles, template))) {
-
- if (!((key = hashtab_alloc_name (xstrdup (template))))) {
-
- remove (template);
- fclose (fp);
-
- return 0;
-
- }
-
- }
+ if ((key = hashtab_get_key (&hashtab_tmpfiles, name))) {
+
+ close (fd);
- tmp = xmalloc (sizeof (*tmp) + strlen (template )+ 1);
+ remove (template);
+ free (name);
- sprintf (tmp->name, "%s", template);
+ goto again;
+
+ }
+
+ if (!((key = hashtab_alloc_name (name)))) {
+
+ report_at (program_name, 0, REPORT_ERROR, "failed to allocate memory for key");
+ exit (EXIT_FAILURE);
+
+ }
+
+ if ((fp = fdopen (fd, "w+b"))) {
+
+ tmp = xmalloc (sizeof (*tmp));
tmp->fp = fp;
+ tmp->path = xstrdup (template);
+ tmp->name = name;
+
hashtab_put (&hashtab_tmpfiles, key, tmp);
+
+ free (template);
return fp;
}
+ free (name);
+ free (key);
+
+ close (fd);
+
+ remove (template);
+ free (template);
+
return 0;
}
continue;
}
- if (!entry->key || !entry->value) {
+ if (!entry->key || !entry->value || entry->value == HASHTAB_TOMBSTONE) {
continue;
}
return ret;
}
- remove (tmp->name);
+ hashtab_remove (&hashtab_tmpfiles, entry->key);
+ remove (tmp->path);
- hashtab_remove (&hashtab_tmpfiles, hashtab_get_key (&hashtab_tmpfiles, tmp->name));
+ free (tmp->path);
+ free (tmp->name);
+
+ free (tmp);
return 0;
}
continue;
}
- if (!entry->key || !entry->value) {
+ if (!entry->key || !entry->value || entry->value == HASHTAB_TOMBSTONE) {
continue;
}
continue;
}
- remove (tmp->name);
- hashtab_remove (&hashtab_tmpfiles, hashtab_get_key (&hashtab_tmpfiles, tmp->name));
+ remove (tmp->path);
}
struct hashtab_name *key;
struct temp_file *tmp;
- char *name;
+ char *name, *p;
FILE *fp;
if (!GetTempPath (MAX_PATH + 1, temp_path)) {
return 0;
}
+again:
+
if (!GetTempFileName (temp_path, "scc", 0, full_path)) {
return 0;
}
- if ((name = strrchr (full_path, '\\'))) {
- name++;
+ if ((p = strrchr (full_path, '\\'))) {
+ name = xstrdup (p + 1);
} else {
- name = full_path;
+ name = xstrdup (full_path);
+ }
+
+ if ((key = hashtab_get_key (&hashtab_tmpfiles, name))) {
+
+ free (name);
+ goto again;
+
+ }
+
+ if (!((key = hashtab_alloc_name (name)))) {
+
+ report_at (program_name, 0, REPORT_ERROR, "failed to allocate memory for key");
+ exit (EXIT_FAILURE);
+
}
if ((fp = fopen (full_path, "w+b"))) {
-
- if (!(key = hashtab_get_key (&hashtab_tmpfiles, name))) {
-
- if (!((key = hashtab_alloc_name (xstrdup (name))))) {
-
- remove (full_path);
- fclose (fp);
-
- return 0;
-
- }
-
- }
-
+
tmp = xmalloc (sizeof (*tmp));
tmp->fp = fp;
tmp->path = xstrdup (full_path);
- tmp->name = xstrdup (name);
+ tmp->name = name;
hashtab_put (&hashtab_tmpfiles, key, tmp);
return fp;
}
+ free (name);
+ free (key);
+
return 0;
}
int scc_close (FILE *fp) {
struct hashtab_entry *entry;
- struct temp_file *tmp;
+ struct hashtab_name *key;
+ struct temp_file *tmp;
int ret, i;
for (i = 0; i < hashtab_tmpfiles.capacity; i++) {
continue;
}
- if (!entry->key || !entry->value) {
+ if (!entry->key || !entry->value || entry->value == HASHTAB_TOMBSTONE) {
continue;
}
return ret;
}
+ hashtab_remove (&hashtab_tmpfiles, entry->key);
remove (tmp->path);
- hashtab_remove (&hashtab_tmpfiles, hashtab_get_key (&hashtab_tmpfiles, tmp->name));
+ free (tmp->path);
+ free (tmp->name);
+
return 0;
}
continue;
}
- if (!entry->key || !entry->value) {
+ if (!entry->key || !entry->value || entry->value == HASHTAB_TOMBSTONE) {
continue;
}
}
remove (tmp->path);
- hashtab_remove (&hashtab_tmpfiles, hashtab_get_key (&hashtab_tmpfiles, tmp->name));
}
struct temp_file {
+ char *name;
FILE *fp;
- char name[1];
};
static unsigned long temp_id = 1;
- char name[64];
+ char name[64], *temp;
unsigned long i;
struct hashtab_name *key;
}
+ temp = xstrdup (name);
+
+ if ((key = hashtab_get_key (&hashtab_tmpfiles, temp))) {
+
+ free (temp);
+ continue;
+
+ }
+
+ if (!((key = hashtab_alloc_name (temp)))) {
+
+ report_at (program_name, 0, REPORT_ERROR, "failed to allocate memory for key");
+ exit (EXIT_FAILURE);
+
+ }
+
if ((fp = fopen (name, "w+b"))) {
- if (!(key = hashtab_get_key (&hashtab_tmpfiles, name))) {
-
- if (!((key = hashtab_alloc_name (xstrdup (name))))) {
-
- remove (name);
- fclose (fp);
-
- continue;
-
- }
-
- }
-
- tmp = xmalloc (sizeof (*tmp) + strlen (name) + 1);
- sprintf (tmp->name, "%s", name);
+ tmp = xmalloc (sizeof (*tmp));
+ tmp->name = temp;
tmp->fp = fp;
hashtab_put (&hashtab_tmpfiles, key, tmp);
return fp;
}
+
+ free (temp);
+ free (key);
}
int scc_close (FILE *fp) {
struct hashtab_entry *entry;
- struct temp_file *tmp;
+ struct hashtab_name *key;
+ struct temp_file *tmp;
int ret, i;
for (i = 0; i < hashtab_tmpfiles.capacity; i++) {
continue;
}
- if (!entry->key || !entry->value) {
+ if (!entry->key || !entry->value || entry->value == HASHTAB_TOMBSTONE) {
continue;
}
return ret;
}
+ hashtab_remove (&hashtab_tmpfiles, entry->key);
remove (tmp->name);
- hashtab_remove (&hashtab_tmpfiles, hashtab_get_key (&hashtab_tmpfiles, tmp->name));
+ free (tmp->name);
return 0;
}
continue;
}
- if (!entry->key || !entry->value) {
+ if (!entry->key || !entry->value || entry->value == HASHTAB_TOMBSTONE) {
continue;
}
}
remove (tmp->name);
- hashtab_remove (&hashtab_tmpfiles, hashtab_get_key (&hashtab_tmpfiles, tmp->name));
}