File: tmp-files.c

package info (click to toggle)
tla 1.3.5%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 22,292 kB
  • ctags: 11,952
  • sloc: ansic: 149,771; sh: 16,009; xml: 2,689; lisp: 1,927; makefile: 1,064; cpp: 363; tcl: 230; awk: 48; sed: 25
file content (101 lines) | stat: -rw-r--r-- 2,228 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* tmp-files.c:
 *
 ****************************************************************
 * Copyright (C) 2003 Tom Lord
 *
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */


#include "hackerlab/os/time.h"
#include "hackerlab/os/sys/types.h"
#include "hackerlab/os/unistd.h"
#include "hackerlab/fmt/cvt.h"
#include "hackerlab/char/str.h"
#include "hackerlab/char/str-many.h"
#include "hackerlab/vu/safe.h"
#include "hackerlab/fs/file-names.h"
#include "hackerlab/fs/tmp-files.h"
#include "hackerlab/vu/safe.h"
#include "tla/libfsutils/tmp-files.h"
#include "tla/libarch/cmdutils.h"



static int seq = 0;

t_uchar *
tmp_file_name (const t_uchar * dir,
               const t_uchar * basename)
{
  time_t now;
  pid_t pid;
  t_uchar now_str[32];
  t_uchar pid_str[32];
  t_uchar seq_str[32];
  t_uchar * path;

  now = time (0);
  pid = getpid ();
  ++seq;

  cvt_ulong_to_decimal (now_str, (unsigned long)now);
  cvt_ulong_to_decimal (pid_str, (unsigned long)pid);
  cvt_ulong_to_decimal (seq_str, (unsigned long)seq);

  path = file_name_in_vicinity (0, dir, basename);
  path = str_realloc_cat_many (0, path, ".", now_str, ".", pid_str, ".", seq_str, str_end);

  return path;
}

t_uchar *
tmp_seq_file (const t_uchar * dir,
              const t_uchar * basename)
{
  int seq = 0;

  while (1)
    {
      t_uchar seq_str[32];
      t_uchar * path = 0;

      cvt_ulong_to_decimal (seq_str, (unsigned long)seq);

      path = file_name_in_vicinity (0, dir, basename);
      path = str_realloc_cat_many (0, path, ".", seq_str, str_end);

      if (safe_access (path, F_OK))
        return path;

      ++seq;
      lim_free (0, path);
      path = 0;
    }
}

/**
 * \brief return a semi-random pathname, honouring TMP
 *
 * \return a absolute path
 */
t_uchar *
tmp_file_name_in_tmp (t_uchar const * const basename)
{
    t_uchar * tmpdir = tmp_dir(NULL);
    t_uchar * talloced_result = str_replace (tmpdir,
                                            tmp_file_name (tmpdir, basename));
    t_uchar * result = str_save (0, talloced_result);
    lim_free (0, talloced_result);
    return result;
}







/* tag: Tom Lord Thu May 15 23:22:25 2003 (tmp-files.c)
 */