http://prdownloads.sourceforge.net/lufs/lufs-0.9.7.tar.gz?download
[lufs.git] / filesystems / ftpfs / ftpsys_netware.cpp
1 /*
2  * ftpsys_netware.cpp
3  * Copyright (C) 2002 Jaroslav Rohel <rohel@kn.vutbr.cz>
4  *               inspired by ftpsys_unix writen by Florian Malita <mali@go.ro>
5  *
6  * This file is part of LUFS, a free userspace filesystem implementation.
7  * See http://lufs.sourceforge.net/ for updates.
8  *
9  * LUFS is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * LUFS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <string.h>
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 #ifndef __USE_XOPEN
34 #define __USE_XOPEN
35 #endif
36 #include <time.h>
37
38 #include <lufs/proto.h>
39 #include <lufs/fs.h>
40
41 #include "ftpsys.h"
42 #include "ftpsys_netware.h"
43
44 static char*
45 next_word(char *c){
46     while( (*c != ' ') && (*c != 0))
47         c++;
48     if(! *c)
49         return c;
50
51     while(*c == ' ')
52         c++;    
53     return c;
54 }
55
56 static char*
57 nth_word(char *line, int n){
58   int i;
59   char *c;
60
61   for(c = line, i = 0; (c != NULL) && (i < n); i++)
62     c = next_word(c);
63
64   return c;
65 }
66
67 ftpsys_netware::ftpsys_netware(){
68     CMD_LIST = "LIST -al";
69 }
70
71 ftpsys_netware::~ftpsys_netware(){
72
73 }
74
75 int
76 ftpsys_netware::parse_line(char *buf, char *file, struct lufs_fattr *fattr, char *link, struct credentials *cred){
77     unsigned long nlink, size;
78     int res, own = 0;
79     struct tm tm;
80     time_t tt;
81     char user[32], month[5], day[5], year[6], date[20];
82     char *c, *cc;
83
84     *file = *link = user[0] = month[0] = day[0] = year[0] = 0;
85
86     nlink = 1;
87     if((res = sscanf(buf, "%*2s %*12s %32s %lu %3s %2s %5s %1024s", user, &size, month, day, year, file)) < 6){
88         WARN("could only match "<<res<<" attributes!");
89
90         return -1;
91     }
92
93     sprintf(date,"%s,%s,%s", year, month, day);
94     tt = time(NULL);
95     memcpy(&tm, gmtime(&tt), sizeof(struct tm));
96     tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
97     if(strchr(year, ':'))
98         strptime(date, "%H:%M,%b,%d", &tm);
99     else
100         strptime(date, "%Y,%b,%d", &tm);
101
102     if(!strcmp(cred->user, user))
103         own = 1;
104     
105     memset(fattr, 0, sizeof(struct lufs_fattr));
106     fattr->f_nlink = nlink;
107     fattr->f_size = size;
108     fattr->f_ctime = fattr->f_mtime = fattr->f_atime = mktime(&tm);
109
110     if(tolower(buf[0]) == 'd') fattr->f_mode = S_IFDIR;
111     else fattr->f_mode = S_IFREG;
112     
113     fattr->f_mode |= S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
114     if (fattr->f_mode & S_IFDIR) fattr->f_mode |= S_IXUSR | S_IXGRP | S_IXOTH;
115
116     for(c = buf; *c; c++){
117         if((*c == 0x0a) || (*c == 0x0d)){
118             *c = 0;
119             break;
120         }
121     }
122
123     if((c = nth_word(buf, 8))){
124
125       TRACE("left: " << c);
126       cc=strstr(c, "->");
127       if(cc != NULL){
128         *(cc-1) = 0;
129         strcpy(file, c);
130         strcpy(link, (cc + 3));
131       }else{
132         strcpy(file, c);
133       }
134
135       TRACE("file: " << file<<", link: " << link);
136       return 0;
137     }else
138       return -1;
139 }