-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk1.c
750 lines (678 loc) · 19.8 KB
/
disk1.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/****************************************************************
** **
** Copyright (c) 1996 - 2001 Caldera International, Inc. **
** All Rights Reserved. **
** **
** This program is free software; you can redistribute it **
** and/or modify it under the terms of the GNU General Public **
** License as published by the Free Software Foundation; **
** either version 2 of the License, or (at your option) any **
** later version. **
** **
** This program is distributed in the hope that it will be **
** useful, but WITHOUT ANY WARRANTY; without even the implied **
** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR **
** PURPOSE. See the GNU General Public License for more **
** details. **
** **
** You should have received a copy of the GNU General Public **
** License along with this program; if not, write to the Free **
** Software Foundation, Inc., 59 Temple Place, Suite 330, **
** Boston, MA 02111-1307 USA **
** **
****************************************************************/
#define _POSIX_SOURCE 1
#define _XOPEN_SOURCE 1 /* so O_SYNC can be used */
#define _INCLUDE_XOPEN_SOURCE 1 /* so O_SYNC can be used on HP */
#define _M_XOUT 1 /* so O_SYNC can be used on Acer/Altos */
#include <stdio.h> /* enable scanf(), etc. */
#include <stdlib.h> /* enable rand(), etc. */
#include <unistd.h> /* enable write(), etc. */
#include <sys/types.h> /* required for creat() */
#include <sys/stat.h> /* required for creat() */
#include <fcntl.h> /* required for creat() */
#include <string.h> /* for strchr() */
#include "suite.h" /* our includes */
static int
sync_disk_rw(),
sync_disk_wrt(),
sync_disk_cp(),
sync_disk_update(),
disk_rr(),
disk_rw(),
disk_rd(),
disk_wrt(),
disk_cp();
void aim_mktemp();
source_file *
disk1_c()
{
static source_file s = { " @(#) disk1.c:1.20 2/10/94 13:51:47", /* SCCS info */
__FILE__, __DATE__, __TIME__
};
if (NBUFSIZE != 1024) { /* enforce known block size */
fprintf(stderr, "NBUFSIZE changed to %d\n", NBUFSIZE);
exit(1);
}
register_test("disk_rr", "DISKS", disk_rr, WAITING_FOR_DISK_COUNT,
"Random Disk Reads (K)");
register_test("disk_rw", "DISKS", disk_rw, WAITING_FOR_DISK_COUNT,
"Random Disk Writes (K)");
register_test("disk_rd", "DISKS", disk_rd, WAITING_FOR_DISK_COUNT,
"Sequential Disk Reads (K)");
register_test("disk_wrt", "DISKS", disk_wrt, WAITING_FOR_DISK_COUNT,
"Sequential Disk Writes (K)");
register_test("disk_cp", "DISKS", disk_cp, WAITING_FOR_DISK_COUNT,
"Disk Copies (K)");
register_test("sync_disk_rw", "DISKS", sync_disk_rw,
WAITING_FOR_DISK_COUNT, "Sync Random Disk Writes (K)");
register_test("sync_disk_wrt", "DISKS", sync_disk_wrt,
WAITING_FOR_DISK_COUNT,
"Sync Sequential Disk Writes (K)");
register_test("sync_disk_cp", "DISKS", sync_disk_cp,
WAITING_FOR_DISK_COUNT, "Sync Disk Copies (K)");
register_test("sync_disk_update", "DISKS", sync_disk_update,
WAITING_FOR_DISK_COUNT, "Sync Disk Updates (K)");
return &s;
}
static char fn1[STRLEN];
static char fn2[STRLEN];
static char nbuf[NBUFSIZE]; /* 1K blocks */
/* test file size is 2^10 */
#define SHIFT 10 /* Change with block size */
/*
* "Semi"-Random disk read
* TVL 11/28/89
*/
static int
disk_rr(char *argv,
Result * res)
{
int
i, fd, n;
long
sk;
sk = 0l;
n = disk_iteration_count; /* user specified size */
if (*argv)
sprintf(fn2, "%s/%s", argv, TMPFILE2);
else
sprintf(fn2, "%s", TMPFILE2);
aim_mktemp(fn2); /* generate new file name */
if ((fd = creat(fn2, (S_IRWXU | S_IRWXG | S_IRWXO))) < 0) {
fprintf(stderr, "disk_rr : cannot create %s\n", fn2);
perror(__FILE__);
return (-1);
}
/*
* We do this to "encourage" the system to read from disk
* instead of the buffer cache.
* 12/12/89 TVL
*/
while (n--) {
if (write(fd, nbuf, sizeof nbuf) != sizeof nbuf) {
perror("disk_rr()");
fprintf(stderr, "disk_rr : cannot write %s\n", fn2);
close(fd);
unlink(fn2);
return (-1);
}
}
close(fd);
if ((fd = open(fn2, O_RDONLY)) < 0) {
fprintf(stderr, "disk_rr : cannot open %s\n", fn2);
perror(__FILE__);
return (-1);
}
unlink(fn2);
/********** pseudo random read *************/
for (i = 0; i < disk_iteration_count; i++) {
/*
* get random block to read, making sure not to read past end of file
*/
sk = aim_rand() % ((disk_iteration_count * (long)sizeof (nbuf)) >> SHIFT); /* rand() % (filesize/blocksize) */
/*
* sk specifies a specific block, multiply by blocksize to get offset in bytes
*/
sk <<= SHIFT;
if (lseek(fd, sk, 0) == -1) {
perror("disk_rr()");
fprintf(stderr, "disk_rr : can't lseek %s\n", fn2);
/* unlink(fn2); */
close(fd);
return (-1);
}
if ((n = read(fd, nbuf, sizeof nbuf)) != sizeof nbuf) {
perror("disk_rr()");
fprintf(stderr, "disk_rr : can't read %s\n", fn2);
/* unlink(fn2); */
close(fd);
return (-1);
}
}
/*
* unlink(fn2);
*/
close(fd);
res->d = n;
return (0);
}
/*
* "Semi"-Random disk write
*/
static int
disk_rw(char *argv,
Result * res)
{
int
i, fd, n;
long
sk;
sk = 0l;
n = disk_iteration_count; /* user specified size */
if (*argv)
sprintf(fn2, "%s/%s", argv, TMPFILE2);
else
sprintf(fn2, "%s", TMPFILE2);
aim_mktemp(fn2);
if ((fd = creat(fn2, (S_IRWXU | S_IRWXG | S_IRWXO))) < 0) {
fprintf(stderr, "disk_rw : cannot create %s\n", fn2);
perror(__FILE__);
return (-1);
}
while (n--) {
if (write(fd, nbuf, sizeof nbuf) != sizeof nbuf) {
perror("disk_rw()");
fprintf(stderr, "disk_rw : cannot write %s\n", fn2);
close(fd);
unlink(fn2);
return (-1);
}
}
close(fd);
if ((fd = open(fn2, O_WRONLY)) < 0) {
fprintf(stderr, "disk_rw : cannot open %s\n", fn2);
perror(__FILE__);
return (-1);
}
unlink(fn2);
/********** pseudo random write *************/
for (i = 0; i < disk_iteration_count; i++) {
/*
* get random block to read, making sure not to read past end of file
*/
sk = aim_rand() % ((disk_iteration_count * (long)sizeof (nbuf)) >> SHIFT); /* rand() % (filesize/blocksize) */
/*
* sk specifies a specific block, multiply by blocksize to get offset in bytes
*/
sk <<= SHIFT;
if (lseek(fd, sk, 0) == -1) {
perror("disk_rw()");
fprintf(stderr, "disk_rw : can't lseek %s\n", fn2);
/* unlink(fn2); */
close(fd);
return (-1);
}
if ((n = write(fd, nbuf, sizeof nbuf)) != sizeof nbuf) {
perror("disk_rw()");
fprintf(stderr, "disk_rw : can't read %s\n", fn2);
/* unlink(fn2); */
close(fd);
return (-1);
}
}
/* unlink(fn2); */
close(fd);
res->d = n;
return (0);
}
static int
sync_disk_rw(char *argv,
Result * res)
{
int
i, fd, blocks, n;
long
sk;
sk = 0l;
n = blocks = disk_iteration_count / 2; /* divide by 2 so as not to pound disk */
if (*argv)
sprintf(fn2, "%s/%s", argv, TMPFILE2);
else
sprintf(fn2, "%s", TMPFILE2);
aim_mktemp(fn2);
if ((fd = open(fn2, (O_WRONLY | O_CREAT | O_TRUNC), /* standard CREAT mode */
(S_IRWXU | S_IRWXG | S_IRWXO))) < 0) { /* standard permissions */
fprintf(stderr, "sync_disk_rw : cannot create %s\n", fn2);
perror(__FILE__);
return (-1);
}
while (n--) {
if (write(fd, nbuf, sizeof nbuf) != sizeof nbuf) {
perror("disk_rw()");
fprintf(stderr, "sync_disk_rw : cannot write %s\n",
fn2);
close(fd);
unlink(fn2);
return (-1);
}
}
close(fd);
if ((fd = open(fn2, O_WRONLY | O_SYNC)) < 0) { /* open it again synchronously */
fprintf(stderr, "sync_disk_rw : cannot open %s\n", fn2);
perror(__FILE__);
return (-1);
}
/* unlink(fn2); *//*
* unlink moved after write 10/17/95
*/
/********** pseudo random write *************/
for (i = 0; i < blocks; i++) { /* get random block to read,
* making sure not to read past end of file */
sk = aim_rand() % ((blocks * (long)sizeof (nbuf)) >> SHIFT); /* rand() % (filesize/blocksize) */
sk <<= SHIFT; /* sk specifies a specific block,
* multiply by blocksize to get offset in bytes */
if (lseek(fd, sk, 0) == -1) {
perror("sync_disk_rw()");
fprintf(stderr, "sync_disk_rw : can't lseek %s\n",
fn2);
unlink(fn2);
close(fd);
return (-1);
}
if ((n = write(fd, nbuf, sizeof nbuf)) != sizeof nbuf) {
perror("sync_disk_rw()");
fprintf(stderr,
"sync_disk_rw : can't write %d bytes to %s\n",
sizeof nbuf, fn2);
unlink(fn2);
close(fd);
return (-1);
}
}
unlink(fn2);
close(fd);
res->d = n;
return (0);
}
static int
disk_rd(char *argv,
Result * res)
{
int
i, fd;
if (*argv)
sprintf(fn1, "%s/%s", argv, TMPFILE1);
else
sprintf(fn1, "%s", TMPFILE1);
fd = open(fn1, O_RDONLY);
if (fd < 0) { /* */
fprintf(stderr, "disk_rd : cannot open %s\n", fn1);
perror(__FILE__);
return (-1);
}
/*
* forward sequential read only
*/
if (lseek(fd, 0L, 0) < 0) {
fprintf(stderr, "disk_rd : can't lseek %s\n", fn1);
perror(__FILE__);
close(fd);
return (-1);
}
for (i = 0; i < disk_iteration_count; i++) {
if (read(fd, nbuf, sizeof nbuf) != sizeof nbuf) {
fprintf(stderr, "disk_rd : can't read %s\n", fn1);
perror(__FILE__);
close(fd);
return (-1);
}
}
close(fd);
res->d = i;
return (0);
}
static int
disk_cp(char *argv,
Result * res)
{
int
status, /* result of last system call */
n, fd, fd2;
n = disk_iteration_count; /* user specified size */
if (*argv) { /* are we passing a directory? */
sprintf(fn1, "%s/%s", argv, TMPFILE1); /* if so, build source file name */
sprintf(fn2, "%s/%s", argv, TMPFILE2); /* and destination file name */
} else { /* else build names in this directory */
sprintf(fn1, "%s", TMPFILE1); /* source file name */
sprintf(fn2, "%s", TMPFILE2); /* desination file nam */
}
aim_mktemp(fn2); /* convert into unique temporary name */
fd = open(fn1, O_RDONLY); /* open the file */
if (fd < 0) { /* open source file */
fprintf(stderr, "disk_cp (1): cannot open %s\n", fn1); /* handle error */
perror(__FILE__); /* print error */
return (-1); /* return error */
}
fd2 = creat(fn2, (S_IRWXU | S_IRWXG | S_IRWXO)); /* create the file */
if (fd2 < 0) { /* create output file */
fprintf(stderr, "disk_cp (2): cannot create %s\n", fn2); /* talk to human on error */
perror(__FILE__);
close(fd); /* close source file */
return (-1); /* return error */
}
unlink(fn2); /* make it anonymous (and work NFS harder) */
status = lseek(fd, 0L, SEEK_SET); /* move pointer to offset 0 (rewind) */
if (status < 0) { /* handle error case */
fprintf(stderr, "disk_cp (3): cannot lseek %s\n", fn1); /* talk to human */
perror(__FILE__);
/*
* unlink(fn2);
*//*
* make it anonymous (and work NFS harder)
*/
close(fd); /* close source file */
close(fd2); /* close this file */
return (-1); /* return error */
}
while (n--) { /* while not done */
status = read(fd, nbuf, sizeof nbuf); /* do the read */
if (status != sizeof nbuf) { /* return the status */
fprintf(stderr,
"disk_cp (4): cannot read %s %d (status = %d)\n",
fn1, fd, status);
perror(__FILE__);
/*
* unlink(fn2);
*//*
* make it anonymous (and work NFS harder)
*/
close(fd);
close(fd2);
return (-1);
}
status = write(fd2, nbuf, sizeof nbuf); /* do the write */
if (status != sizeof nbuf) { /* check for error */
fprintf(stderr, "disk_cp (5): cannot write %s\n", fn2);
perror(__FILE__);
/*
* unlink(fn2);
*//*
* make it anonymous (and work NFS harder)
*/
close(fd);
close(fd2);
return (-1);
}
}
/*
* unlink(fn2);
*//*
* make it anonymous (and work NFS harder)
*/
close(fd); /* close input file */
close(fd2); /* close (and delete) output file */
res->d = disk_iteration_count; /* return number */
return (0); /* show success */
}
static int
sync_disk_cp(char *argv,
Result * res)
{
int
status, /* result of last system call */
n, blocks, fd, fd2;
n = blocks = disk_iteration_count / 2; /* divide by 2 so as not to pound disk */
if (*argv) { /* are we passing a directory? */
sprintf(fn1, "%s/%s", argv, TMPFILE1); /* if so, build source file name */
sprintf(fn2, "%s/%s", argv, TMPFILE2); /* and destination file name */
} else { /* else build names in this directory */
sprintf(fn1, "%s", TMPFILE1); /* source file name */
sprintf(fn2, "%s", TMPFILE2); /* desination file nam */
}
aim_mktemp(fn2); /* convert into unique temporary name */
fd = open(fn1, O_RDONLY); /* open the file */
if (fd < 0) { /* open source file */
fprintf(stderr, "sync_disk_cp (1): cannot open %s\n", fn1); /* handle error */
perror(__FILE__); /* print error */
return (-1); /* return error */
}
fd2 = open(fn2, (O_SYNC | O_WRONLY | O_CREAT | O_TRUNC), (S_IRWXU | S_IRWXG | S_IRWXO)); /* create the file */
if (fd2 < 0) { /* create output file */
fprintf(stderr, "sync_disk_cp (2): cannot create %s\n", fn2); /* talk to human on error */
perror(__FILE__);
close(fd); /* close source file */
return (-1); /* return error */
}
/* unlink(fn2); *//*
* make it anonymous (and work NFS harder)
*/
status = lseek(fd, 0L, SEEK_SET); /* move pointer to offset 0 (rewind) */
if (status < 0) { /* handle error case */
fprintf(stderr, "sync_disk_cp (3): cannot lseek %s\n", fn1); /* talk to human */
perror(__FILE__);
close(fd); /* close source file */
close(fd2); /* close this file */
return (-1); /* return error */
}
while (n--) { /* while not done */
status = read(fd, nbuf, sizeof nbuf); /* do the read */
if (status != sizeof nbuf) { /* return the status */
fprintf(stderr,
"sync_disk_cp (4): cannot read %s %d (status = %d)\n",
fn1, fd, status);
perror(__FILE__);
unlink(fn2); /* make it anonymous (and work NFS harder) */
close(fd);
close(fd2);
return (-1);
}
status = write(fd2, nbuf, sizeof nbuf); /* do the write (SYNC) */
if (status != sizeof nbuf) { /* check for error */
fprintf(stderr, "sync_disk_cp (5): cannot write %s\n",
fn2);
perror(__FILE__);
unlink(fn2); /* make it anonymous (and work NFS harder) */
close(fd);
close(fd2);
return (-1);
}
}
unlink(fn2); /* make it anonymous (and work NFS harder) */
close(fd); /* close input file */
close(fd2); /* close (and delete) output file */
res->d = blocks; /* return number */
return (0); /* show success */
}
static int
disk_wrt(char *argv,
Result * res)
{
int
n, fd, i;
n = disk_iteration_count; /* user specified size */
i = 0;
if (*argv)
sprintf(fn2, "%s/%s", argv, TMPFILE2);
else
sprintf(fn2, "%s", TMPFILE2);
aim_mktemp(fn2);
fd = creat(fn2, (S_IRWXU | S_IRWXG | S_IRWXO));
if (fd < 0) {
fprintf(stderr, "disk_wrt : cannot create %s\n", fn2);
perror(__FILE__);
return (-1);
}
unlink(fn2);
while (n--) {
if ((i = write(fd, nbuf, sizeof nbuf)) != sizeof nbuf) {
fprintf(stderr, "disk_wrt : cannot write %s\n", fn2);
perror(__FILE__);
/* unlink(fn2); *//*
* unlink moved after write 10/17/95
*/
unlink(fn2);
close(fd);
return (-1);
}
}
/* unlink(fn2); *//*
* unlink moved after write 10/17/95
*/
close(fd);
res->d = disk_iteration_count;
return (0);
}
static int
sync_disk_wrt(char *argv,
Result * res)
{
int
n, blocks, fd, i;
n = blocks = disk_iteration_count / 2; /* divide by 2 so as not to pound disk */
i = 0;
if (*argv)
sprintf(fn2, "%s/%s", argv, TMPFILE2);
else
sprintf(fn2, "%s", TMPFILE2);
aim_mktemp(fn2);
fd = open(fn2, (O_SYNC | O_WRONLY | O_CREAT | O_TRUNC), (S_IRWXU | S_IRWXG | S_IRWXO)); /* sync creat */
if (fd < 0) {
fprintf(stderr, "sync_disk_wrt : cannot create %s\n", fn2);
perror(__FILE__);
return (-1);
}
/* unlink(fn2); *//*
* unlink moved after write 10/17/95
*/
while (n--) {
if ((i = write(fd, nbuf, sizeof nbuf)) != sizeof nbuf) {
fprintf(stderr, "sync_disk_wrt : cannot write %s\n",
fn2);
perror(__FILE__);
unlink(fn2);
close(fd);
return (-1);
}
}
unlink(fn2);
close(fd);
res->d = blocks;
return (0);
}
static int
sync_disk_update(char *argv,
Result * res)
{
int
i, fd, blocks, loop, n;
long
sk;
sk = 0l;
n = blocks = disk_iteration_count;
if (*argv)
sprintf(fn2, "%s/%s", argv, TMPFILE2);
else
sprintf(fn2, "%s", TMPFILE2);
aim_mktemp(fn2);
/*
* create a "database" file
*/
if ((fd = open(fn2, (O_WRONLY | O_CREAT | O_TRUNC), /* standard CREAT mode */
(S_IRWXU | S_IRWXG | S_IRWXO))) < 0) { /* standard permissions */
fprintf(stderr, "sync_disk_update : cannot create %s\n", fn2);
perror(__FILE__);
return (-1);
}
while (n--) {
if (write(fd, nbuf, sizeof nbuf) != sizeof nbuf) {
perror("disk_rw()");
fprintf(stderr, "sync_disk_update : cannot write %s\n",
fn2);
close(fd);
unlink(fn2);
return (-1);
}
}
close(fd);
if ((fd = open(fn2, O_RDWR | O_SYNC)) < 0) { /* open it again synchronously */
fprintf(stderr, "sync_disk_update : cannot open %s\n", fn2);
perror(__FILE__);
return (-1);
}
/* unlink(fn2); *//*
* unlink moved after update 10/17/95
*/
/*
* pseudo random read then update
*/
loop = blocks / 2; /* only touch 1/2 the blocks */
for (i = 0; i < loop; i++) { /* get random block to read,
* making sure not to read past end of file */
sk = aim_rand() % ((blocks * (long)sizeof (nbuf)) >> SHIFT); /* rand() % (filesize/blocksize) */
sk <<= SHIFT; /* sk specifies a specific block,
* multiply by blocksize to get offset in bytes */
if (lseek(fd, sk, 0) == -1) { /* look something up */
perror("sync_disk_update()");
fprintf(stderr, "sync_disk_update : can't lseek %s\n",
fn2);
unlink(fn2);
close(fd);
return (-1);
}
if ((n = read(fd, nbuf, sizeof nbuf)) != sizeof nbuf) { /* read it in */
perror("sync_disk_update()");
fprintf(stderr,
"sync_disk_update : can't read %d bytes to %s\n",
sizeof nbuf, fn2);
unlink(fn2);
close(fd);
return (-1);
}
if ((n = write(fd, nbuf, sizeof nbuf)) != sizeof nbuf) { /* update it */
perror("sync_disk_update()");
fprintf(stderr,
"sync_disk_update : can't write %d bytes to %s\n",
sizeof nbuf, fn2);
unlink(fn2);
close(fd);
return (-1);
}
}
unlink(fn2);
close(fd);
res->d = n;
return (0);
}
/*
* replaces the contents of the string pointed to by template with a unique file name.
* The string in template should look like a file name with six trailing Xs.
* aim_mktemp() will replace the Xs with a character string that can be used to create a
* unique file name.
* aim_mktemp() is a substitute for unix "mktemp()", which is not POSIX compliant.
* tmpnam() does not allow a directory to be specified.
* tempnam() is not POSIX compliant.
* changed from 3 pid char to 5 pid char 10/17/95 1.1.
*/
void
aim_mktemp(char *template)
{
static int counter = -1; /* used to fill in template */
static int pid_end; /* holds last 5 digits of pid, constant for this process */
char *Xs; /* points to string of Xs in template */
if ((template == NULL) || (*template == '\0')) { /* make sure caller passes parameter */
fprintf(stderr, "aim_mktemp : template parameter empty \n");
return;
}
Xs = template + (strlen(template) - sizeof ("XXXXXXXXXX")) + 1; /* find the X's */
if (strcmp(Xs, "XXXXXXXXXX") != 0) { /* bad parameter */
fprintf(stderr,
"aim_mktemp : template parameter needs 10 Xs \n");
return;
}
if (counter++ == -1) { /* initialize counter and pid */
pid_end = getpid() % 100000; /* use uniqueness of pid, only need 5 digits */
} else if (counter == 100000) /* reset, only need 5 digits */
counter = 0;
sprintf(Xs, "%05d%05d", pid_end, counter); /* write over XXXXXXXXXX, zero pad counter */
}