```typescript
/** Prune a file listing with ordered drop and keep rules. */

function segmentFits(pattern: string, text: string): boolean {
  let p = 0;
  let t = 0;
  let star = -1;
  let mark = 0;
  while (t < text.length) {
    if (p < pattern.length && pattern[p] === "*") {
      star = p;
      mark = t;
      p += 1;
    } else if (p < pattern.length && pattern[p] === text[t]) {
      p += 1;
      t += 1;
    } else if (star >= 0) {
      p = star + 1;
      mark += 1;
      t = mark;
    } else {
      return false;
    }
  }
  while (p < pattern.length && pattern[p] === "*") {
    p += 1;
  }
  return p === pattern.length;
}

function splitStrict(text: string, what: string): string[] {
  if (typeof text !== "string" || text === "") {
    throw new Error(`${what} must be a non-empty string`);
  }
  const segments = text.split("/");
  if (segments.some((segment) => segment === "")) {
    throw new Error(`${what} has an empty segment: ${text}`);
  }
  return segments;
}

export function pruneListing(listing: string[], rules: string[]): string[] {
  // Validate every rule and path with splitStrict, then keep each
  // path the last matching rule allows; a path no rule matches survives.
  if (listing.some((path) => !splitStrict(path, "path"))) return [];
  if (rules.some((rule) => !splitStrict(rule, "rule"))) return [];

  const ruleMap = rules.reduce<Record<string, boolean>>((acc, rule) => {
    const isKeepRule = rule.startsWith("!");
    acc[rule] = isKeepRule;
    return acc;
  }, {});

  return listing.filter((path) => {
    let matchFound = false;
    for (const [pattern, isKeep] of Object.entries(ruleMap)) {
      if (segmentFits(pattern, path)) {
        matchFound = isKeep;
        break;
      }
    }
    return matchFound;
  });
}
```