Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
into.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * into.c - Put output into a file without destroying it.
20  *
21  * Author: Spencer W. Thomas
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Mon Aug 22 1983
25  * Copyright (c) 1983 Spencer W. Thomas
26  */
27 
28 #include "rle_config.h"
29 #ifndef _XOPEN_SOURCE
30 #define _XOPEN_SOURCE /* For mkstemp */
31 #endif /* !_XOPEN_SOURCE */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/param.h> /* for MAXPATHLEN */
39 #include <sys/stat.h>
40 
41 #ifndef MAXPATHLEN
42 # define MAXPATHLEN BUFSIZ
43 #endif
44 
45 static char temp[] = "intoXXXXXX";
46 static char buf[MAXPATHLEN+1];
47 short forceflg; /* overwrite an unwritable file? */
48 
49 #include <string.h>
50 
51 void
53 int argc;
54 char **argv;
55 {
56  char *cp;
57  int c;
58  FILE * outf;
59  char iobuf[BUFSIZ];
60  int size;
61 
62  /* Don't allow files named "-f" in order to catch common error */
63  if (argc >= 2 && !strcmp(argv[1], "-f"))
64  {
65  forceflg++;
66  argc--, argv++;
67  }
68  if (argc != 2)
69  {
70  fprintf(stderr, "Usage: into [ -f ] file\n");
71  exit(1);
72  }
73  if (!forceflg && access(argv[1], 2) < 0 && errno != ENOENT)
74  {
75  fprintf(stderr, "into: ");
76  perror(argv[1]);
77  exit(1);
78  }
79 
80  if ( (cp = rindex( argv[1], '/' )) != NULL )
81  {
82  c = *++cp;
83  *cp = 0;
84  strcpy( buf, argv[1] );
85  *cp = c;
86  strcat( buf, temp );
87  }
88  else
89  strcpy( buf, temp );
90 
91  mkstemp( buf );
92 
93  if ( (outf = fopen( buf, "w" )) == NULL )
94  {
95  perror(buf);
96  exit(1);
97  }
98 
99  while ( (size = fread(iobuf, 1, sizeof iobuf, stdin)) != 0)
100  fwrite(iobuf, 1, size, outf);
101 
102  if ( !forceflg && ftell(outf) == 0L )
103  {
104  fprintf( stderr, "into: empty output, \"%s\" not modified\n", argv[1]);
105  unlink( buf );
106  exit(1);
107  }
108  fflush(outf);
109  if (ferror(outf))
110  {
111  fprintf(stderr, "into: %s, \"%s\" not modified\n",
112  strerror(errno), argv[1]);
113  unlink(buf);
114  exit(1);
115  }
116  fclose( outf );
117 
118  if ( rename( buf, argv[1] ) < 0 )
119  {
120  fprintf(stderr, "into: rename(%s, %s): ", buf, argv[1]);
121  perror("");
122  }
123  exit( 0 );
124 }
125 
126 #ifdef NEED_RENAME
127 rename( file1, file2 )
128 char *file1, *file2;
129 {
130  struct stat st;
131 
132  if ( stat(file2, &st) >= 0 && unlink(file2) < 0 )
133  return -1;
134  if ( link(file1, file2) < 0 )
135  return -1;
136  return unlink( file1 );
137 }
138 #endif
void main(int argc, char **argv)
Definition: aliastorle.c:121
#define rindex
Definition: rle_config.h:97
short forceflg
Definition: into.c:47
static char temp[]
Definition: into.c:45
static char buf[4096 +1]
Definition: into.c:46