~martin-decky/helenos/rcu

« back to all changes in this revision

Viewing changes to uspace/app/bdsh/cmds/modules/rm/rm.c

  • Committer: Vojtech Horky
  • Date: 2012-05-23 12:03:26 UTC
  • mfrom: (1443.1.19 misc)
  • mto: This revision was merged to the branch mainline in revision 1479.
  • Revision ID: vojtechhorky@users.sourceforge.net-20120523120326-jv50stjymxmh598s
Merge GSOC-originated patches 

Merge from lp:~vojtech-horky/helenos/misc.

The merge includes:
 * Switching to previous directory with `cd -' in Bdsh
 * Interactive mode for cp module in Bdsh
 * Implementation of sleep command
 * printf and echo for Bdsh
 * Rewrite of the mkdir module
 * Ctrl-arrow jumps over words in the editor
 * The scripts work with Python 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
static const char *cmdname = "rm";
46
46
#define RM_VERSION "0.0.1"
47
47
 
48
 
static rm_job_t rm;
49
 
 
50
48
static struct option const long_options[] = {
51
49
        { "help", no_argument, 0, 'h' },
52
50
        { "version", no_argument, 0, 'v' },
56
54
        { 0, 0, 0, 0 }
57
55
};
58
56
 
 
57
/* Return values for rm_scope() */
 
58
#define RM_BOGUS 0
 
59
#define RM_FILE  1
 
60
#define RM_DIR   2
 
61
 
 
62
/* Flags for rm_update() */
 
63
#define _RM_ENTRY   0
 
64
#define _RM_ADVANCE 1
 
65
#define _RM_REWIND  2
 
66
#define _RM_EXIT    3
 
67
 
 
68
/* A simple job structure */
 
69
typedef struct {
 
70
        /* Options set at run time */
 
71
        unsigned int force;      /* -f option */
 
72
        unsigned int recursive;  /* -r option */
 
73
        unsigned int safe;       /* -s option */
 
74
 
 
75
        /* Keeps track of the job in progress */
 
76
        int advance; /* How far deep we've gone since entering */
 
77
        DIR *entry;  /* Entry point to the tree being removed */
 
78
        char *owd;   /* Where we were when we invoked rm */
 
79
        char *cwd;   /* Current directory being transversed */
 
80
        char *nwd;   /* Next directory to be transversed */
 
81
 
 
82
        /* Counters */
 
83
        int f_removed; /* Number of files unlinked */
 
84
        int d_removed; /* Number of directories unlinked */
 
85
} rm_job_t;
 
86
 
 
87
static rm_job_t rm;
 
88
 
 
89
static unsigned int rm_recursive(const char *);
 
90
 
59
91
static unsigned int rm_start(rm_job_t *rm)
60
92
{
61
93
        rm->recursive = 0;
96
128
                free(rm->cwd);
97
129
}
98
130
 
 
131
static unsigned int rm_single(const char *path)
 
132
{
 
133
        if (unlink(path)) {
 
134
                cli_error(CL_EFAIL, "rm: could not remove file %s", path);
 
135
                return 1;
 
136
        }
 
137
        return 0;
 
138
}
 
139
 
 
140
static unsigned int rm_scope(const char *path)
 
141
{
 
142
        int fd;
 
143
        DIR *dirp;
 
144
 
 
145
        dirp = opendir(path);
 
146
        if (dirp) {
 
147
                closedir(dirp);
 
148
                return RM_DIR;
 
149
        }
 
150
 
 
151
        fd = open(path, O_RDONLY);
 
152
        if (fd > 0) {
 
153
                close(fd);
 
154
                return RM_FILE;
 
155
        }
 
156
 
 
157
        return RM_BOGUS;
 
158
}
 
159
 
99
160
static unsigned int rm_recursive_not_empty_dirs(const char *path)
100
161
{
101
162
        DIR *dirp;
155
216
        return ret + 1;
156
217
}
157
218
 
158
 
static unsigned int rm_single(const char *path)
159
 
{
160
 
        if (unlink(path)) {
161
 
                cli_error(CL_EFAIL, "rm: could not remove file %s", path);
162
 
                return 1;
163
 
        }
164
 
        return 0;
165
 
}
166
 
 
167
 
static unsigned int rm_scope(const char *path)
168
 
{
169
 
        int fd;
170
 
        DIR *dirp;
171
 
 
172
 
        dirp = opendir(path);
173
 
        if (dirp) {
174
 
                closedir(dirp);
175
 
                return RM_DIR;
176
 
        }
177
 
 
178
 
        fd = open(path, O_RDONLY);
179
 
        if (fd > 0) {
180
 
                close(fd);
181
 
                return RM_FILE;
182
 
        }
183
 
 
184
 
        return RM_BOGUS;
185
 
}
186
 
 
187
219
/* Dispays help for rm in various levels */
188
220
void help_cmd_rm(unsigned int level)
189
221
{