~mmach/netext73/meson

« back to all changes in this revision

Viewing changes to test cases/common/163 external program shebang parsing/main.c

  • Committer: mmach
  • Date: 2020-04-09 10:35:04 UTC
  • Revision ID: netbit73@gmail.com-20200409103504-wkxbh6yl5fzlueas
0.53.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <fcntl.h>
3
 
#include <errno.h>
4
 
#include <string.h>
5
 
#include <stdlib.h>
6
 
#include <sys/types.h>
7
 
 
8
 
#ifdef _WIN32
9
 
 #include <io.h>
10
 
 #include <windows.h>
11
 
#else
12
 
 #include <unistd.h>
13
 
#endif
14
 
 
15
 
/* Who cares about stack sizes in test programs anyway */
16
 
#define LINE_LENGTH 4096
17
 
 
18
 
static int
19
 
intrp_copyfile (char * src, char * dest)
20
 
{
21
 
#ifdef _WIN32
22
 
  if (!CopyFile (src, dest, FALSE))
23
 
    return 1;
24
 
  return 0;
25
 
#else
26
 
  return execlp ("cp", "copyfile", src, dest, NULL);
27
 
#endif
28
 
}
29
 
 
30
 
static void
31
 
parser_get_line (FILE * f, char line[LINE_LENGTH])
32
 
{
33
 
  if (!fgets (line, LINE_LENGTH, f))
34
 
    fprintf (stderr, "%s\n", strerror (errno));
35
 
}
36
 
 
37
 
int
38
 
main (int argc, char * argv[])
39
 
{
40
 
  FILE *f = NULL;
41
 
  char line[LINE_LENGTH];
42
 
 
43
 
  if (argc != 4) {
44
 
    fprintf (stderr, "Invalid number of arguments: %i\n", argc);
45
 
    goto err;
46
 
  }
47
 
 
48
 
  if ((f = fopen (argv[1], "r")) == NULL) {
49
 
    fprintf (stderr, "%s\n", strerror (errno));
50
 
    goto err;
51
 
  }
52
 
 
53
 
  parser_get_line (f, line);
54
 
 
55
 
  if (!line || line[0] != '#' || line[1] != '!') {
56
 
    fprintf (stderr, "Invalid script\n");
57
 
    goto err;
58
 
  }
59
 
 
60
 
  parser_get_line (f, line);
61
 
 
62
 
  if (!line || strncmp (line, "copy", 4) != 0) {
63
 
    fprintf (stderr, "Syntax error: %s\n", line);
64
 
    goto err;
65
 
  }
66
 
 
67
 
  return intrp_copyfile (argv[2], argv[3]);
68
 
 
69
 
err:
70
 
  fclose (f);
71
 
  return 1;
72
 
}