-
Notifications
You must be signed in to change notification settings - Fork 0
/
dktext.c
166 lines (159 loc) · 3.32 KB
/
dktext.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char **text;
int size=0;
int capacity=0;
void save_file (char *fname);
void display_usage (void);
void load_file (char *fname);
void add_text (char *str);
int main (int argc, char **argv)
{
static char message[4096];
int i;
int num=0;
if (argc==1)
display_usage();
load_file (argv[1]);
if (argc==2) /* List strings */
{
for (i=0; i < size; i++)
printf ("%d: %s\r\n\r\n", i, text[i]);
return 0;
}
switch (argv[2][0])
{
case 'v': /* Display a single message */
num = atoi (argv[3]);
if (num==0 && argv[3][0]!='0')
{
printf ("Error: %s is not a number.\r\n", argv[3]);
return 1;
}
if (num<0 || num >= size)
{
printf ("Error: Message number must be in range 0-%d.\r\n",
size-1);
return 1;
}
printf ("%d: %s\r\n", num, text[num]);
return 0;
case 'e': /* Edit a message */
num = atoi (argv[3]);
if (num==0 && argv[3][0]!='0')
{
printf ("Error: %s is not a number.\r\n", argv[3]);
return 1;
}
if (num<0 || num >= size)
{
printf ("Error: Message number must be in range 0-%d.\r\n",
size-1);
return 1;
}
break;
case 'a':
num=-1;
break;
default:
printf ("Error: Unknown option %s\r\n", argv[2]);
display_usage();
break;
}
printf ("Enter message: ");
fgets (message, 4095, stdin);
for (i=0; message[i]; i++)
{
if (message[i]<32)
{
message[i]=0;
break;
}
}
if (!*message)
{
printf ("Operation cancelled.\r\n");
return 0;
}
if (num != -1)
text[num]=strdup(message);
else
{
printf ("Adding message %d\n", size);
add_text (strdup (message));
}
save_file (argv[1]);
return 0;
}
void save_file (char *fname)
{
int i;
FILE *fp;
fp = fopen (fname, "wb");
if (!fp)
{
printf ("Error: Can't open %s.\r\n", fname);
exit (1);
}
for (i=0; i < size; i++)
fprintf (fp, "%s%c", text[i], 0);
fputc (0, fp);
fclose (fp);
printf ("File saved.\r\n");
}
void load_file (char *fname)
{
static unsigned char buffer [4096]; /* Should be enough! */
int l=0, g;
FILE *fp;
fp = fopen (fname, "rb");
if (!fp)
{
printf ("Error: Can't open %s.\r\n", fname);
exit (1);
}
while ((g=fgetc (fp)) != EOF)
{
buffer[l++]=(unsigned char)g;
if (l==4096)
{
printf ("Error: Length of message %d exceeds 4096 bytes.\r\n",
size);
exit (1);
}
if (!g)
{
l=0;
add_text (strdup (buffer));
}
}
fclose (fp);
size--; /* Last message is always blank */
}
void add_text (char *str)
{
if (size==capacity)
{
capacity+=5;
if (size)
text = realloc (text, capacity*sizeof (char *));
else
text = malloc (capacity*sizeof (char *));
if (!text)
{
printf ("Error: Out of memory.\r\n");
exit (1);
}
}
text[size++]=str;
}
void display_usage (void)
{
printf ("Usage:\r\n"
" dktext <filename> - List all messages\r\n"
" dktext <filename> v <number> - Display specified message\r\n"
" dktext <filename> e <number> - Replace message\r\n"
" dktext <filename> a - Add message to end\r\n");
exit (0);
}