glsl/apps: Include missing header, properly escape format strings.
[mesa.git] / src / glsl / apps / purify.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "../pp/sl_pp_public.h"
32
33
34 int
35 main(int argc,
36 char *argv[])
37 {
38 FILE *in;
39 long size;
40 char *inbuf;
41 struct sl_pp_purify_options options;
42 char *outbuf;
43 FILE *out;
44
45 if (argc != 3) {
46 return 1;
47 }
48
49 in = fopen(argv[1], "rb");
50 if (!in) {
51 return 1;
52 }
53
54 fseek(in, 0, SEEK_END);
55 size = ftell(in);
56 fseek(in, 0, SEEK_SET);
57
58 out = fopen(argv[2], "wb");
59 if (!out) {
60 fclose(in);
61 return 1;
62 }
63
64 inbuf = malloc(size + 1);
65 if (!inbuf) {
66 fprintf(out, "$OOMERROR\n");
67
68 fclose(out);
69 fclose(in);
70 return 1;
71 }
72
73 if (fread(inbuf, 1, size, in) != size) {
74 fprintf(out, "$READERROR\n");
75
76 free(inbuf);
77 fclose(out);
78 fclose(in);
79 return 1;
80 }
81 inbuf[size] = '\0';
82
83 fclose(in);
84
85 memset(&options, 0, sizeof(options));
86
87 if (sl_pp_purify(inbuf, &options, &outbuf)) {
88 fprintf(out, "$PURIFYERROR\n");
89
90 free(inbuf);
91 fclose(out);
92 return 1;
93 }
94
95 free(inbuf);
96
97 fwrite(outbuf, 1, strlen(outbuf), out);
98
99 free(outbuf);
100 fclose(out);
101
102 return 0;
103 }