File: unshar.c

package info (click to toggle)
sharutils 1:4.15.2-4
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 10,936 kB
  • sloc: ansic: 53,545; sh: 6,933; makefile: 726; yacc: 291; sed: 16
file content (462 lines) | stat: -rw-r--r-- 11,229 bytes parent folder | download | duplicates (2)
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

/* Handle so called 'shell archives'.
   Copyright (C) 1994-2015 Free Software Foundation, Inc.

   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; if not, see:  http://www.gnu.org/licenses.
*/

/* Unpackage one or more shell archive files.  The 'unshar' program is a
   filter which removes the front part of a file and passes the rest to
   the 'sh' command.  It understands phrases like "cut here", and also
   knows about shell comment characters and the Unix commands 'echo',
   'cat', and 'sed'.  */

#define UNSHAR_C 1
#include "unshar-opts.h"

#include <sys/stat.h>

#include <ctype.h>
#include <fcntl.h>

#include "xgetcwd.h"

/*
 * FIXME: actually configure this stuff:
 */
#if defined(_SC_PAGESIZE)
# define GET_PAGE_SIZE  sysconf (_SC_PAGESIZE)
#elif defined(_SC_PAGE_SIZE)
# define GET_PAGE_SIZE  sysconf (_SC_PAGE_SIZE)
#elif defined(HAVE_GETPAGESIZE)
# define GET_PAGE_SIZE  getpagesize()
#else
# define GET_PAGE_SIZE  8192
#endif

#ifndef HAVE_LOCALE_H
# define setlocale(Category, Locale)
#endif

#define EOL '\n'
static inline unsigned char to_uchar (char ch) { return ch; }

/** amount of data to read and write at once */
size_t       rw_base_size = 0;

/** a buffer of that size */
char *       rw_buffer    = NULL;

/**
 * see if the line looks like C program code.
 * Check for start of comment ("//" or "/*") and directives.
 *
 * @param[in] buf buffer containing current input line
 * @returns true or false
 */
static bool
looks_like_c_code (char const * buf)
{
  static char const * const directives[] = {
    "include", "define", "ifdef", "ifndef", "if",
    "pragma", "undef", "elif", "error", "line", NULL };

  while (isspace (to_uchar (*buf)))  buf++;
  switch (*(buf++))
    {
    case '#':
      {
        int ix = 0;
        while (isspace ((int) *buf))  buf++;
        for (;;) {
          char const * dir = directives[ix++];
          size_t ln;
          if (dir == NULL)
            return false;
          ln = strlen (dir);
          if ((strncmp (buf, dir, ln) == 0) && isspace ((int)buf[ln]))
            return true;
        }
      }

    case '/':
      return ((*buf == '*') || (*buf == '/'));

    default: return false;
    }
}

/**
 * see if the line looks like shell program code.
 * Check for start of comment ("#") and a few commands (":" and others).
 *
 * @param[in] buf buffer containing current input line
 * @returns true or false
 */
static bool
looks_like_shell_code (char const * buf)
{
  while (isspace ((int) *buf))  buf++;
  switch (*buf)
    {
    case '#': case ':':
      return true;
    default:
      if (islower ((int)*buf))
        break;
      return false;
    }

  {
    static char const * const cmn_cmds[] = {
      "echo", "sed", "cat", "if", NULL };

    int ix = 0;
    for (;;) {
      char const * cmd = cmn_cmds[ix++];
      size_t ln;
      if (cmd == NULL)
        return false;
      ln = strlen (cmd);
      if ((strncmp (buf, cmd, ln) == 0) && isspace ((int)buf[ln]))
        return true;
    }
  }
}

/**
 * see if the line contains a cut mark.
 * "cut" can be spelled either that way or as "tear" and is either
 * all lower case or all upper case.  That word must be followed by either
 * another copy of that word or the word "here", and both must be in the
 * same case.  (All upper or all lower.)
 *
 * @param[in] buf buffer containing current input line
 * @returns true or false
 */
static bool
this_is_cut_line (char const * buf)
{
  static char const * const kwds[] = {
    "cut", "CUT", "tear", "TEAR" };
  static char const * const here_here[] = {
    "here", "HERE" };
  int ix = 0;

  for (;;)
    {
      char const * kw  = kwds[ix];
      char const * fnd = strstr (buf, kw);
      if (fnd != NULL)
        {
          buf = fnd + strlen (kw);
          break;
        }

      if (++ix >= 4)
        return false;
    }

  {
    char const * fnd = strstr (buf, kwds[ix]);
    if (fnd != NULL)
      return true;
    fnd = strstr (buf, here_here[ix & 1]);
    return (fnd != NULL);
  }
}

/**
 * Check the line following a cut line.  The next non-blank line
 * must start with a command or a shell comment character ('#').
 *
 * @param[in]  name     input file name (for error message)
 * @param[in]  file     file pointer of input
 *
 * @returns true if valid, false otherwise
 */
static bool
next_line_is_valid (char const * name, FILE * file)
{
  off_t position;

  /* Read next line after "cut here", skipping blank lines.  */

  for (;;)
    {
      char * buf;
      position = ftello (file);
      buf = fgets (rw_buffer, rw_base_size, file);
      if (buf == NULL)
        {
          error (0, 0, _("Found no shell commands after cut line in %s"),
                 name);
          return false;
        }

      while (isspace ((int)*buf))  buf++;
      if (*buf != '\0')
        break;
    }

  /* Win if line starts with a comment character of lower case letter.  */
  if (islower ((int)*rw_buffer) || (*rw_buffer == '#') || (*rw_buffer == ':'))
    {
      fseeko (file, position, SEEK_SET);
      return true;
    }

  /* Cut here message lied to us.  */

  error (0, 0, _("%s is probably not a shell archive"), name);
  error (0, 0, _("The 'cut line' was followed by: %s"), rw_buffer);
  return false;
}

/**
 * Associated with a given file NAME, position FILE at the start of the
 * shell command portion of a shell archive file.  Scan file from position
 * START.
 */
static bool
find_archive (char const * name, FILE * file, off_t start)
{
  fseeko (file, start, SEEK_SET);

  while (1)
    {
      /* Record position of the start of this line.  */
      off_t position = ftello (file);

      /* Read next line, fail if no more and no previous process.  */
      if (!fgets (rw_buffer, rw_base_size, file))
	{
	  if (!start)
	    error (0, 0, _("Found no shell commands in %s"), name);
	  return false;
	}

      /*
       * Bail out if we see C preprocessor commands or C comments.
       */
      if (looks_like_c_code (rw_buffer))
	{
	  error (0, 0, _("%s looks like raw C code, not a shell archive"),
		 name);
	  return false;
	}

      /* Does this line start with a shell command or comment.  */

      if (looks_like_shell_code (rw_buffer))
	{
	  fseeko (file, position, SEEK_SET);
	  return true;
	}

      /*
       * Does this line say "Cut here"?
       */
      if (this_is_cut_line (rw_buffer))
        return next_line_is_valid (name, file);
    }
}

#ifdef __MINGW32__

static char const *
get_env_tmpdir(void)
{
  char const * res = getenv ("TMPDIR");
  if (res) return res;
  res = getenv ("TEMP");
  if (res) return res;
  res = getenv ("TMP");
  return res ? res : ".";
}

#else

static char const *
get_env_tmpdir(void)
{
  char const * res = getenv ("TMPDIR");
  return res ? res : "/tmp";
}
#endif

/**
 * load a file that does not support ftell/seek.
 * Create a temporary file and suck up all the file data, writing it out
 * to the temporary file.  Return a file pointer to this temporary file,
 * and a pointer to the allocated string containing the file name.
 * The file must be removed and the name pointer deallocated.
 *
 * @param[out] tmp_fname    temporary file name
 * @param[in]  infp         input data file pointer (usually stdin)
 *
 * @returns a file pointer to the temporary file
 */
static FILE *
load_file (char const ** tmp_fname, FILE * infp)
{
  static char const z_tmpfile[] = "unsh.XXXXXX";
  char * pz_fname;
  FILE * outfp;

  {
    size_t name_size;
    char const * pz_tmp = get_env_tmpdir();

    name_size = strlen (pz_tmp) + sizeof (z_tmpfile) + 1;
    *tmp_fname = pz_fname = malloc (name_size);

    if (pz_fname == NULL)
      fserr (UNSHAR_EXIT_NOMEM, "malloc", _("file name buffer"));
    
    sprintf (pz_fname, "%s/%s", pz_tmp, z_tmpfile);
  }

  {
    int fd = mkstemp (pz_fname);
    if (fd < 0)
      fserr (UNSHAR_EXIT_CANNOT_CREATE, "mkstemp", z_tmpfile);
    
    outfp = fdopen (fd, "wb+");
  }

  if (outfp == NULL)
    fserr (UNSHAR_EXIT_CANNOT_CREATE, "fdopen", pz_fname);

  for (;;)
    {
      size_t size_read = fread (rw_buffer, 1, rw_base_size, infp);
      if (size_read == 0)
        break;
      fwrite (rw_buffer, size_read, 1, outfp);
    }

  rewind (outfp);

  return outfp;
}

/**
 * Unarchive a shar file provided on file NAME.  The file itself is
 * provided on the already opened FILE pointer.  If the file position
 * cannot be determined, we suck up all the data and use a temporary.
 */
int
unshar_file (const char * name, FILE * file)
{
  char const * tmp_fname = NULL;
  off_t curr_pos;

#ifdef __MINGW32__
  _setmode (fileno (file), _O_BINARY);
#endif

  curr_pos = ftello (file);
  if (curr_pos < 0)
    {
      file = load_file (&tmp_fname, file);
      curr_pos = ftello (file);
      if (curr_pos < 0)
        fserr (UNSHAR_EXIT_CANNOT_CREATE, "ftell", tmp_fname);
    }
      
  while (find_archive (name, file, curr_pos))
    {
      char const * cmd = HAVE_OPT(OVERWRITE) ? "sh -s - -c" : "sh";
      FILE * shell_fp = popen (cmd, "w");

      if (shell_fp == NULL)
	fserr (UNSHAR_EXIT_POPEN_PROBLEM, "popen", cmd);
      if (HAVE_OPT(DIRECTORY))
        fprintf (shell_fp, "cd %s >/dev/null || exit 1\n", OPT_ARG(DIRECTORY));
      printf ("%s:\n", name);
      if (! HAVE_OPT(SPLIT_AT))
	{
          for (;;)
            {
              size_t len = fread (rw_buffer, 1, rw_base_size, file);
              if (len == 0)
                break;
              fwrite (rw_buffer, 1, len, shell_fp);
            }

	  pclose (shell_fp);
	  break;
	}
      else
	{
          char * text_in;

	  while (text_in = fgets (rw_buffer, rw_base_size, file),
		 text_in != NULL)
	    {
	      fputs (rw_buffer, shell_fp);
	      if (!strncmp (OPT_ARG(SPLIT_AT), rw_buffer, separator_str_len))
		break;
	    }
	  pclose (shell_fp);

	  if (text_in == NULL)
	    break;

          curr_pos = ftello (file);
	}
    }

  if (tmp_fname != NULL)
    {
      unlink (tmp_fname);
      free ((void *)tmp_fname);
    }

  return UNSHAR_EXIT_SUCCESS;
}

/**
 * unshar initialization.  Do all the initializations necessary to be ready
 * to start processing files.  I18N stuff, file descriptors for source and
 * destination files, and allocation of I/O buffers.
 *
 * This routine works or does not return.
 */
void
init_unshar (void)
{
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);
#ifdef __MSDOS__
  setbuf (stdout, NULL);
  setbuf (stderr, NULL);
#endif

  rw_base_size = GET_PAGE_SIZE;
  rw_buffer    = malloc (rw_base_size);
  if (rw_buffer == NULL)
    fserr (UNSHAR_EXIT_NOMEM, "malloc", _("read/write buffer"));
}

/*
 * Local Variables:
 * mode: C
 * c-file-style: "gnu"
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 * end of unshar.c */