.
This commit is contained in:
commit
aae369b9c8
9 changed files with 334 additions and 0 deletions
95
pipeshare.c
Normal file
95
pipeshare.c
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#define _XOPEN_SOURCE 500
|
||||
#include <libowfat/errmsg.h>
|
||||
#include <libowfat/str.h>
|
||||
#include <libowfat/buffer.h>
|
||||
#include <unistd.h>
|
||||
#include <alloca.h>
|
||||
#include <libgen.h>
|
||||
|
||||
#include "pipeshare.h"
|
||||
|
||||
#define dieusage() die(100,argv0," [-C dir]")
|
||||
#define dienoarg(OPT) \
|
||||
do { \
|
||||
carp("missing argument for -"OPT); \
|
||||
dieusage(); \
|
||||
} while(0)
|
||||
|
||||
static char *wwwbase=NULL;
|
||||
|
||||
static int mkaliasfile(char *id,char ext[16])
|
||||
{
|
||||
char *alias;
|
||||
size_t l;
|
||||
alias=alloca(str_len(id)+17);
|
||||
l=str_copy(alias,id);
|
||||
alias[l++]='.';
|
||||
l+=str_copy(alias+l,ext);
|
||||
alias[l]=0;
|
||||
if(symlink(id,alias)<0) {
|
||||
carpsys("symlink ",id," -> ",alias);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
char *dir=".";
|
||||
char tmp[]="./XXXXXX";
|
||||
int tmpfd;
|
||||
char ext[16];
|
||||
errmsg_iam(*argv);
|
||||
for (i=1;i<argc;i++) {
|
||||
if (argv[i][0]!='-') break;
|
||||
if (argv[i][1]=='-' && !argv[i][2]) break;
|
||||
switch (argv[i][1]) {
|
||||
case 'C':
|
||||
if (!argv[++i]) dienoarg("C");
|
||||
dir=argv[i];
|
||||
break;
|
||||
default: dieusage();
|
||||
}
|
||||
}
|
||||
argc-=i; argv+=i;
|
||||
if (argc!=0) dieusage();
|
||||
wwwbase=getenv("WWWBASE");
|
||||
if (!wwwbase) die(100,"WWWBASE not set");
|
||||
|
||||
if (chdir(dir)<0)
|
||||
diesys(111,"chdir ",dir);
|
||||
|
||||
tmpfd=mkstemp(tmp);
|
||||
if(tmpfd<0) diesys(111, "mkstemp in ", argv[0]);
|
||||
if(!io_fd(tmpfd)
|
||||
||!io_fd(0)
|
||||
) {
|
||||
unlink(tmp);
|
||||
diesys(111,"io_fd");
|
||||
}
|
||||
|
||||
if(!send_stdin(tmpfd)) {
|
||||
unlink(tmp);
|
||||
die(111,"copy stdin to ",tmp);
|
||||
}
|
||||
|
||||
lseek(tmpfd,0,SEEK_SET);
|
||||
if(!extension(tmpfd,ext)) {
|
||||
carp("determine extension");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(!mkaliasfile(tmp,ext)) {
|
||||
unlink(tmp);
|
||||
die(111,"create alias with extension");
|
||||
}
|
||||
|
||||
buffer_puts(buffer_1,wwwbase);
|
||||
buffer_puts(buffer_1,"/");
|
||||
buffer_puts(buffer_1,basename(tmp));
|
||||
buffer_puts(buffer_1,".");
|
||||
buffer_puts(buffer_1,ext);
|
||||
buffer_putsflush(buffer_1,"\n");
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue