1#! /usr/bin/env slsh
2% -*- mode: slang -*-
3_debug_info = 1;
4
5static define purge_file (file, age, print_option)
6{
7   variable st = stat_file (file);
8   if (st == NULL)
9     {
10	() = fprintf (stderr, "stat %s failed: %s\n", file, errno_string (errno));
11	return;
12     }
13
14   if (st.st_ctime >= age)
15     return;
16
17   if (print_option)
18     {
19	() = fprintf (stdout, "%s\n", file);
20	return;
21     }
22
23   if (-1 == remove (file))
24     () = fprintf (stderr, "remove %s failed: %s\n", file, errno_string (errno));
25}
26
27static define purge_usage ()
28{
29   () = fprintf (stderr, "Usage: %s [-n] NUM-DAYS-OLD files...\n", __argv[0]);
30   () = fprintf (stderr, "  Files older than NUM-DAYS-OLD be deleted.\n");
31   () = fprintf (stderr, "  -n ==> Just print the files to be removed but do not remove them.\n");
32   exit (1);
33}
34
35static define main (argc, argv)
36{
37   variable age, i, print_option, file;
38
39   if (argc < 3) purge_usage ();
40
41   i = 2;
42   print_option = 0;
43   if (argv[1] == "-n")
44     {
45	i++;
46	print_option = 1;
47	if (argc < 4)
48	  purge_usage ();
49     }
50
51   age = __argv[i-1];
52   if (String_Type == _slang_guess_type (age))
53     purge_usage ();
54
55   age = _time() - atof(age) * 24 * 3600;
56
57   foreach (argv[[i:]])
58     {
59	file = ();
60	purge_file (file, age, print_option);
61     }
62   exit (0);
63}
64
65main (__argc, __argv);
66