File: purge

package info (click to toggle)
slang2 2.3.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,488 kB
  • sloc: ansic: 101,756; sh: 3,435; makefile: 1,046; pascal: 440
file content (65 lines) | stat: -rwxr-xr-x 1,308 bytes parent folder | download | duplicates (7)
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
#! /usr/bin/env slsh
% -*- mode: slang -*-
_debug_info = 1;

private define purge_file (file, age, print_option)
{
   variable st = stat_file (file);
   if (st == NULL)
     {
	() = fprintf (stderr, "stat %s failed: %s\n", file, errno_string (errno));
	return;
     }

   if (st.st_ctime >= age)
     return;

   if (print_option)
     {
	() = fprintf (stdout, "%s\n", file);
	return;
     }

   if (-1 == remove (file))
     () = fprintf (stderr, "remove %s failed: %s\n", file, errno_string (errno));
}

private define purge_usage ()
{
   () = fprintf (stderr, "Usage: %s [-n] NUM-DAYS-OLD files...\n", __argv[0]);
   () = fprintf (stderr, "  Files older than NUM-DAYS-OLD be deleted.\n");
   () = fprintf (stderr, "  -n ==> Just print the files to be removed but do not remove them.\n");
   exit (1);
}

private define main (argc, argv)
{
   variable age, i, print_option, file;

   if (argc < 3) purge_usage ();

   i = 2;
   print_option = 0;
   if (argv[1] == "-n")
     {
	i++;
	print_option = 1;
	if (argc < 4)
	  purge_usage ();
     }

   age = __argv[i-1];
   if (String_Type == _slang_guess_type (age))
     purge_usage ();

   age = _time() - atof(age) * 24 * 3600;

   foreach (argv[[i:]])
     {
	file = ();
	purge_file (file, age, print_option);
     }
   exit (0);
}

main (__argc, __argv);