-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkln.c
192 lines (163 loc) · 4.76 KB
/
mkln.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
//
// Created by paul on 6/26/20.
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <linux/limits.h>
char * gInvokedAs;
#if 0
const char * typeStrMap[] =
{
[S_IFBLK] = "block device",
[S_IFCHR] = "char device",
[S_IFDIR] = "directory",
[S_IFIFO] = "pipe",
[S_IFLNK] = "symlink",
[S_IFREG] = "file",
[S_IFSOCK] = "socket"
};
#endif
int usage( void )
{
fprintf( stderr, "Usage: %s <original> <target>\n", gInvokedAs );
return -2;
}
int printErrno( char * format, ...)
{
int errorNumber = errno;
char msgBuf[2048];
va_list args;
va_start( args, format );
vsnprintf( msgBuf, sizeof(msgBuf), format, args );
fprintf( stderr, "%s: %s (%d: %s)\n", gInvokedAs, msgBuf, errorNumber, strerror(errorNumber) );
va_end( args );
return errorNumber;
}
int createPath( char * path, int mode )
{
char * parent;
struct stat here;
if ( stat( path, &here ) == -1 )
{
switch (errno)
{
case ENOENT:
parent = strdup(path);
if ( createPath( dirname( parent ), mode ) == 0 )
{
if ( mkdir( path, mode ) == -1 )
{
return printErrno( "unable to create directory \"%s\"", path );
}
}
free( parent );
break;
default:
printErrno("stat returned ");
break;
}
}
return 0;
}
int main( int argc, char *argv[] )
{
struct stat originalInfo, targetInfo;
char * original;
char * target;
gInvokedAs = strdup(basename(argv[0]) );
if ( argc != 3 )
{
return usage();
}
original = strdup( argv[1] );
target = strdup( argv[2] );
if ( stat( original, &originalInfo ) == -1 )
{
switch (errno)
{
default:
return printErrno( "unable to get information about \"%s\"", original );
}
}
switch ( originalInfo.st_mode & S_IFMT )
{
case S_IFREG:
break;
default:
fprintf( stderr, "%s: original file \"%s\"must be a regular file\n", gInvokedAs, original );
return -99;
}
if ( stat( target, &targetInfo ) == -1 )
{
int mode;
switch ( errno )
{
case ENOENT:
mode = originalInfo.st_mode;
/* since we're copying a file's perms to directories,
* mirror each 'read' perm to the 'search' perm */
mode |= (mode & (S_IRUSR | S_IRGRP | S_IROTH )) >> 2;
/* dirname modifies its parameter, so dup it first */
char * path = strdup( target );
if (createPath( dirname( path ), mode ) == 0)
{
if ( link(original, target ) == -1 )
{
return printErrno( "linking \"%s\" to \"%s\" failed", original, target );
}
}
free(path);
break;
default:
return printErrno( "unable to get information about target \"%s\"", target );
}
}
else
{
#if 0
fprintf( stderr, "%s: target \"%s\" is an existing %s\n",
gInvokedAs, target, typeStrMap[targetInfo.st_mode & S_IFMT] );
#endif
char * ext = "";
char * lastPeriod = strrchr( target, '.');
if ( lastPeriod != NULL && strlen( lastPeriod ) > 5 )
{ /* what we found is too long to be a 'proper' extension */
lastPeriod = NULL;
}
if ( lastPeriod != NULL)
{
ext = strdup( lastPeriod ); /* remember the extension (including period) */
*lastPeriod = '\0'; /* lop off the extension */
}
int i = 2;
do {
char newTarget[PATH_MAX];
snprintf( newTarget, sizeof( newTarget ), "%s (%d)%s", target, i, ext );
if ( stat( newTarget, &targetInfo ) == -1 )
{
switch (errno)
{
case ENOENT: /* success - we found a path that doesn't already exist */
if ( link( original, newTarget ) == -1 )
{
return printErrno( "linking \"%s\" to \"%s\" failed", original, newTarget );
}
return 0;
default:
return printErrno( "unable to get information about new target \"%s\"", newTarget );
}
}
/* else we got info, something already exits, try again */
} while ( ++i < 100 );
fprintf( stderr, "%s: can't find a target name that doesn't already exist.", gInvokedAs );
return 99;
}
return 0;
}