Exemplo n.º 1
0
/* Searches the filedb for a file matching the specified mask, starting
 * at position 'pos'. The first matching file is returned.
 */
static filedb_entry *_filedb_matchfile(FILE *fdb, long pos, char *match,
                                       char *file, int line)
{
    filedb_entry *fdbe = NULL;

    fseek(fdb, pos, SEEK_SET);
    while (!feof(fdb)) {
        pos = ftell(fdb);
        fdbe = filedb_getfile(fdb, pos, GET_FILENAME);
        if (fdbe) {
            if (!(fdbe->stat & FILE_UNUSED) &&        /* Not unused?         */
                    wild_match_file(match, fdbe->filename)) {     /* Matches our mask?   */
                free_fdbe(&fdbe);
                fdbe = _filedb_getfile(fdb, pos, GET_FULL, file, line); /* Save all data now   */
                return fdbe;
            }
            free_fdbe(&fdbe);
        }
    }
    return NULL;
}
Exemplo n.º 2
0
/* use a where of 0 to start out, then increment 1 space for each next */
static int findmatch(FILE * f, char *lookfor, long *where, filedb * fdb)
{
  char match[256];
  int l;

  strncpy(match, lookfor, 255);
  match[255] = 0;
  /* clip any trailing / */
  l = strlen(match) - 1;
  if (match[l] == '/')
    match[l] = 0;
  fseek(f, *where, SEEK_SET);
  while (!feof(f)) {
    *where = ftell(f);
    fread(fdb, sizeof(filedb), 1, f);
    if (!feof(f)) {
      if (!(fdb->stat & FILE_UNUSED) &&
	  wild_match_file(match, fdb->filename))
	return 1;
    }
  }
  return 0;
}
Exemplo n.º 3
0
/* Outputs a sorted list of files/directories matching the mask,
 * to idx.
 */
static void filedb_ls(FILE *fdb, int idx, char *mask, int showall)
{
    int ok = 0, cnt = 0, is = 0;
    char s1[81], *p = NULL;
    struct flag_record user = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };
    filedb_entry *fdbe = NULL;
    filelist_t *flist = NULL;

    flist = filelist_new();
    filedb_readtop(fdb, NULL);
    fdbe = filedb_getfile(fdb, ftell(fdb), GET_FULL);
    while (fdbe) {
        ok = 1;
        if (fdbe->stat & FILE_UNUSED)
            ok = 0;
        if (ok && (fdbe->stat & FILE_DIR) && fdbe->flags_req) {
            /* Check permissions */
            struct flag_record req = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };

            break_down_flags(fdbe->flags_req, &req, NULL);
            get_user_flagrec(dcc[idx].user, &user, dcc[idx].u.file->chat->con_chan);
            if (!flagrec_ok(&req, &user)) {
                ok = 0;
            }
        }
        if (ok)
            is = 1;
        if (ok && !wild_match_file(mask, fdbe->filename))
            ok = 0;
        if (ok && (fdbe->stat & FILE_HIDDEN) && !(showall))
            ok = 0;
        if (ok) {
            /* Display it! */
            if (cnt == 0) {
                dprintf(idx, FILES_LSHEAD1);
                dprintf(idx, FILES_LSHEAD2);
            }
            filelist_add(flist, fdbe->filename);
            if (fdbe->stat & FILE_DIR) {
                char *s2 = NULL, *s3 = NULL;

                /* Too long? */
                if (strlen(fdbe->filename) > 45) {
                    /* Display the filename on its own line. */
                    s2 = nmalloc(strlen(fdbe->filename) + 3);
                    sprintf(s2, "%s/\n", fdbe->filename);
                    filelist_addout(flist, s2);
                    my_free(s2);
                } else {
                    s2 = nmalloc(strlen(fdbe->filename) + 2);
                    sprintf(s2, "%s/", fdbe->filename);
                }
                /* Note: You have to keep the sprintf and the nmalloc statements
                 *       in sync, i.e. always check that you allocate enough
                 *       memory.
                 */
                if ((fdbe->flags_req) && (user.global &(USER_MASTER | USER_JANITOR))) {
                    s3 = nmalloc(42 + strlen(s2 ? s2 : "") + 6 +
                                 strlen(FILES_REQUIRES) + strlen(fdbe->flags_req) + 1 +
                                 strlen(fdbe->chan ? fdbe->chan : "") + 1);
                    sprintf(s3, "%-30s <DIR%s>  (%s %s%s%s)\n", s2,
                            fdbe->stat & FILE_SHARE ?
                            " SHARE" : "", FILES_REQUIRES, fdbe->flags_req,
                            fdbe->chan ? " " : "", fdbe->chan ? fdbe->chan : "");
                } else {
                    s3 = nmalloc(38 + strlen(s2 ? s2 : ""));
                    sprintf(s3, "%-30s <DIR>\n", s2 ? s2 : "");
                }
                if (s2)
                    my_free(s2);
                filelist_addout(flist, s3);
                my_free(s3);
            } else {
                char s2[41], t[50], *s3 = NULL, *s4;

                s2[0] = 0;
                if (showall) {
                    if (fdbe->stat & FILE_SHARE)
                        strcat(s2, " (shr)");
                    if (fdbe->stat & FILE_HIDDEN)
                        strcat(s2, " (hid)");
                }
                egg_strftime(t, 10, "%d%b%Y", localtime(&fdbe->uploaded));
                if (fdbe->size < 1024)
                    sprintf(s1, "%5d", fdbe->size);
                else
                    sprintf(s1, "%4dk", (int) (fdbe->size / 1024));
                if (fdbe->sharelink)
                    strcpy(s1, "     ");
                /* Too long? */
                if (strlen(fdbe->filename) > 30) {
                    s3 = nmalloc(strlen(fdbe->filename) + 2);
                    sprintf(s3, "%s\n", fdbe->filename);
                    filelist_addout(flist, s3);
                    my_free(s3);
                    /* Causes filename to be displayed on its own line */
                } else
                    malloc_strcpy(s3, fdbe->filename);
                s4 = nmalloc(69 + strlen(s3 ? s3 : "") + strlen(s1) +
                             strlen(fdbe->uploader) + strlen(t) + strlen(s2));
                sprintf(s4, "%-30s %s  %-9s (%s)  %6d%s\n", s3 ? s3 : "", s1,
                        fdbe->uploader, t, fdbe->gots, s2);
                if (s3)
                    my_free(s3);
                filelist_addout(flist, s4);
                my_free(s4);
                if (fdbe->sharelink) {
                    s4 = nmalloc(9 + strlen(fdbe->sharelink));
                    sprintf(s4, "   --> %s\n", fdbe->sharelink);
                    filelist_addout(flist, s4);
                    my_free(s4);
                }
            }
            if (fdbe->desc) {
                p = strchr(fdbe->desc, '\n');
                while (p != NULL) {
                    *p = 0;
                    if ((fdbe->desc)[0]) {
                        char *sd;

                        sd = nmalloc(strlen(fdbe->desc) + 5);
                        sprintf(sd, "   %s\n", fdbe->desc);
                        filelist_addout(flist, sd);
                        my_free(sd);
                    }
                    strcpy(fdbe->desc, p + 1);
                    p = strchr(fdbe->desc, '\n');
                }
                if ((fdbe->desc)[0]) {
                    char *sd;

                    sd = nmalloc(strlen(fdbe->desc) + 5);
                    sprintf(sd, "   %s\n", fdbe->desc);
                    filelist_addout(flist, sd);
                    my_free(sd);
                }
            }
            cnt++;
        }
        free_fdbe(&fdbe);
        fdbe = filedb_getfile(fdb, ftell(fdb), GET_FULL);
    }
    if (is == 0)
        dprintf(idx, FILES_NOFILES);
    else if (cnt == 0)
        dprintf(idx, FILES_NOMATCH);
    else {
        filelist_sort(flist);
        filelist_idxshow(flist, idx);
        dprintf(idx, "--- %d file%s.\n", cnt, cnt != 1 ? "s" : "");
    }
    filelist_free(flist);
}
Exemplo n.º 4
0
static void filedb_ls(FILE *f, int idx, char *mask, int showall)
{
  filedb fdb;
  int ok = 0, cnt = 0, is = 0;
  char s[81], s1[81], *p;
  struct flag_record user = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};

  rewind(f);
  while (!feof(f)) {
    fread(&fdb, sizeof(filedb), 1, f);
    if (!feof(f)) {
      ok = 1;
      if (fdb.stat & FILE_UNUSED)
	ok = 0;
      if (fdb.stat & FILE_DIR) {
	/* check permissions */
	struct flag_record req = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};

	break_down_flags(fdb.flags_req, &req, NULL);
	get_user_flagrec(dcc[idx].user, &user,
			 dcc[idx].u.file->chat->con_chan);
	if (!flagrec_ok(&req, &user))
	  ok = 0;
      }
      if (ok)
	is = 1;
      if (!wild_match_file(mask, fdb.filename))
	ok = 0;
      if ((fdb.stat & FILE_HIDDEN) && !(showall))
	ok = 0;
      if (ok) {
	/* display it! */
	if (cnt == 0) {
	  dprintf(idx, FILES_LSHEAD1);
	  dprintf(idx, FILES_LSHEAD2);
	}
	if (fdb.stat & FILE_DIR) {
	  char s2[50];

	  /* too long? */
	  if (strlen(fdb.filename) > 45) {
	    dprintf(idx, "%s/\n", fdb.filename);
	    s2[0] = 0;
	    /* causes filename to be displayed on its own line */
	  } else
	    sprintf(s2, "%s/", fdb.filename);
	  if ((fdb.flags_req[0]) &&
	      (user.global &(USER_MASTER | USER_JANITOR))) {
	    dprintf(idx, "%-30s <DIR%s>  (%s %s%s%s)\n", s2,
		    fdb.stat & FILE_SHARE ?
		    " SHARE" : "", FILES_REQUIRES, fdb.flags_req,
		    fdb.chname[0] ? " " : "", fdb.chname);
	  } else
	    dprintf(idx, "%-30s <DIR>\n", s2);
	} else {
	  char s2[41];

	  s2[0] = 0;
	  if (showall) {
	    if (fdb.stat & FILE_SHARE)
	      strcat(s2, " (shr)");
	    if (fdb.stat & FILE_HIDDEN)
	      strcat(s2, " (hid)");
	  }
	  strcpy(s, ctime(&fdb.uploaded));
	  s[10] = 0;
	  s[7] = 0;
	  s[24] = 0;
	  strcpy(s, &s[8]);
	  strcpy(&s[2], &s[4]);
	  strcpy(&s[5], &s[22]);
	  if (fdb.size < 1024)
	    sprintf(s1, "%5d", fdb.size);
	  else
	    sprintf(s1, "%4dk", (int) (fdb.size / 1024));
	  if (fdb.sharelink[0])
	    strcpy(s1, "     ");
	  /* too long? */
	  if (strlen(fdb.filename) > 30) {
	    dprintf(idx, "%s\n", fdb.filename);
	    fdb.filename[0] = 0;
	    /* causes filename to be displayed on its own line */
	  }
	  dprintf(idx, "%-30s %s  %-9s (%s)  %6d%s\n", fdb.filename, s1,
		  fdb.uploader, s, fdb.gots, s2);
	  if (fdb.sharelink[0]) {
	    dprintf(idx, "   --> %s\n", fdb.sharelink);
	  }
	}
	if (fdb.desc[0]) {
	  p = strchr(fdb.desc, '\n');
	  while (p != NULL) {
	    *p = 0;
	    if (fdb.desc[0])
	      dprintf(idx, "   %s\n", fdb.desc);
	    strcpy(fdb.desc, p + 1);
	    p = strchr(fdb.desc, '\n');
	  }
	  if (fdb.desc[0])
	    dprintf(idx, "   %s\n", fdb.desc);
	}
	cnt++;
      }
    }
  }
  if (is == 0)
    dprintf(idx, FILES_NOFILES);
  else if (cnt == 0)
    dprintf(idx, FILES_NOMATCH);
  else
    dprintf(idx, "--- %d file%s.\n", cnt, cnt > 1 ? "s" : "");
}