File: function.c

package info (click to toggle)
fsp 2.81.b3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,072 kB
  • ctags: 1,264
  • sloc: ansic: 7,764; makefile: 357; sh: 312
file content (617 lines) | stat: -rw-r--r-- 14,474 bytes parent folder | download | duplicates (2)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*-
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Cimarron D. Taylor of the University of California, Berkeley.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */


#include "tweak.h"
#ifdef HAVE_UNISTD_H
#ifndef __hpux
#include <unistd.h>
#endif
#endif
#include <stdio.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
#include "my-string.h"
#ifndef VMS
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#else
#include "types.h"
#include "stat.h"
#include "param.h"
#endif
#ifdef _POSIX_SOURCE
#include "limits.h"
#if !defined(MAXPATHLEN) && defined(PATH_MAX)
#define MAXPATHLEN PATH_MAX
#endif
#endif
#ifdef HAVE_TZFILE_H
#include <tzfile.h>
#endif

#ifndef VMS
#include <sys/wait.h>
#else
#include "wait.h"
#endif
#include "find.h"

#define	FIND_EQUAL	0
#define	FIND_LESSTHAN	1
#define	FIND_GREATER	2

#define	COMPARE(a, b) { \
  switch(plan->flags) { \
    case FIND_EQUAL: \
      return(a == b); \
    case FIND_LESSTHAN: \
      return(a < b); \
    case FIND_GREATER: \
      return(a > b); \
  } \
  return(0); \
}

#ifdef HAVE_ANSI_PROTO
static PLAN *palloc (enum ntype t, int (*f)())
#else
static PLAN *palloc (t, f)
    enum ntype t;
    int (*f)();
#endif
{
  PLAN *new;

  if (new = (PLAN *) malloc(sizeof(PLAN))) {
    new->type = t;
    new->eval = f;
    new->flags = 0;
    new->next = NULL;
    return(new);
  }
  perror("palloc");
  exit(1);
}

extern int isoutput;
extern int process;

/*
 * find_parsenum --
 *	Parse a string of the form [+-]# and return the value.
 */
static long find_parsenum PROTO4(PLAN *, plan, char *, option, char *, str,
				 char *, endch)
{
  long value;
  char *endchar;		/* pointer to character ending conversion */
     
  /* determine comparison from leading + or - */
  switch(*str) {
    case '+':
      ++str;
      plan->flags = FIND_GREATER;
      break;
    case '-':
      ++str;
      plan->flags = FIND_LESSTHAN;
      break;
    default:
      plan->flags = FIND_EQUAL;
      break;
  }
     
  /*
   * convert the string with strtol().  Note, if strtol() returns zero
   * and endchar points to the beginning of the string we know we have
   * a syntax error.
   */
  value = strtol(str, &endchar, 10);
  if (!value && endchar == str || endchar[0] &&
      (!endch || endchar[0] != *endch)) {
    fprintf(stderr,"%s: %s", option, "illegal numeric value");
    exit(1);
  }
  if (endch)
    *endch = endchar[0];
  return(value);
}

/*
 * -time n functions --
 *
 *	True if the difference between the file time and the
 *	current time is n 24 hour periods.
 *
 */

static int find_time PROTO3(PLAN *, plan, struct stat *, sbuf,  char *, path)
{
  extern time_t now;

  /* with FSP sbuf.st_atime=sbuf.st_ctime=sbuf.st_mtime */
  COMPARE((now - sbuf->st_atime + SECSPERDAY - 1) / SECSPERDAY, plan->t_data);
}

 
PLAN * c_time PROTO1(char *, arg)
{
  PLAN *new;

  new = palloc(N_TIME, find_time);
  new->t_data = find_parsenum(new, "-time", arg, NULL);
  return(new);
}

/*
 * brace_subst --
 *      Replace occurrences of {} in s1 with s2 and return the result string.
 */
void brace_subst PROTO4(char *, orig, char **, store, char *, path, int,len)
{
  register int plen;
  register char ch, *p;

  plen = strlen(path);
  for (p = *store; ch = *orig; ++orig)
    if (ch == '{' && orig[1] == '}') {
      while ((p - *store) + plen > len)
        if (!(*store = (char *)realloc(*store, len *= 2))) {
          perror("realloc");
          client_done();
          exit(1);
        }
      bcopy(path, p, plen);
      p += plen;
      ++orig;
    } else
      *p++ = ch;
  *p = '\0';
}

/*
 * queryuser --
 *      print a message to standard error and then read input from standard
 *      input. If the input is 'y' then 1 is returned.
 */
int queryuser PROTO1(char **, argv)
{
  int ch, first, nl;

  fprintf(stderr, "\"%s", *argv);
  while (*++argv) fprintf(stderr, " %s", *argv);
  fprintf(stderr, "\"? ");
  fflush(stderr);

  first = ch = getchar();
  for (nl = 0;;) {
    if (ch == '\n') {
      nl = 1;
      break;
    }
    if (ch == EOF) break;
    ch = getchar();
  }

  if (!nl) {
    fprintf(stderr, "\n");
    fflush(stderr);
  }
  return(first == 'y');
}

/*
 * [-exec | -ok] utility [arg ... ] ; functions --
 *
 *	True if the executed utility returns a zero value as exit status.
 *	The end of the primary expression is delimited by a semicolon.  If
 *	"{}" occurs anywhere, it gets replaced by the current pathname.
 *	The current directory for the execution of utility is the same as
 *	the current directory when the find utility was started.
 *
 *	The primary -ok is different in that it requests affirmation of the
 *	user before executing the utility.
 */
static int find_exec PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  register int cnt;
  pid_t pid;
#ifndef HAVE_UNION_WAIT
  int status;
#else
  union wait status;
#endif

  for (cnt = 0; plan->e_argv[cnt]; ++cnt)
    if (plan->e_len[cnt])
      brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt], path,
		  plan->e_len[cnt]);

  if (plan->flags && !queryuser(plan->e_argv)) return(0);

#ifndef VMS
  switch(pid = fork())
#else
  switch(pid = vfork())
#endif
   {
    case -1:
      perror ("fork");
      exit(1);
    case 0:
      execvp(plan->e_argv[0], plan->e_argv);
      perror ("execvp");
      exit(1);
  }
  pid = wait(&status);

 return(pid != -1 && WIFEXITED(status) && !WEXITSTATUS(status));
}

char *emalloc_ffind PROTO1(unsigned int, len)
{
  char *p;

  if (p = (char *)malloc(len)) return((char *)p);
  perror("malloc");
  exit(1);
}
 
/*
 * c_exec --
 *	build three parallel arrays, one with pointers to the strings passed
 *	on the command line, one with (possibly duplicated) pointers to the
 *	argv array, and one with integer values that are lengths of the
 *	strings, but also flags meaning that the string has to be massaged.
 */
PLAN *c_exec PROTO2(char ***, argvp, int, isok)
{
  PLAN *new;			/* node returned */
  register int cnt;
  register char **argv, **ap, *p;

  isoutput = 1;
    
  new = palloc(N_EXEC, find_exec);
  new->flags = isok;

  for (ap = argv = *argvp;; ++ap) {
    if (!*ap) {
      fprintf(stderr,"%s: no terminating", isok ? "-ok" : "-exec");
      exit(1);
    }
    if (**ap == ';') break;
  }

  cnt = ap - *argvp + 1;
  new->e_argv = (char **)emalloc_ffind((unsigned int)cnt * sizeof(char *));
  new->e_orig = (char **)emalloc_ffind((unsigned int)cnt * sizeof(char *));
  new->e_len = (int *)emalloc_ffind((unsigned int)cnt * sizeof(int));

  for (argv = *argvp, cnt = 0; argv < ap; ++argv, ++cnt) {
    new->e_orig[cnt] = *argv;
    for (p = *argv; *p; ++p)
      if (p[0] == '{' && p[1] == '}') {
       	new->e_argv[cnt] = (char *)emalloc_ffind((unsigned int)MAXPATHLEN);
      	new->e_len[cnt] = MAXPATHLEN;
      	break;
      }
    if (!*p) {
      new->e_argv[cnt] = *argv;
      new->e_len[cnt] = 0;
    }
  }
  new->e_argv[cnt] = new->e_orig[cnt] = NULL;

  *argvp = argv + 1;
  return(new);
}

int printtime_ffind PROTO1(time_t, ftime)
{
  int i;
  char *longstring;

  longstring = (char *)ctime((long *)&ftime);
  for (i = 4; i < 11; ++i) putchar(longstring[i]);

#define SIXMONTHS       ((DAYSPERNYEAR / 2) * SECSPERDAY)

  if (ftime + SIXMONTHS > time((time_t *)NULL))
    for (i = 11; i < 16; ++i) putchar(longstring[i]);
  else {
    (void)putchar(' ');
    for (i = 20; i < 24; ++i) putchar(longstring[i]);
  }
  putchar(' ');
}

#define BLK(A) (((A)+1023)/1024)

void printlong_ffind PROTO2(char *, name, struct stat *, sb)
{
  char *modep;

  printf("%4ld ", BLK(sb->st_size));
  modep = ((S_IFDIR & sb->st_mode)) ? "drwxrwxrwx" : "-rw-rw-rw-" ;
  printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, 8, "nobody", 8, "nobody");

  printf("%8ld ", sb->st_size);
  printtime_ffind(sb->st_mtime);
  printf("%s", name);
  putchar('\n');
}
 
/*
 * -ls functions --
 *
 *	Always true - prints the current sbuf to stdout in "ls" format.
 */
static int find_ls PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  printlong_ffind(path, sbuf);
  return(1);
}
 
PLAN *c_ls PROTO0((void))
{
  isoutput = 1;
  return(palloc(N_LS, find_ls));
}

/*
 * -name functions --
 *
 *	True if the basename of the filename being examined
 *	matches pattern using Pattern Matching Notation S3.14
 */
static int find_name PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  register char *name;

  /* extract filename */
  for (name = path + strlen(path); name > path && *name != '/'; name--);
  if (*name == '/') name++;
  return(fnmatch(plan->c_data, name));
}
 
PLAN *c_name PROTO1(char *, pattern)
{
  PLAN *new;

  new = palloc(N_NAME, find_name);
  new->c_data = pattern;
  return(new);
}
 
/*
 * -newer file functions --
 *
 *	True if the current file has been modified more recently
 *	then the modification time of the file named by the pathname
 *	file.
 */
static int find_newer PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  return(sbuf->st_mtime > plan->t_data);
}
 
PLAN *c_newer PROTO1(char *, filename)
{
  PLAN *new;
  struct stat sb;
    
  if (stat(filename, &sb)) {
    perror("stat");
    exit(1);
  }
  new = palloc(N_NEWER, find_newer);
  new->t_data = sb.st_mtime;
  return(new);
}
 
/*
 * -print functions --
 *
 *	Always true, causes the current pathame to be written to
 *	standard output.
 */
static int find_print PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  (void)printf("%s\n", path);
  return(1);
}
 
PLAN *c_print PROTO0((void))
{
  isoutput = 1;

  return(palloc(N_PRINT, find_print));
}
 
/*
 * -prune functions --
 *
 *	Prune a portion of the hierarchy.
 */
static int find_prune PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  process = -1;
  return(1);
}
 
PLAN *c_prune PROTO0((void))
{
  return(palloc(N_PRUNE, find_prune));
}
 
/*
 * -size n[c] functions --
 *
 *	True if the file size in bytes, divided by an implementation defined
 *	value and rounded up to the next integer, is n.  If n is followed by
 *	a c, the size is in bytes.
 */
#define	FIND_SIZE	512
static int divsize = 1;

static int find_size PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  off_t size;

  size = divsize ? ((sbuf->st_size + FIND_SIZE - 1)/FIND_SIZE) : sbuf->st_size;
  COMPARE(size, plan->o_data);
}
 
PLAN *c_size PROTO1(char *, arg)
{
  PLAN *new;
  char endch='c';
    
  new = palloc(N_SIZE, find_size);
  new->o_data = find_parsenum(new, "-size", arg, &endch);
  if (endch == 'c') divsize = 0;
  return(new);
}
 
/*
 * -type c functions --
 *
 *	True if the type of the file is c, where c is d or f for
 *	directory or regular file, respectively.
 */
static int find_type PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  return((sbuf->st_mode & S_IFMT) == plan->m_data);
}
 
PLAN *c_type PROTO1(char *, typestring)
{
  PLAN *new;
  mode_t  mask;
    
  switch (typestring[0]) {
    case 'd':
      mask = S_IFDIR;
      break;
    case 'f':
      mask = S_IFREG;
      break;
    default:
      fprintf(stderr,"-type: unknown type");
      exit(1);
  }
     
  new = palloc(N_TYPE, find_type);
  new->m_data = mask;
  return(new);
}
 
/*
 * ( expression ) functions --
 *
 *	True if expression is true.
 */
int find_expr PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  register PLAN *p;
  register int state;

  for(p=plan->p_data[0]; p && (state=(p->eval)(p, sbuf, path)); p=p->next);
  return(state);
}
 
/*
 * N_OPENPAREN and N_CLOSEPAREN nodes are temporary place markers.  They are
 * eliminated during phase 2 of find_formplan() --- the '(' node is converted
 * to a N_EXPR node containing the expression and the ')' node is discarded.
 */
PLAN *c_openparen PROTO0((void))
{
  return(palloc(N_OPENPAREN, (int (*)())-1));
}
 
PLAN *c_closeparen PROTO0((void))
{
  return(palloc(N_CLOSEPAREN, (int (*)())-1));
}
 

/*
 * ! expression functions --
 *
 *	Negation of a primary; the unary NOT operator.
 */
static int find_not PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  register PLAN *p;
  register int state;

  for(p=plan->p_data[0]; p && (state=(p->eval)(p, sbuf, path)); p=p->next);
  return(!state);
}
 
PLAN *c_not PROTO0((void))
{
  return(palloc(N_NOT, find_not));
}
 
/*
 * expression -o expression functions --
 *
 *	Alternation of primaries; the OR operator.  The second expression is
 * not evaluated if the first expression is true.
 */
static int find_or PROTO3(PLAN *, plan, struct stat *, sbuf, char *, path)
{
  register PLAN *p;
  register int state;

  for(p=plan->p_data[0]; p && (state=(p->eval)(p, sbuf, path)); p=p->next);

  if (state) return(1);

  for(p=plan->p_data[1]; p && (state=(p->eval)(p, sbuf, path)); p=p->next);
  return(state);
}

PLAN *c_or PROTO0((void))
{
  return(palloc(N_OR, find_or));
}