File: filename.c

package info (click to toggle)
wcstools 3.9.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,684 kB
  • sloc: ansic: 113,336; sh: 553; makefile: 245; lisp: 86; sed: 1
file content (202 lines) | stat: -rw-r--r-- 4,974 bytes parent folder | download | duplicates (2)
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
/* File filename.c
 * January 16, 2021
 * By Jessica Mink, Harvard-Smithsonian Center for Astrophysics
 * Send bug reports to jmink@cfa.harvard.edu

   Copyright (C) 2006-2021
   Smithsonian Astrophysical Observatory, Cambridge, MA USA

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

static int verbose = 0;         /* Verbose/debugging flag */
static int getroot = 0;         /* Return file root */
static int nslash = 1;		/* Start this many slashes from end of name */
static int keepdir = 0;		/* Return directory(ies) as part of name */
static int nonl = 0;		/* Output null at end of string for scripting */
static int newline = 0;		/* Output linefeed at end of string for listing */
static void usage();

int
main (ac, av)
int ac;
char **av;
{
    char *fn, *fn0;
    char *str;
    char *name;
    char *is[10];
    char *endroot;
    int i, n;

    /* crack arguments */
    for (av++; --ac > 0 && *(str = *av) == '-'; av++) {
        char c;
        while ((c = *++str))
        switch (c) {

        case '/':       /* Keep one more directory in path */
            nslash++;
            break;

        case 'e':       /* Append endline after each file name */
            newline++;
            break;

        case 'n':       /* Prepend directory to name with . */
            keepdir++;
	    nslash++;
            break;

        case 'r':       /* Return root of filename */
            getroot++;
            break;

        case 'v':       /* More verbosity */
            verbose++;
            break;

        default:
            usage();
            break;
        }
    }

    /* There are ac remaining file names starting at av[0] */
    if (ac == 0)
        usage ();

    if (nslash > 10)
	nslash = 10;

    while (ac-- > 0) {
	fn0 = *av++;
	fn = fn0;

	for (n = 0; n < 10; n++)
	    is[n] = NULL;
	if (verbose)
    	    printf ("%s -> ", fn);
	endroot = NULL;
	for (n = 0; n < nslash; n++) {
	    name = strrchr (fn, '/');
	    if (n == 0 && getroot) {
		if (name != NULL)
		    endroot = strrchr (name, '.');
		else
		    endroot = strrchr (fn, '.');
		if (endroot != NULL) {
		    *endroot = (char) 0;
		    if (getroot > 1) {
			if (name != NULL)
			    endroot = strrchr (name, '.');
			else
			    endroot = strrchr (fn, '.');
			if (endroot != NULL)
			    *endroot = (char) 0;
			}
		    }
		}
	    if (name == NULL) {
		name = fn;
		break;
		}
	    else {
		if (name > fn0) {
		    is[n] = name;
		    if (keepdir)
			*name = '.';
		    else
			*name = (char) 0;
		    }
		else
		    break;
		name = name + 1;
		if (verbose) {
		    for (i = 0; i < nslash; i++) {
			if (is[i] != NULL) {
			    if (keepdir)
			 	*(is[i]) = '.';
			    else
			 	*(is[i]) = '/';
			    }
			}
		    printf ("%s", name);
		    for (i = 0; i < nslash; i++) {
			if (is[i] != NULL) {
			    if (keepdir)
				*(is[i]) = '.';
			    else
				*(is[i]) = (char) 0;
			    }
			}
		    }
		}
	    }

	for (n = 0; n < nslash; n++) {
	    if (is[n] != NULL) {
		if (keepdir)
		    *(is[n]) = '.';
		else
		    *(is[n]) = '/';
		}
	    }

	printf ("%s", name);
	if (newline)
	    printf ("\n");
	}
    if (nonl)
	printf ("%c",(char)0);
    else
	printf ("\n");

    return (0);
}

static void
usage ()
{
    fprintf (stderr,"FILENAME: Drop directory from pathname\n");
    fprintf(stderr,"Usage:  filename [-v/] path1 path2 path3 ...\n");
    fprintf(stderr,"  -/: Keep one more end directory for each /\n");
    fprintf(stderr,"  -e: Append endline after each file name\n");
    fprintf(stderr,"  -n: Prepend one more end directory with .\n");
    fprintf(stderr,"  -r: Root of file name (before first .)\n");
    fprintf(stderr,"  -v: Verbose\n");
    fprintf(stderr,"  -z: Add null instead of nl to output for scripting\n");
    exit (1);
}
/* Aug  3 1998	New program
 *
 * Jun 21 2006	Clean up code
 *
 * Jun  1 2007	Add option to keep directories up from file
 * Jun 11 2007	Add -r option to keep only root of file name (before first .)
 *
 * Feb  6 2008	Add -n option to keep directory with "."
 * Feb  6 2008	Allow more than one -r command
 *
 * Aug 18 2020	Add -z option to end name with null instead of newline
 *
 * Jan 16 2020	Add -e option to append newline after each output string
 */