home *** CD-ROM | disk | FTP | other *** search
- /*
- Stuff 2.0.
-
- Checksum: 2327246523 (verify with "brik")
-
- (C) Copyright 1988 Rahul Dhesi. Permission is granted to copy and
- distribute this file in modified or unmodified form, whether for
- noncommercial or commercial use, provided (a) this copyright notice
- is preserved, (b) no attempt is made to restrict redistribution of
- this file, and (c) this file is not distributed as part of any
- collection whose redistribution is restricted by a compilation
- copyright.
- */
-
- /*
- parses a single option and stores in global array
- */
-
- #include "stuff.h"
- #include <stdio.h>
- #include <string.h>
-
- #define NEXT_ARG(i,j) (++(i) < j ? i : insufargs())
-
- request_rec *options[MAX_OPTS];
- char fmtstr[FMTSIZE];
- long size_limit = 0L;
-
- int last_opt = -1;
- static void badopt (char *option);
- static int insufargs(void);
-
- /* allocate a record */
- request_rec *getrec(int negate)
- {
- request_rec *optr = (request_rec *) emalloc (sizeof (request_rec));
- if (last_opt >= MAX_OPTS)
- memerr();
- options[++last_opt] = optr;
- optr->negate = negate;
- return optr;
- }
-
- void parseopt (char **argv, int *i, int argc, int negate)
- {
- FILE *infile; /* to read @ files */
- char *option;
- request_rec *optr;
-
- if (*i >= argc)
- (void) insufargs();
-
- option = argv[*i];
- if (STRCMP (option,==,OPT_PRINT)) {
- return; /* ignore print requests */
- }
-
- if (STRCMP (option,==,"!")) {
- ++(*i); /* move to next option */
- parseopt (argv, i, argc, NEGATE); /* parse it with negation */
- return;
- }
-
- if (STRCMP(option,==,OPT_NAME)) {
- optr = getrec (negate);
- optr->choice = NAME;
- strcpy (optr->info.name, argv[NEXT_ARG(*i,argc)]);
-
- #define LINE_SIZE 80
- /* now, if name was preceded by @, it is actually a filename */
- if (optr->info.name[0] == '@') {
- int first_time = 1;
- char this_line[LINE_SIZE];
- infile = fopen (optr->info.name + 1, "r");
- if (infile == NULL) {
- fprintf (stderr, "Can't open %s, skipped\n", optr->info.name);
- return;
- }
- while (1) {
- char *lastpos;
- do {
- *this_line = '\0';
- fgets (this_line, LINE_SIZE, infile);
- } while (*this_line == '\0' && !feof(infile));/*skip null lines*/
-
- if (*this_line == '\0') {
- break;
- }
-
- lastpos = lastptr (this_line);
- if (*lastpos == '\n') /* remove trailing newline */
- *lastpos = '\0';
-
- /* ugly code -- mem already allocated for first record */
- if (!first_time) {
- optr = getrec (negate);
- optr->choice = NAME;
- }
-
- first_time = 0;
-
- strcpy (optr->info.name, this_line);
-
- if (feof(infile))
- break;
- }
- fclose (infile);
- }
-
- } else if (STRCMP(option,==,OPT_MTIME)) {
- optr = getrec (negate);
- optr->choice = MTIME;
- optr->info.mtime_info.choice = getcompare(argv[NEXT_ARG(*i,argc)]);
- optr->info.mtime_info.value = getvalue(argv[*i]);
- } else if (STRCMP(option,==,OPT_SIZE)) {
- optr = getrec (negate);
- optr->choice = SIZE;
- optr->info.size_info.choice = getcompare(argv[NEXT_ARG(*i,argc)]);
- optr->info.size_info.value = getvalue(argv[*i]);
- } else if (STRCMP(option,==,OPT_NEWER)) {
- optr = getrec (negate);
- optr->choice = NEWER;
- optr->info.mtime_info.value = filetime(argv[NEXT_ARG(*i,argc)]);
- } else if (STRCMP(option,==,OPT_OLDER)) {
- optr = getrec (negate);
- optr->choice = OLDER;
- optr->info.mtime_info.value = filetime(argv[NEXT_ARG(*i,argc)]);
- } else if (STRCMP(option,==,OPT_MODIFIED)) {
- optr = getrec (negate);
- optr->choice = MODIFIED;
- } else if (STRCMP(option,==,OPT_FORMAT)) {
- strcpy(fmtstr,argv[NEXT_ARG(*i,argc)]);
- strcat(fmtstr, "\n");
- } else if (STRCMP(option,==,OPT_LIMIT)) {
- size_limit = getvalue(argv[NEXT_ARG(*i,argc)]);
- } else
- badopt (option);
- }
- void badopt (char *option)
- {
- fprintf (stderr, "stuff: FATAL: option [%s] is invalid\n", option);
- exit(1);
- }
-
- int insufargs(void)
- {
- fprintf (stderr, "stuff: FATAL: missing argument\n");
- exit(1);
- }
-
- /*
- returns BIGGER, SMALLER, or SAME according as argument begins with
- +, -, or neither
- */
- compare_pt getcompare (char *str)
- {
- if (*str == '+')
- return (BIGGER);
- else if (*str == '-')
- return (SMALLER);
- return (SAME);
- }
-
- #include <stdlib.h>
- /*
- changes text string into long integer
- */
- long getvalue (char *str)
- {
- if (*str == '+' || *str == '-')
- str++;
- return (atol(str));
- }
-
- void *emalloc (unsigned size)
- {
- void *ptr;
- ptr = malloc (size);
- if (ptr == NULL) {
- memerr();
- }
- return (ptr);
- }
-
- void memerr(void)
- {
- fprintf (stderr, "stuff: ran out of memory\n");
- exit(1);
- }
-