-
Notifications
You must be signed in to change notification settings - Fork 0
/
edln.c
219 lines (193 loc) · 6.47 KB
/
edln.c
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
** edln - a simple readline-based utility for interactively editing the
** target of a symbolic link.
**
** Author: Jeremy Lin <[email protected]>
**
** Placed in the public domain.
*/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <readline/readline.h>
/*
** This length corresponds to PATH_MAX on Linux (which has one of the larger
** PATH_MAX values around). This length includes the NUL terminator.
*/
#define BUFLEN 4096
/* Stores the original symlink target. */
char orig_target[BUFLEN];
/* Stores the original working directory. */
char orig_wd[BUFLEN];
int init_rl_line_buffer(void)
{
rl_replace_line(orig_target, 0);
rl_point = rl_end; /* Move cursor to end of line. */
rl_redisplay();
return 0;
}
char* edln_rl_filename_completion_function(const char* text, int state)
{
/*
** N.B. There are two ways to make this space-suppression customization.
** The original method is to set 'rl_completion_append_character' to the
** NUL character. The alternate method, which was introduced in Readline
** 4.3, is to set 'rl_completion_suppress_append' to a nonzero value.
** Digging through the Readline source code reveals that, surprisingly,
** 'rl_completion_suppress_append' has always been completely redundant.
** There was a time when it seemed to have been intended to also suppress
** appending a '/' character to directories, but this was never actually
** put into effect.
**
** In any case, Readline resets both variables to their default values
** before every call to the completion function, hence the need for this
** wrapper function, which sets one of them back to our desired value.
*/
rl_completion_append_character = '\0';
/* Call out to Readline's built-in filename completion function. */
return rl_filename_completion_function(text, state);
}
void customize_rl_completion_behavior(void)
{
/*
** Don't break on any of the typical word break characters. This would
** cause filename completion to fail on filenames that contain spaces or
** other "unusual" characters.
*/
rl_completer_word_break_characters = "";
/*
** Don't append a space character to a fully-completed filename.
** This would just result in a broken symlink.
**
** This customization requires use of a custom completion function.
*/
rl_completion_entry_function = &edln_rl_filename_completion_function;
}
/*
** Change to the directory containing the symlink, so that filename
** completions are performed relative to that directory.
*/
void change_to_symlink_dir(char* link_path)
{
/* Find the last slash in the symlink path, if any. */
int i;
for (i = strlen(link_path) - 1; i > 0; --i) {
if (link_path[i] == '/') {
link_path[i] = '\0'; /* Temporarily truncate at the slash. */
if (chdir(link_path) < 0) {
fprintf(stderr, "Warning: Couldn't chdir() to '%s': ", link_path);
perror(NULL);
}
link_path[i] = '/'; /* Restore the slash. */
break;
}
}
}
/*
** Remove extraneous trailing slash ('/') characters in-place:
**
** "foo///" => "foo", but "///" => "/"
*/
void remove_trailing_slashes(char* path)
{
char* last_char = path + strlen(path) - 1;
while (last_char > path && *last_char == '/') {
*last_char-- = '\0';
}
}
void fatal_error(const char* fmt, ...)
{
char msg[BUFLEN];
va_list ap;
if (fmt) {
/* Format and print the error message. */
va_start(ap, fmt);
vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
fprintf(stderr, "%s\n", msg);
} else {
perror(NULL);
}
exit(EXIT_FAILURE);
}
int main(int argc, char** argv)
{
char* link_path = NULL;
char* new_target = NULL;
switch (argc) {
case 3:
new_target = argv[2];
/* FALL THROUGH */
case 2:
link_path = argv[1];
break;
default:
fprintf(stderr, "usage: %s symlink_to_edit [new_target]\n", argv[0]);
return EXIT_SUCCESS;
}
/*
** If there is a trailing slash in the symlink path, remove it.
**
** When performing filename completion on a symlink, shells often add a
** trailing slash when the symlink points to a directory. However, this
** trailing slash will cause readlink() to fail.
*/
remove_trailing_slashes(link_path);
/* Read the original symlink target. 'orig_target' is already zero-filled. */
if (readlink(link_path, orig_target, sizeof(orig_target)-1) < 0) {
/* Print less-cryptic messages for the most common errors. */
switch (errno) {
case ENOENT:
fatal_error("'%s' does not exist.", link_path);
break;
case EINVAL:
fatal_error("'%s' is not a symlink.", link_path);
break;
default:
fatal_error(NULL); /* Behave like perror(). */
break;
}
}
if (new_target == NULL) {
if (!getcwd(orig_wd, sizeof(orig_wd))) {
fatal_error("getcwd() failed (buffer too small).");
}
change_to_symlink_dir(link_path);
customize_rl_completion_behavior();
rl_extend_line_buffer(BUFLEN+1);
rl_pre_input_hook = &init_rl_line_buffer;
new_target = readline("New target: ");
if (new_target == NULL)
fatal_error("No input received, aborting.");
/*
** Remove trailing slash(es) from the target. This typically occurs
** when Readline completes a directory name, but if the existing
** target had trailing slashes, they will be removed as well.
*/
remove_trailing_slashes(new_target);
if (!strcmp(new_target, orig_target)) {
/* New target same as old target, so don't make any changes. */
exit(EXIT_SUCCESS);
} else if (*new_target == '\0') {
fatal_error("Can't symlink to an empty target.");
}
}
/*
** Change back to the original working directory so that unlink() executes
** relative to that.
*/
if (chdir(orig_wd) < 0) {
fprintf(stderr, "Warning: Couldn't chdir() back to '%s': ", orig_wd);
perror(NULL);
}
/* Remove the original symlink. */
if (unlink(link_path) < 0)
fatal_error(NULL);
/* Write out the updated symlink. */
if (symlink(new_target, link_path) < 0)
fatal_error(NULL);
return EXIT_SUCCESS;
}