-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfname.c
110 lines (105 loc) · 2.85 KB
/
fname.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
/*
* fname.c
*
* Copyright 2019 Ryan Koehler, VerdAscend Sciences, ryan@verdascend.com
*
* The programs and source code of the vertools collection are free software.
* They are distributed in the hope that they will be useful,
* WITHOUT ANY WARRANTY OF FITNESS FOR ANY PARTICULAR PURPOSE.
*
* Permission is granted for research, educational, and possibly commercial use
* and modification as long as 1) Code and any derived works are not
* redistributed for any fee, and 2) Proper credit is given to the authors.
* If you wish to include this software in a product, or use it commercially,
* please contact the authors.
*
* See https://www.verdascend.com/ for more
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define __MAIN__
#include "prim.h"
#define VERSION_S "Fname Version 0.32"
int main(int argc, char **argv);
void FnameUse(void);
int FnameI(int argc, char **argv);
/**************************************************************************/
int main(int argc, char **argv)
{ Init(argc,argv); exit( AllDoneI(FnameI(argc,argv),NULL) ); }
/**************************************************************************/
void FnameUse()
{
VersionSplash(NULL,VERSION_S,"# ",TRUE);
printf("Usage: <filename> [...options]\n");
printf(" <infile> Is a filename\n");
printf(" -p Print path\n");
printf(" -b Print base\n");
printf(" -e Print extension\n");
printf(" -l Length of filename component\n");
printf("Options may be combined\n");
}
/**************************************************************************/
int FnameI(int argc, char **argv)
{
char pathS[BBUFF_SIZE], baseS[BBUFF_SIZE], extS[BBUFF_SIZE];
char inS[BBUFF_SIZE], outS[BBUFF_SIZE];
int i,p,b,e,l;
p=b=e=l=FALSE;
if(!ParseArgsI(argc,argv,"S -p B -b B -e B -l B",
inS,&p,&b,&e,&l,
(int *)NULL))
{
FnameUse();
return(FALSE);
}
/***
* If path and extension, need base too
*/
if(p && e)
{
b = TRUE;
}
/***
* Get parts into substrings then put together the parts
*/
GetFilePartsI(inS,pathS,baseS,extS);
/*
printf("path |%s|\n",pathS);
printf("base |%s|\n",baseS);
printf("ext |%s|\n",extS);
*/
INIT_S(outS);
i = 0;
if(p)
{
strcat(outS,pathS); i++;
}
if( (b) && (!NO_S(baseS)) )
{
if( (p) && (!NO_S(pathS)) )
{
if( pathS[strlen(pathS)-1]!='/' )
strcat(outS,"/");
}
strcat(outS,baseS); i++;
}
if( (e) && (!NO_S(extS)) )
{
if( (b) && (!NO_S(baseS)) )
{
strcat(outS,".");
}
strcat(outS,extS); i++;
}
if(l)
{
printf("%d\n",INT(strlen(outS)));
}
else if(i)
{
printf("%s\n",outS);
}
return(TRUE);
}