/[james]/archive/graphfree/graphfree.c
ViewVC logotype

Contents of /archive/graphfree/graphfree.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (show annotations) (download) (as text)
Mon Feb 10 22:47:44 2003 UTC (21 years, 3 months ago) by james
File MIME type: text/x-csrc
File size: 4520 byte(s)
Initial import.

1 /*
2 * graphfree.c
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "oslib/osfile.h"
9 #include "oslib/osfscontrol.h"
10 #include "oslib/osword.h"
11 #include "oslib/territory.h"
12 #include "oslib/wimp.h"
13
14
15 char **message_table;
16
17 void die(const char *message);
18 char **messages_read(const char *file_name);
19 unsigned long long used_space(const char *disc);
20 unsigned long long current_time(void);
21 void format_time(char *buffer, int size, const char *format);
22 void print_ulonglong(FILE *stream, unsigned long long x);
23 int main(void);
24
25
26 void die(const char *message)
27 {
28 os_error error;
29
30 error.errnum = 1;
31 strcpy(error.errmess, message);
32 xwimp_command_window(-1);
33 xwimp_report_error_by_category(&error, wimp_ERROR_BOX_CANCEL_ICON |
34 wimp_ERROR_BOX_GIVEN_CATEGORY | wimp_ERROR_BOX_CATEGORY_ERROR,
35 "GraphFree", "!graphfree", (osspriteop_area *) 1, NULL, NULL);
36 exit(EXIT_FAILURE);
37 }
38
39
40 char **messages_read(const char *file_name)
41 {
42 char *data;
43 char **table;
44 unsigned int size = 0, count = 0, pos = 0, i, len;
45 int c;
46 FILE *file;
47
48 file = fopen(file_name, "rb");
49 if (file == NULL) die("unable to open messages file");
50
51 while ((c = fgetc(file)) != EOF)
52 {
53 size++;
54 if (c == '\n') count++;
55 }
56 size++;
57
58 data = malloc(size);
59 if (data == NULL) die("out of memory");
60 table = calloc(count, sizeof(char *));
61 if (table == NULL) die("out of memory");
62
63 if (fseek(file, 0, SEEK_SET)) die("file seek failed");
64
65 for (i = 0; i < count; i++)
66 {
67 if (!fgets(data + pos, size - pos, file)) die("fgets failed");
68 len = strlen(data + pos);
69 data[pos + len - 1] = '\0';
70 table[i] = data + pos;
71 pos += len;
72 }
73
74 fclose(file);
75
76 return table;
77 }
78
79
80 unsigned long long used_space(const char *disc)
81 {
82 unsigned int free_lo, free_hi, size_lo, size_hi;
83 unsigned long long free, size;
84 os_error *error;
85
86 error = xosfscontrol_free_space64(disc, &free_lo, &free_hi, NULL, &size_lo, &size_hi);
87 if (error != NULL)
88 {
89 error = xosfscontrol_free_space(disc, &free_lo, NULL, &size_lo);
90 if (error != NULL)
91 {
92 char message[500];
93 sprintf(message, message_table[0], disc, error->errmess);
94 die(message);
95 }
96 free_hi = size_hi = 0;
97 }
98
99 free = ((unsigned long long) free_hi) << 32 | free_lo;
100 size = ((unsigned long long) size_hi) << 32 | size_lo;
101
102 return size - free;
103 }
104
105
106 unsigned long long current_time(void)
107 {
108 oswordreadclock_utc_block utc;
109 os_error *error;
110
111 utc.op = oswordreadclock_OP_UTC;
112 error = xoswordreadclock_utc(&utc);
113 if (error != NULL) die(error->errmess);
114
115 return (unsigned long long) utc.utc[0] |
116 (unsigned long long) utc.utc[1] << 8 |
117 (unsigned long long) utc.utc[2] << 16 |
118 (unsigned long long) utc.utc[3] << 24 |
119 (unsigned long long) utc.utc[4] << 32;
120 }
121
122
123 void format_time(char *buffer, int size, const char *format)
124 {
125 oswordreadclock_utc_block utc;
126 os_error *error;
127
128 utc.op = oswordreadclock_OP_UTC;
129 error = xoswordreadclock_utc(&utc);
130 if (error != NULL) die(error->errmess);
131
132 error = xterritory_convert_date_and_time(territory_CURRENT,
133 &utc.utc, buffer, size, format, NULL);
134 if (error != NULL) die(error->errmess);
135 }
136
137
138 void print_ulonglong(FILE *stream, unsigned long long x)
139 {
140 char s[32];
141 int i = 31;
142
143 s[i] = '\0';
144 while (x)
145 {
146 s[--i] = "0123456789"[x % 10];
147 x /= 10;
148 }
149 fprintf(stream, s + i);
150 }
151
152
153 int main(void)
154 {
155 FILE *discs;
156 char line[200];
157
158 message_table = messages_read("<GraphFree$Messages>");
159
160 discs = fopen("<Obey$Dir>.Discs.Discs", "r");
161 if (discs == NULL) die(message_table[1]);
162
163 while (fgets(line, 200, discs))
164 {
165 line[strlen(line) - 1] = '\0';
166 if ((line[0] != '#') && (line[0] != '\0'))
167 {
168 char buffer[200];
169 unsigned long long used, time;
170 FILE *out;
171 os_error *error;
172
173 used = used_space(line);
174 time = current_time();
175
176 sprintf(buffer, "<Obey$Dir>.Discs.%s", strchr(line, ':') + 2);
177 out = fopen(buffer, "a");
178 if (out == NULL) die(message_table[3]);
179
180 if (ftell(out) == 0) fprintf(out, "%s\n", message_table[5]);
181
182 format_time(buffer, 200, message_table[4]);
183 fprintf(out, "\"%s\",", buffer);
184 print_ulonglong(out, time);
185 fprintf(out, ",");
186 print_ulonglong(out, used);
187 fprintf(out, "\n");
188
189 fclose(out);
190
191 sprintf(buffer, "<Obey$Dir>.Discs.%s", strchr(line, ':') + 2);
192 error = xosfile_set_type(buffer, 0xdfe);
193 if (error != NULL) die(error->errmess);
194 }
195 }
196
197 return 0;
198 }

  ViewVC Help
Powered by ViewVC 1.1.26