xref: /openbsd-src/usr.bin/ftp/list.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*
2  * Copyright (c) 2008 Martynas Venckus <martynas@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifndef SMALL
18 #include <string.h>
19 
20 void
21 parse_unix(char **line, char *type)
22 {
23 	char *tok;
24 	int field = 0;
25 
26 	while ((tok = strsep(line, " \t")) != NULL) {
27 		if (*tok == '\0')
28 			continue;
29 
30 		if (field == 0)
31 			*type = *tok;
32 
33 		if (field == 7) {
34 			while (**line == ' ' || **line == '\t')
35 				(*line)++;
36 			break;
37 		}
38 
39 		field++;
40 	}
41 }
42 
43 void
44 parse_windows(char **line, char *type)
45 {
46 	char *tok;
47 	int field = 0;
48 
49 	*type = '-';
50 	while ((tok = strsep(line, " \t")) != NULL) {
51 		if (*tok == '\0')
52 			continue;
53 
54 		if (field == 2 && strcmp(tok, "<DIR>") == 0)
55 			*type = 'd';
56 
57 		if (field == 2) {
58 			while (**line == ' ' || **line == '\t')
59 				(*line)++;
60 			break;
61 		}
62 
63 		field++;
64 	}
65 }
66 
67 void
68 parse_list(char **line, char *type)
69 {
70 	if (**line >= '0' && **line <= '9')
71 		return parse_windows(line, type);
72 
73 	return parse_unix(line, type);
74 }
75 
76 #endif /* !SMALL */
77