File clp.c changed (mode: 100644) (index 193c45d..5834d6d) |
3 |
3 |
#include <string.h> |
#include <string.h> |
4 |
4 |
|
|
5 |
5 |
#define maxtofrom 10 |
#define maxtofrom 10 |
6 |
|
/* ocountflag means that it will only count |
|
7 |
|
how many entries there are then quit */ |
|
8 |
6 |
|
|
9 |
|
long from=0, to=0, end=0; |
|
10 |
|
int inflag=0, outflag=0, ocountflag=0, delflag=0, commaflag=0; |
|
11 |
|
char tester[100000]; /* if you have a line longer than this then you have a problem. */ |
|
12 |
|
char *filename; |
|
13 |
|
|
|
14 |
|
void prepcomms(char *where) |
|
|
7 |
|
int main(int argc, char *argv[]) |
15 |
8 |
{ |
{ |
16 |
|
int fromcount=0, tocount=0; |
|
17 |
|
char cfrom[maxtofrom+8]; |
|
18 |
|
char cto[maxtofrom+8]; |
|
19 |
|
for (int j=0; where[j] && j<=(maxtofrom*2+8); j++) { |
|
20 |
|
if (where[j]==',') { commaflag=1; continue; } |
|
21 |
|
switch (where[j]) { |
|
22 |
|
case 'i': inflag = 1; |
|
23 |
|
break; |
|
24 |
|
case 'p': outflag = 1; |
|
25 |
|
break; |
|
26 |
|
case 'd': delflag = 1;; |
|
27 |
|
break; |
|
28 |
|
case 'c': ocountflag = 1; |
|
29 |
|
break; |
|
30 |
|
} |
|
31 |
|
if (!commaflag) cfrom[fromcount++] = where[j]; |
|
32 |
|
else cto[tocount++] = where[j]; |
|
33 |
|
} |
|
34 |
|
if (commaflag) { |
|
35 |
|
from = atol(cfrom); |
|
36 |
|
to = atol(cto); |
|
37 |
|
} else { |
|
38 |
|
to = (from=atol(cfrom)); |
|
39 |
|
} |
|
40 |
|
if (!from && commaflag && to) { |
|
41 |
|
fputs("clip: must have a number before comma\n", stderr); |
|
42 |
|
free(filename); |
|
43 |
|
exit(1); |
|
44 |
|
} |
|
45 |
|
if (from > to) { |
|
46 |
|
fputs("clip: first number must be less than second\n", stderr); |
|
47 |
|
free(filename); |
|
48 |
|
exit(1); |
|
49 |
|
} |
|
50 |
|
} |
|
|
9 |
|
long from=0, to=0, end=0; |
|
10 |
|
int inflag=0, outflag=0, ocountflag=0, delflag=0, commaflag=0; |
|
11 |
|
FILE *filep; |
|
12 |
|
FILE *tdelfilep; |
|
13 |
|
char tester[100000]; /* if you have a line longer than this then you have a problem. */ |
|
14 |
|
char *filename; |
|
15 |
|
filename = (char *)malloc(300); |
|
16 |
|
sprintf(filename, "%s/%s", getenv("HOME"), ".clipboard"); |
|
17 |
|
char delim[32] = "------------------------------\n"; |
|
18 |
|
long counter = 0; |
51 |
19 |
|
|
52 |
|
void cookargs(int c, char *v[]) |
|
53 |
|
{ |
|
54 |
|
if (c>1) { |
|
55 |
|
for (int i=1; v[i]; i++) { |
|
56 |
|
if (v[i][0]=='-' && v[i][1] && v[i][1]=='f' && v[i+1]) { |
|
57 |
|
filename = v[i+1]; |
|
|
20 |
|
if (argc>1) { |
|
21 |
|
for (int i=1; argv[i]; i++) { |
|
22 |
|
if (argv[i][0]=='-' && argv[i][1] && argv[i][1]=='f' && argv[i+1]) { |
|
23 |
|
filename = argv[i+1]; |
58 |
24 |
i++; /* makes cursor skip file name */ |
i++; /* makes cursor skip file name */ |
59 |
25 |
} |
} |
60 |
|
else prepcomms(v[i]); |
|
|
26 |
|
else { |
|
27 |
|
char *where = argv[i]; |
|
28 |
|
int fromcount=0, tocount=0; |
|
29 |
|
char cfrom[maxtofrom+8]; |
|
30 |
|
char cto[maxtofrom+8]; |
|
31 |
|
for (int j=0; where[j] && j<=(maxtofrom*2+8); j++) { |
|
32 |
|
if (where[j]==',') { commaflag=1; continue; } |
|
33 |
|
switch (where[j]) { |
|
34 |
|
case 'i': inflag = 1; |
|
35 |
|
break; |
|
36 |
|
case 'p': outflag = 1; |
|
37 |
|
break; |
|
38 |
|
case 'd': delflag = 1;; |
|
39 |
|
break; |
|
40 |
|
case 'c': ocountflag = 1; |
|
41 |
|
break; |
|
42 |
|
} |
|
43 |
|
if (!commaflag) cfrom[fromcount++] = where[j]; |
|
44 |
|
else cto[tocount++] = where[j]; |
|
45 |
|
} |
|
46 |
|
if (commaflag) { |
|
47 |
|
from = atol(cfrom); |
|
48 |
|
to = atol(cto); |
|
49 |
|
} else { |
|
50 |
|
to = (from=atol(cfrom)); |
|
51 |
|
} |
|
52 |
|
if (!from && commaflag && to) { |
|
53 |
|
fputs("clip: must have a number before comma\n", stderr); |
|
54 |
|
exit(1); |
|
55 |
|
} |
|
56 |
|
if (from > to) { |
|
57 |
|
fputs("clip: first number must be less than second\n", stderr); |
|
58 |
|
exit(1); |
|
59 |
|
} |
|
60 |
|
} |
61 |
61 |
} |
} |
62 |
62 |
if (!inflag && !outflag && !delflag && !ocountflag) { |
if (!inflag && !outflag && !delflag && !ocountflag) { |
63 |
63 |
fputs("usage: clip [-f filename] [justcount|in|out #,#|delete #,#]\n", stderr); |
fputs("usage: clip [-f filename] [justcount|in|out #,#|delete #,#]\n", stderr); |
64 |
|
free(filename); |
|
65 |
64 |
exit(1); |
exit(1); |
66 |
65 |
} |
} |
67 |
66 |
} else { |
} else { |
68 |
67 |
fputs("usage: clip [-f filename] [count|in|print #,#|delete #,#]\n", stderr); |
fputs("usage: clip [-f filename] [count|in|print #,#|delete #,#]\n", stderr); |
69 |
|
free(filename); |
|
70 |
68 |
exit(1); |
exit(1); |
71 |
69 |
} |
} |
72 |
|
} |
|
73 |
|
|
|
74 |
|
int main(int argc, char *argv[]) |
|
75 |
|
{ |
|
76 |
|
filename = malloc(3000); |
|
77 |
|
sprintf(filename, "%s/%s", getenv("HOME"), ".clipboard"); |
|
78 |
|
char delim[31] = "------------------------------\n"; |
|
79 |
|
FILE *filep; |
|
80 |
|
long counter = 0; |
|
81 |
|
cookargs(argc, argv); /* can change file name based on what comes after "-f" */ |
|
82 |
70 |
|
|
83 |
71 |
if ((filep=fopen(filename, "a+"))==NULL) { |
if ((filep=fopen(filename, "a+"))==NULL) { |
84 |
72 |
fprintf(stderr, "clip: cannot open %s", filename); |
fprintf(stderr, "clip: cannot open %s", filename); |
85 |
|
free(filename); |
|
86 |
73 |
exit(1); |
exit(1); |
87 |
74 |
} |
} |
88 |
75 |
if (inflag) { |
if (inflag) { |
89 |
76 |
char inbuf[1000000]; |
char inbuf[1000000]; |
90 |
77 |
fread(inbuf, sizeof(inbuf), 1, stdin); |
fread(inbuf, sizeof(inbuf), 1, stdin); |
91 |
78 |
fprintf(filep, "%s%s", delim, inbuf); |
fprintf(filep, "%s%s", delim, inbuf); |
92 |
|
fclose(filep); |
|
93 |
|
free(filename); |
|
94 |
|
exit(1); |
|
|
79 |
|
goto ending; |
95 |
80 |
} |
} |
96 |
81 |
|
|
97 |
82 |
while (fgets(tester, sizeof(tester), filep)) |
while (fgets(tester, sizeof(tester), filep)) |
98 |
|
if (memcmp(tester, delim, sizeof(delim))==0) |
|
|
83 |
|
if (memcmp(tester, delim, sizeof(delim)-1)==0) |
99 |
84 |
end++; |
end++; |
100 |
|
|
|
101 |
85 |
rewind(filep); |
rewind(filep); |
|
86 |
|
|
102 |
87 |
if (ocountflag) { |
if (ocountflag) { |
103 |
88 |
printf("%ld\n", end); |
printf("%ld\n", end); |
104 |
89 |
goto ending; /* stops program after outputting count */ |
goto ending; /* stops program after outputting count */ |
|
... |
... |
int main(int argc, char *argv[]) |
107 |
92 |
if (end==0) { |
if (end==0) { |
108 |
93 |
fputs("nothing to act on\n", stderr); |
fputs("nothing to act on\n", stderr); |
109 |
94 |
fclose(filep); |
fclose(filep); |
110 |
|
free(filename); |
|
111 |
95 |
exit(1); |
exit(1); |
112 |
96 |
} |
} |
113 |
97 |
if (to>end) { |
if (to>end) { |
114 |
98 |
fputs("clip: end point too big\n", stderr); |
fputs("clip: end point too big\n", stderr); |
115 |
99 |
fclose(filep); |
fclose(filep); |
116 |
|
free(filename); |
|
117 |
100 |
exit(1); |
exit(1); |
118 |
101 |
} |
} |
119 |
102 |
if (!from && !to && commaflag) { from=1; to=end; } |
if (!from && !to && commaflag) { from=1; to=end; } |
120 |
103 |
if (!from && !to && !commaflag) { from = end; to = end; } |
if (!from && !to && !commaflag) { from = end; to = end; } |
121 |
104 |
if (delflag) { |
if (delflag) { |
122 |
|
FILE *tdelfilep; |
|
123 |
105 |
int delimflag=0; |
int delimflag=0; |
124 |
106 |
if ((tdelfilep=fopen(".clipboard~~~", "w+"))==NULL) { |
if ((tdelfilep=fopen(".clipboard~~~", "w+"))==NULL) { |
125 |
107 |
fputs("clip: cannot create temporary file\n", stderr); |
fputs("clip: cannot create temporary file\n", stderr); |
126 |
108 |
fclose(filep); |
fclose(filep); |
127 |
|
free(filename); |
|
128 |
109 |
exit(1); |
exit(1); |
129 |
110 |
} |
} |
130 |
111 |
while (counter <= end && fgets(tester, sizeof(tester), filep)) { |
while (counter <= end && fgets(tester, sizeof(tester), filep)) { |
131 |
|
if (memcmp(tester, delim, sizeof(delim))==0) { |
|
|
112 |
|
if (memcmp(tester, delim, sizeof(delim)-1)==0) { |
132 |
113 |
counter++; |
counter++; |
133 |
114 |
delimflag=1; |
delimflag=1; |
134 |
115 |
continue; |
continue; |
|
... |
... |
int main(int argc, char *argv[]) |
144 |
125 |
|
|
145 |
126 |
if (outflag) { |
if (outflag) { |
146 |
127 |
while (counter <= end && fgets(tester, sizeof(tester), filep)) { |
while (counter <= end && fgets(tester, sizeof(tester), filep)) { |
147 |
|
if (memcmp(tester, delim, sizeof(delim))==0) { |
|
|
128 |
|
if (memcmp(tester, delim, sizeof(delim)-1)==0) { |
148 |
129 |
counter++; |
counter++; |
149 |
130 |
continue; |
continue; |
150 |
131 |
} |
} |
|
... |
... |
int main(int argc, char *argv[]) |
154 |
135 |
} |
} |
155 |
136 |
} |
} |
156 |
137 |
ending: |
ending: |
157 |
|
free(filename); |
|
158 |
138 |
fclose(filep); |
fclose(filep); |
159 |
|
return 0; |
|
|
139 |
|
exit(0); |
160 |
140 |
} |
} |