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