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