summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/libunix/srmdir.c
blob: e143cdf217a8c70901e4c1e70e37fd3947d770aa (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* srmdir.c
   Remove a directory and all its contents.

   Copyright (C) 1992 Ian Lance Taylor

   This file is part of the Taylor UUCP package.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
   */

#include "uucp.h"

#include "uudefs.h"
#include "sysdep.h"
#include "system.h"

#include <errno.h>

#if HAVE_FTW_H
#include <ftw.h>
#endif

static int isremove_dir P((const char *, struct stat *, int));

/* Keep a list of directories to be removed.  */

struct sdirlist
{
  struct sdirlist *qnext;
  char *zdir;
};

static struct sdirlist *qSdirlist;

/* Remove a directory and all files in it.  */

boolean
fsysdep_rmdir (zdir)
     const char *zdir;
{
  boolean fret;
  struct sdirlist *q;

  qSdirlist = NULL;

  fret = TRUE;
  if (ftw ((char *) zdir, isremove_dir, 5) != 0)
    {
      ulog (LOG_ERROR, "ftw: %s", strerror (errno));
      fret = FALSE;
    }

  q = qSdirlist;
  while (q != NULL)
    {
      struct sdirlist *qnext;
      
      if (rmdir (q->zdir) != 0)
	{
	  ulog (LOG_ERROR, "rmdir (%s): %s", q->zdir, strerror (errno));
	  fret = FALSE;
	}
      ubuffree (q->zdir);
      qnext = q->qnext;
      xfree ((pointer) q);
      q = qnext;
    }

  return fret;
}

/* Remove a file in a directory.  */

/*ARGSUSED*/
static int
isremove_dir (zfile, qstat, iflag)
     const char *zfile;
     struct stat *qstat;
     int iflag;
{
  if (iflag == FTW_D || iflag == FTW_DNR)
    {
      struct sdirlist *q;

      q = (struct sdirlist *) xmalloc (sizeof (struct sdirlist));
      q->qnext = qSdirlist;
      q->zdir = zbufcpy (zfile);
      qSdirlist = q;
    }
  else
    {
      if (remove (zfile) != 0)
	ulog (LOG_ERROR, "remove (%s): %s", zfile, strerror (errno));
    }

  return 0;
}
OpenPOWER on IntegriCloud