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
|
/*
* routine.c
*
* general use routines
* Copyright (c) 1988-1993 Miguel Santana
* Copyright (c) 1995-1999 Akim Demaille, Miguel Santana
* $Id: routines.c,v 1.1.1.1.2.1 2007/12/29 01:58:23 mhatta Exp $
*/
/*
* This file is part of a2ps.
*
* 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 3, 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; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "a2ps.h"
#include "routines.h"
#include "stpncpy.h"
#include "message.h"
#include "quotearg.h"
/*
* Convert a list of string of valid chars to an yes/no array
*/
void
string_to_array (uchar arr[256], const uchar * string)
{
int c;
for (c = 0 ; c < 256 ; c++)
arr [c] = false;
for ( /* nothing */ ; *string ; string ++)
arr [*string] = true;
}
/*
* Concatenation of a char. No malloc is done.
*/
void
ustrccat (uchar * string, uchar c)
{
int len = strlen((char *)string);
*(string+len) = c;
*(string+len+1) = '\0';
}
/*
* return true iff there are no upper case chars
*/
int
is_strlower (const uchar * string)
{
for (/* skip */; *string != '\0'; string++)
if (isupper(*string))
return false;
return true;
}
/* Copy the LEN first characters of SRC into DST in lower case.
DST[LEN] is set to \0. */
static inline uchar *
_strncpylc (uchar *dst, const uchar *src, size_t len)
{
size_t i;
for (i = 0 ; i < len ; i++)
dst[i] = tolower (src[i]);
dst[len] = '\0';
return dst;
}
uchar *
strnlower (uchar *string, size_t len)
{
return _strncpylc (string, string, len);
}
uchar *
strlower (uchar *string)
{
return _strncpylc (string, string, strlen (string));
}
uchar *
strcpylc (uchar *dst, const uchar *src)
{
return _strncpylc (dst, src, strlen (src));
}
/*
* Count the number of occurrence of C in S
*/
int
strcnt (uchar *s, uchar c)
{
int res;
for (res = 0 ; *s ; s++)
if (*s == c)
res++;
return res;
}
/*
* Extract a substring for START, of LENGTH, making sure to
* set the trailing '\0' (return pos of \0)
*/
char *
strsub (char * dest, const char * string, int start, int length)
{
char * end = stpncpy (dest, string + start, length);
*end = '\0';
return end;
}
/*
* fopen, but exits on failure
*/
static inline FILE *
_xfopen (const char * filename, const char * rights, const char * format)
{
FILE * res;
message (msg_file,
(stderr, "%s-fopen (%s)\n", rights, quotearg (filename)));
res = fopen (filename, rights);
if (!res)
error (1, errno, format, quotearg (filename));
return res;
}
FILE *
xfopen (const char * filename, const char * rights, const char * format)
{
return _xfopen (filename, rights, format);
}
FILE *
xrfopen (const char * filename)
{
return _xfopen (filename, "r", _("cannot open file `%s'"));
}
FILE *
xwfopen (const char * filename)
{
return _xfopen (filename, "w", _("cannot create file `%s'"));
}
/*
* Like popen, but exist upon failure
*/
static inline FILE *
_xpopen (const char * filename, const char * rights, const char * format)
{
FILE * res;
message (msg_file,
(stderr, "%s-popen (%s)\n", rights, filename));
res = popen (filename, rights);
if (!res)
error (1, errno, format, quotearg (filename));
return res;
}
FILE *
xpopen (const char * filename, const char * rights, const char * format)
{
return _xpopen (filename, rights, format);
}
FILE *
xrpopen (const char * filename)
{
return _xpopen (filename, "r", _("cannot open a pipe on `%s'"));
}
FILE *
xwpopen (const char * filename)
{
return _xpopen (filename, "w", _("cannot open a pipe on `%s'"));
}
/*
* Copy the content of IN into OUT
*/
static inline void
_streams_copy (FILE * in, FILE * out)
{
size_t read_length;
char buf [BUFSIZ];
while ((read_length = fread (buf, sizeof (char), sizeof (buf), in)))
fwrite (buf, sizeof (char), read_length, out);
}
void
streams_copy (FILE * in, FILE * out)
{
_streams_copy (in, out);
}
/*
* Dump the content of the file FILENAME onto STREAM.
* Used when honoring a subcontract.
*/
void
stream_dump (FILE * stream, const char * filename)
{
FILE * fp;
message (msg_tool | msg_file, (stderr, "Dumping file `%s'\n", filename));
fp = xrfopen (filename);
_streams_copy (fp, stream);
fclose (fp);
}
/*
* Unlink the file FILENAME.
*/
void
unlink2 (PARAM_UNUSED void * dummy, const char * filename)
{
message (msg_tool | msg_file, (stderr, "Unlinking file `%s'\n", filename));
/* Don't complain if you can't unlink. Who cares of a tmp file? */
unlink (filename);
}
/*
* Securely generate a temp file, and make sure it gets
* deleted upon exit.
*/
static char ** tempfiles;
static unsigned ntempfiles;
static void
cleanup_tempfiles()
{
while (ntempfiles--)
unlink(tempfiles[ntempfiles]);
}
char *
safe_tempnam(const char *pfx)
{
char *dirname, *filename;
int fd;
if (!(dirname = getenv("TMPDIR")))
dirname = "/tmp";
tempfiles = (char **) realloc(tempfiles,
(ntempfiles+1) * sizeof(char *));
if (tempfiles == NULL)
return NULL;
filename = malloc(strlen(dirname) + strlen(pfx) + sizeof("/XXXXXX"));
if (!filename)
return NULL;
sprintf(filename, "%s/%sXXXXXX", dirname, pfx);
if ((fd = mkstemp(filename)) < 0) {
free(filename);
return NULL;
}
close(fd);
if (ntempfiles == 0)
atexit(cleanup_tempfiles);
tempfiles[ntempfiles++] = filename;
return filename;
}
|