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