~ubuntu-branches/ubuntu/lucid/debianutils/lucid

« back to all changes in this revision

Viewing changes to tempfile.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2009-11-14 13:16:01 UTC
  • mfrom: (1.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20091114131601-24a06erceh63t4sx
Tags: 3.2.2
* savelog: patch from Patrick Coleman to fix breakage when target file
  does not exist.  closes: #556227.
* Bump to Standards-Version 3.8.3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
char *progname;
14
14
 
 
15
void usage(int);
 
16
void syserror(const char *);
 
17
int parsemode(const char *, mode_t *);
 
18
 
15
19
void
16
20
usage (int status)
17
21
{
55
59
int
56
60
main (int argc, char **argv)
57
61
{
58
 
  char *name=0, *dir=0, *pfx=0, *sfx=0;
 
62
  char *name=0, *dir=0, *pfx=0, *sfx=0, *filename=0;
59
63
  mode_t mode = 0600;
60
64
  int fd, optc;
61
65
  struct option long_options[] = {
91
95
      }
92
96
      break;
93
97
    case 'n':
94
 
      name = optarg;
 
98
      /* strdup because it is freed later on */
 
99
      if((name = strdup(optarg)) == NULL)
 
100
        syserror("strdup");
95
101
      break;
96
102
    case 'h':
97
103
      usage(0);
106
112
  if (name) {
107
113
    if ((fd = open(name, O_RDWR | O_CREAT | O_EXCL, mode)) < 0)
108
114
      syserror("open");
 
115
    filename = name;
109
116
  }
110
 
 
111
117
  else {
112
118
    for (;;) {
113
119
      if (!(name = tempnam(dir, pfx)))
114
120
        syserror("tempnam");
115
 
      if ((fd = open(name, O_RDWR | O_CREAT | O_EXCL, mode)) < 0) {
 
121
      if(sfx) {
 
122
        char *namesfx;
 
123
        if (!(namesfx = (char *)malloc(strlen(name) + strlen(sfx) + 1)))
 
124
          syserror("malloc");
 
125
        strcpy(namesfx, name);
 
126
        strcat(namesfx, sfx);
 
127
        filename = namesfx;
 
128
      }
 
129
      else
 
130
        filename = name;
 
131
 
 
132
      if ((fd = open(filename, O_RDWR | O_CREAT | O_EXCL, mode)) < 0) {
116
133
        if (errno == EEXIST) {
117
 
          free(name);
 
134
          if(name != filename)
 
135
            free(name);
 
136
          free(filename);
118
137
          continue;
119
138
        }
120
139
        syserror("open");
121
140
      }
122
 
      if (sfx) {
123
 
        char *namesfx;
124
 
        if (!(namesfx = malloc(strlen(name) + strlen(sfx) + 1)))
125
 
          syserror("malloc");
126
 
        strcpy(namesfx, name);
127
 
        strcat(namesfx, sfx);
128
 
        if (link(name, namesfx) < 0) {
129
 
          if (errno == EEXIST) {
130
 
            if (unlink(name) < 0)
131
 
              syserror("unlink");
132
 
            free(name);
133
 
            free(namesfx);
134
 
            continue;
135
 
          }
136
 
          syserror("link");
137
 
        }
138
 
        if (unlink(name) < 0)
139
 
          syserror("unlink");
140
 
        free(name);
141
 
        name = namesfx;
142
 
      }
143
141
      break;
144
142
    }
145
143
  }
146
144
  
147
145
  if (close(fd))
148
146
    syserror("close");
149
 
  puts(name);
 
147
  puts(filename);
 
148
  if(name != filename)
 
149
    free(name);
 
150
  free(filename);
150
151
  exit(0);
151
152
}