preparing for release of alpha.0.5
[Samba.git] / source / utils / nbio.c
blobf72be36842fb48d007fe61761b8acaa9234d9243
1 #define NBDEBUG 0
3 /*
4 Unix SMB/Netbios implementation.
5 Version 1.9.
6 SMB torture tester
7 Copyright (C) Andrew Tridgell 1997-1998
9 This program 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.
14 This program 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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define NO_SYSLOG
26 #include "includes.h"
28 #define MAX_FILES 1000
30 static char buf[70000];
31 extern int line_count;
33 static struct {
34 int fd;
35 int handle;
36 } ftable[MAX_FILES];
38 static struct cli_state *c;
40 static void sigsegv(int sig)
42 char line[200];
43 printf("segv at line %d\n", line_count);
44 slprintf(line, sizeof(line), "/usr/X11R6/bin/xterm -e gdb /proc/%d/exe %d",
45 (int)getpid(), (int)getpid());
46 system(line);
47 exit(1);
50 void nb_setup(struct cli_state *cli)
52 signal(SIGSEGV, sigsegv);
53 /* to be like a true Windows client we need to negotiate oplocks */
54 cli->use_oplocks = True;
55 c = cli;
59 void nb_unlink(char *fname)
61 strupper(fname);
63 if (!cli_unlink(c, fname)) {
64 #if NBDEBUG
65 printf("(%d) unlink %s failed (%s)\n",
66 line_count, fname, cli_errstr(c));
67 #endif
71 void nb_open(char *fname, int handle, int size)
73 int fd, i;
74 int flags = O_RDWR|O_CREAT;
75 size_t st_size;
76 static int count;
78 strupper(fname);
80 if (size == 0) flags |= O_TRUNC;
82 fd = cli_open(c, fname, flags, DENY_NONE);
83 if (fd == -1) {
84 #if NBDEBUG
85 printf("(%d) open %s failed for handle %d (%s)\n",
86 line_count, fname, handle, cli_errstr(c));
87 #endif
88 return;
90 cli_getattrE(c, fd, NULL, &st_size, NULL, NULL, NULL);
91 if (size > st_size) {
92 #if NBDEBUG
93 printf("(%d) needs expanding %s to %d from %d\n",
94 line_count, fname, size, (int)st_size);
95 #endif
96 } else if (size < st_size) {
97 #if NBDEBUG
98 printf("(%d) needs truncating %s to %d from %d\n",
99 line_count, fname, size, (int)st_size);
100 #endif
102 for (i=0;i<MAX_FILES;i++) {
103 if (ftable[i].handle == 0) break;
105 if (i == MAX_FILES) {
106 printf("file table full for %s\n", fname);
107 exit(1);
109 ftable[i].handle = handle;
110 ftable[i].fd = fd;
111 if (count++ % 100 == 0) {
112 printf(".");
116 void nb_write(int handle, int size, int offset)
118 int i;
120 if (buf[0] == 0) memset(buf, 1, sizeof(buf));
122 for (i=0;i<MAX_FILES;i++) {
123 if (ftable[i].handle == handle) break;
125 if (i == MAX_FILES) {
126 #if NBDEBUG
127 printf("(%d) nb_write: handle %d was not open size=%d ofs=%d\n",
128 line_count, handle, size, offset);
129 #endif
130 return;
132 if (cli_smbwrite(c, ftable[i].fd, buf, offset, size) != size) {
133 printf("(%d) write failed on handle %d\n",
134 line_count, handle);
138 void nb_read(int handle, int size, int offset)
140 int i, ret;
142 for (i=0;i<MAX_FILES;i++) {
143 if (ftable[i].handle == handle) break;
145 if (i == MAX_FILES) {
146 printf("(%d) nb_read: handle %d was not open size=%d ofs=%d\n",
147 line_count, handle, size, offset);
148 return;
150 if ((ret=cli_read(c, ftable[i].fd, buf, offset, size)) != size) {
151 #if NBDEBUG
152 printf("(%d) read failed on handle %d ofs=%d size=%d res=%d\n",
153 line_count, handle, offset, size, ret);
154 #endif
158 void nb_close(int handle)
160 int i;
161 for (i=0;i<MAX_FILES;i++) {
162 if (ftable[i].handle == handle) break;
164 if (i == MAX_FILES) {
165 printf("(%d) nb_close: handle %d was not open\n",
166 line_count, handle);
167 return;
169 cli_close(c, ftable[i].fd);
170 ftable[i].handle = 0;
173 void nb_mkdir(char *fname)
175 strupper(fname);
177 if (!cli_mkdir(c, fname)) {
178 #if NBDEBUG
179 printf("mkdir %s failed (%s)\n",
180 fname, cli_errstr(c));
181 #endif
185 void nb_rmdir(char *fname)
187 strupper(fname);
189 if (!cli_rmdir(c, fname)) {
190 #if NBDEBUG
191 printf("rmdir %s failed (%s)\n",
192 fname, cli_errstr(c));
193 #endif
197 void nb_rename(char *old, char *new)
199 strupper(old);
200 strupper(new);
202 if (!cli_rename(c, old, new)) {
203 #if NBDEBUG
204 printf("rename %s %s failed (%s)\n",
205 old, new, cli_errstr(c));
206 #endif
211 void nb_stat(char *fname, int size)
213 size_t st_size;
215 strupper(fname);
217 if (!cli_getatr(c, fname, NULL, &st_size, NULL)) {
218 #if NBDEBUG
219 printf("(%d) nb_stat: %s size=%d %s\n",
220 line_count, fname, size, cli_errstr(c));
221 #endif
222 return;
224 if (st_size != size) {
225 #if NBDEBUG
226 printf("(%d) nb_stat: %s wrong size %d %d\n",
227 line_count, fname, (int)st_size, size);
228 #endif
232 void nb_create(char *fname, int size)
234 nb_open(fname, 5000, size);
235 nb_close(5000);