1 | /* |
---|
2 | * $Id: misc.c,v 4.9 1999-08-13 00:15:12 danw Exp $ |
---|
3 | */ |
---|
4 | |
---|
5 | #ifndef lint |
---|
6 | static char *rcsid_header_h = "$Id: misc.c,v 4.9 1999-08-13 00:15:12 danw Exp $"; |
---|
7 | #endif |
---|
8 | |
---|
9 | #include "bellcore-copyright.h" |
---|
10 | #include "mit-copyright.h" |
---|
11 | |
---|
12 | #include "track.h" |
---|
13 | |
---|
14 | /* |
---|
15 | * diagnostic stuff: used throughout track |
---|
16 | */ |
---|
17 | void |
---|
18 | printmsg( filep) FILE *filep; |
---|
19 | { |
---|
20 | int i; |
---|
21 | char *s; |
---|
22 | |
---|
23 | if ( filep); |
---|
24 | else if ( nopullflag) return; |
---|
25 | else if ( logfile = fopen( logfilepath, "w+")) { |
---|
26 | (void) fchmod( fileno(logfile), 0664); |
---|
27 | filep = logfile; |
---|
28 | } |
---|
29 | else { |
---|
30 | fprintf( stderr, "can't open logfile %s.\n", logfilepath); |
---|
31 | perror("system error is: "); |
---|
32 | clearlocks(); |
---|
33 | exit(1); |
---|
34 | } |
---|
35 | fprintf( filep, "\n***%s: %s", prgname, errmsg); |
---|
36 | |
---|
37 | if ( '\n' != errmsg[ strlen( errmsg) - 1]) |
---|
38 | putc('\n', filep); |
---|
39 | |
---|
40 | fprintf( filep, " system error is '%s'.\n", strerror( errno)); |
---|
41 | |
---|
42 | if ( entnum >= 0) i = entnum; /* passed parser */ |
---|
43 | else if ( entrycnt >= 0)i = entrycnt; /* in parser */ |
---|
44 | else i = -1; /* hard to tell */ |
---|
45 | |
---|
46 | if ( *subfilepath) s = subfilepath; |
---|
47 | else if ( *subfilename) s = subfilename; |
---|
48 | else s = "<unknown>"; |
---|
49 | |
---|
50 | fprintf( filep, " Working on list named %s", s); |
---|
51 | |
---|
52 | if ( i < 0) fprintf( filep," before parsing a list-elt.\n"); |
---|
53 | else fprintf( filep," & entry #%d: '%s'\n", |
---|
54 | i, entries[ i].fromfile); |
---|
55 | |
---|
56 | /* a nuance of formatting: |
---|
57 | * we want to separate error-msgs from the update banners |
---|
58 | * with newlines, but if the update banners aren't present, |
---|
59 | * we don't want the error-msgs to be double-spaced. |
---|
60 | */ |
---|
61 | if ( verboseflag && filep == stderr) |
---|
62 | fputc('\n', filep); |
---|
63 | } |
---|
64 | |
---|
65 | void |
---|
66 | do_gripe() |
---|
67 | { |
---|
68 | printmsg( logfile); |
---|
69 | if ( quietflag) return; |
---|
70 | printmsg( stderr); |
---|
71 | |
---|
72 | if ( ! verboseflag) { |
---|
73 | /* turn on verbosity, on the assumption that the user |
---|
74 | * now needs to know what's being done. |
---|
75 | */ |
---|
76 | verboseflag = 1; |
---|
77 | fprintf(stderr, "Turning on -v option. -q suppresses this.\n"); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | void |
---|
82 | do_panic() |
---|
83 | { |
---|
84 | printmsg( logfile); |
---|
85 | printmsg( stderr); |
---|
86 | clearlocks(); |
---|
87 | exit(1); |
---|
88 | } |
---|
89 | |
---|
90 | /* |
---|
91 | * parser-support routines: |
---|
92 | */ |
---|
93 | |
---|
94 | void |
---|
95 | doreset() |
---|
96 | { |
---|
97 | *linebuf = '\0'; |
---|
98 | *wordbuf = '\0'; |
---|
99 | } |
---|
100 | |
---|
101 | void |
---|
102 | parseinit( subfile) FILE *subfile; |
---|
103 | { |
---|
104 | yyin = subfile; |
---|
105 | yyout = stderr; |
---|
106 | doreset(); |
---|
107 | entrycnt = 0; |
---|
108 | clear_ent(); |
---|
109 | errno = 0; |
---|
110 | } |
---|
111 | |
---|
112 | Entry * |
---|
113 | clear_ent() |
---|
114 | { |
---|
115 | Entry* e = &entries[ entrycnt]; |
---|
116 | struct currentness *c = &e->currency; |
---|
117 | |
---|
118 | e->islink = 0; |
---|
119 | *e->sortkey = '\0'; |
---|
120 | e->keylen = 0; |
---|
121 | e->fromfile = (char*) 0; |
---|
122 | e->tofile = (char*) 0; |
---|
123 | e->cmpfile = (char*) 0; |
---|
124 | e->cmdbuf = (char*) 0; |
---|
125 | |
---|
126 | *c->name = '\0'; |
---|
127 | *c->link = '\0'; |
---|
128 | c->cksum = 0; |
---|
129 | clear_stat( &c->sbuf); |
---|
130 | |
---|
131 | e->names.table = (List_element**) 0; |
---|
132 | e->names.shift = 0; |
---|
133 | e->patterns = entries[ 0].patterns; /* XXX global patterns */ |
---|
134 | |
---|
135 | return( e); |
---|
136 | } |
---|
137 | |
---|
138 | void |
---|
139 | savestr(to,from) |
---|
140 | char **to, *from; |
---|
141 | { |
---|
142 | if (!(*to = malloc(( unsigned) strlen( from)+1))) { |
---|
143 | sprintf(errmsg,"ran out of memory during parse"); |
---|
144 | do_panic(); |
---|
145 | } |
---|
146 | strcpy(*to,from); |
---|
147 | } |
---|
148 | |
---|
149 | /* Convert mode (file type) bits to a string */ |
---|
150 | |
---|
151 | char *mode_to_string(mode) |
---|
152 | int mode; |
---|
153 | { |
---|
154 | switch(mode & S_IFMT) |
---|
155 | { |
---|
156 | case S_IFDIR: |
---|
157 | return "directory"; |
---|
158 | case S_IFCHR: |
---|
159 | return "char-device"; |
---|
160 | case S_IFBLK: |
---|
161 | return "block-device"; |
---|
162 | case S_IFREG: |
---|
163 | return "file"; |
---|
164 | #ifdef S_IFIFO |
---|
165 | case S_IFIFO: |
---|
166 | return "fifo"; |
---|
167 | #endif |
---|
168 | case S_IFLNK: |
---|
169 | return "symlink"; |
---|
170 | case S_IFSOCK: |
---|
171 | return "socket"; |
---|
172 | #ifdef S_IFMPX |
---|
173 | case S_IFMPX: |
---|
174 | return "multi char-device"; |
---|
175 | #endif |
---|
176 | default: |
---|
177 | return "nonexistent"; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | char *mode_to_fmt(mode) |
---|
182 | int mode; |
---|
183 | { |
---|
184 | switch(mode & S_IFMT) |
---|
185 | { |
---|
186 | case S_IFDIR: |
---|
187 | return "d%s %c%d(%d.%d.%o)\n"; |
---|
188 | #ifdef S_IFMPX |
---|
189 | case S_IFMPX: |
---|
190 | #endif |
---|
191 | case S_IFCHR: |
---|
192 | return "c%s %c%d(%d.%d.%o)\n"; |
---|
193 | case S_IFBLK: |
---|
194 | return "b%s %c%d(%d.%d.%o)\n"; |
---|
195 | case S_IFREG: |
---|
196 | return "f%s %c%x(%d.%d.%o)%ld\n"; |
---|
197 | case S_IFLNK: |
---|
198 | return "l%s %c%s\n"; |
---|
199 | #ifdef S_IFIFO |
---|
200 | case S_IFIFO: |
---|
201 | #endif |
---|
202 | case S_IFSOCK: |
---|
203 | return "*ERROR (write_statline): can't track socket %s.\n"; |
---|
204 | default: |
---|
205 | return "*ERROR (write_statline): %s's file type is unknown.\n"; |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | char *mode_to_rfmt(mode) |
---|
210 | int mode; |
---|
211 | { |
---|
212 | switch(mode & S_IFMT) |
---|
213 | { |
---|
214 | #ifdef S_IFMPX |
---|
215 | case S_IFMPX: |
---|
216 | #endif |
---|
217 | case S_IFDIR: |
---|
218 | case S_IFCHR: |
---|
219 | case S_IFBLK: |
---|
220 | return "%d(%d.%d.%o)\n"; |
---|
221 | case S_IFREG: |
---|
222 | return "%x(%d.%d.%o)%ld\n"; |
---|
223 | default: |
---|
224 | return ""; |
---|
225 | } |
---|
226 | } |
---|