| 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", "cp", 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 |
} |