xref: /openbsd/usr.bin/ftp/list.c (revision bfd817ad)
1 /*	$OpenBSD: list.c,v 1.9 2019/05/16 12:44:18 florian 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	parse_list(char **, char *);
24 
25 static void
parse_unix(char ** line,char * type)26 parse_unix(char **line, char *type)
27 {
28 	char *tok;
29 	int field = 0;
30 
31 	while ((tok = strsep(line, " \t")) != NULL) {
32 		if (*tok == '\0')
33 			continue;
34 
35 		if (field == 0)
36 			*type = *tok;
37 
38 		if (field == 7) {
39 			if (line == NULL || *line == NULL)
40 				break;
41 			while (**line == ' ' || **line == '\t')
42 				(*line)++;
43 			break;
44 		}
45 
46 		field++;
47 	}
48 }
49 
50 static void
parse_windows(char ** line,char * type)51 parse_windows(char **line, char *type)
52 {
53 	char *tok;
54 	int field = 0;
55 
56 	*type = '-';
57 	while ((tok = strsep(line, " \t")) != NULL) {
58 		if (*tok == '\0')
59 			continue;
60 
61 		if (field == 2 && strcmp(tok, "<DIR>") == 0)
62 			*type = 'd';
63 
64 		if (field == 2) {
65 			if (line == NULL || *line == NULL)
66 				break;
67 			while (**line == ' ' || **line == '\t')
68 				(*line)++;
69 			break;
70 		}
71 
72 		field++;
73 	}
74 }
75 
76 void
parse_list(char ** line,char * type)77 parse_list(char **line, char *type)
78 {
79 	if (**line >= '0' && **line <= '9')
80 		parse_windows(line, type);
81 	else
82 		parse_unix(line, type);
83 }
84 
85 #endif /* !SMALL */
86 
87