Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Macros | Functions | Variables
into.c File Reference
#include "rle_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <string.h>
Include dependency graph for into.c:

Go to the source code of this file.

Macros

#define _XOPEN_SOURCE   /* For mkstemp */
 

Functions

void main (int argc, char **argv)
 

Variables

static char temp [] = "intoXXXXXX"
 
static char buf [4096 +1]
 
short forceflg
 

Macro Definition Documentation

#define _XOPEN_SOURCE   /* For mkstemp */

Definition at line 30 of file into.c.

Function Documentation

void main ( int  argc,
char **  argv 
)

Definition at line 52 of file into.c.

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 }
#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
gpr_ $offset_t size
Definition: getap.c:96

Variable Documentation

char buf[ 4096 +1]
static

Definition at line 46 of file into.c.

short forceflg

Definition at line 47 of file into.c.

char temp[] = "intoXXXXXX"
static

Definition at line 45 of file into.c.