| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | SMB client library test program
|
|---|
| 4 | Copyright (C) Andrew Tridgell 1998
|
|---|
| 5 | Copyright (C) Richard Sharpe 2000
|
|---|
| 6 | Copyright (C) John Terpsra 2000
|
|---|
| 7 |
|
|---|
| 8 | This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | it under the terms of the GNU General Public License as published by
|
|---|
| 10 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 11 | (at your option) any later version.
|
|---|
| 12 |
|
|---|
| 13 | This program is distributed in the hope that it will be useful,
|
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | GNU General Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License
|
|---|
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <errno.h>
|
|---|
| 24 | #include <time.h>
|
|---|
| 25 | #include <sys/time.h>
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <unistd.h>
|
|---|
| 28 | #include <stdlib.h>
|
|---|
| 29 | #include "libsmbclient.h"
|
|---|
| 30 | #include "get_auth_data_fn.h"
|
|---|
| 31 |
|
|---|
| 32 | int global_id = 0;
|
|---|
| 33 |
|
|---|
| 34 | void print_list_fn(struct print_job_info *pji)
|
|---|
| 35 | {
|
|---|
| 36 |
|
|---|
| 37 | fprintf(stdout, "Print job: ID: %u, Prio: %u, Size: %lu, User: %s, Name: %s\n",
|
|---|
| 38 | pji->id,
|
|---|
| 39 | pji->priority,
|
|---|
| 40 | (unsigned long) pji->size,
|
|---|
| 41 | pji->user,
|
|---|
| 42 | pji->name);
|
|---|
| 43 |
|
|---|
| 44 | global_id = pji->id;
|
|---|
| 45 |
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | int main(int argc, char *argv[])
|
|---|
| 49 | {
|
|---|
| 50 | int err, fd, dh1, dh2, dh3, dsize, dirc;
|
|---|
| 51 | const char *file = "smb://samba/public/testfile.txt";
|
|---|
| 52 | const char *file2 = "smb://samba/public/testfile2.txt";
|
|---|
| 53 | char buff[256];
|
|---|
| 54 | char dirbuf[512];
|
|---|
| 55 | char *dirp;
|
|---|
| 56 | struct stat st1, st2;
|
|---|
| 57 |
|
|---|
| 58 | err = smbc_init(get_auth_data_fn, 10); /* Initialize things */
|
|---|
| 59 |
|
|---|
| 60 | if (err < 0) {
|
|---|
| 61 |
|
|---|
| 62 | fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));
|
|---|
| 63 |
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if (argc > 1) {
|
|---|
| 67 |
|
|---|
| 68 | if ((dh1 = smbc_opendir(argv[1]))<1) {
|
|---|
| 69 |
|
|---|
| 70 | fprintf(stderr, "Could not open directory: %s: %s\n",
|
|---|
| 71 | argv[1], strerror(errno));
|
|---|
| 72 |
|
|---|
| 73 | exit(1);
|
|---|
| 74 |
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | fprintf(stdout, "Directory handles: %u, %u, %u\n", dh1, dh2, dh3);
|
|---|
| 78 |
|
|---|
| 79 | /* Now, list those directories, but in funny ways ... */
|
|---|
| 80 |
|
|---|
| 81 | dirp = (char *)dirbuf;
|
|---|
| 82 |
|
|---|
| 83 | if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp,
|
|---|
| 84 | sizeof(dirbuf))) < 0) {
|
|---|
| 85 |
|
|---|
| 86 | fprintf(stderr, "Problems getting directory entries: %s\n",
|
|---|
| 87 | strerror(errno));
|
|---|
| 88 |
|
|---|
| 89 | exit(1);
|
|---|
| 90 |
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /* Now, process the list of names ... */
|
|---|
| 94 |
|
|---|
| 95 | fprintf(stdout, "Directory listing, size = %u\n", dirc);
|
|---|
| 96 |
|
|---|
| 97 | while (dirc > 0) {
|
|---|
| 98 |
|
|---|
| 99 | dsize = ((struct smbc_dirent *)dirp)->dirlen;
|
|---|
| 100 | fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
|
|---|
| 101 | ((struct smbc_dirent *)dirp)->smbc_type,
|
|---|
| 102 | ((struct smbc_dirent *)dirp)->name,
|
|---|
| 103 | ((struct smbc_dirent *)dirp)->comment);
|
|---|
| 104 |
|
|---|
| 105 | dirp += dsize;
|
|---|
| 106 | dirc -= dsize;
|
|---|
| 107 |
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | dirp = (char *)dirbuf;
|
|---|
| 111 |
|
|---|
| 112 | exit(1);
|
|---|
| 113 |
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /* For now, open a file on a server that is hard coded ... later will
|
|---|
| 117 | * read from the command line ...
|
|---|
| 118 | */
|
|---|
| 119 |
|
|---|
| 120 | fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
|---|
| 121 |
|
|---|
| 122 | if (fd < 0) {
|
|---|
| 123 |
|
|---|
| 124 | fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
|
|---|
| 125 | exit(0);
|
|---|
| 126 |
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | fprintf(stdout, "Opened or created file: %s\n", file);
|
|---|
| 130 |
|
|---|
| 131 | /* Now, write some date to the file ... */
|
|---|
| 132 |
|
|---|
| 133 | bzero(buff, sizeof(buff));
|
|---|
| 134 | strcpy(buff, "Some test data for the moment ...");
|
|---|
| 135 |
|
|---|
| 136 | err = smbc_write(fd, buff, sizeof(buff));
|
|---|
| 137 |
|
|---|
| 138 | if (err < 0) {
|
|---|
| 139 |
|
|---|
| 140 | fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
|
|---|
| 141 | exit(0);
|
|---|
| 142 |
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | fprintf(stdout, "Wrote %lu bytes to file: %s\n",
|
|---|
| 146 | (unsigned long) sizeof(buff), buff);
|
|---|
| 147 |
|
|---|
| 148 | /* Now, seek the file back to offset 0 */
|
|---|
| 149 |
|
|---|
| 150 | err = smbc_lseek(fd, SEEK_SET, 0);
|
|---|
| 151 |
|
|---|
| 152 | if (err < 0) {
|
|---|
| 153 |
|
|---|
| 154 | fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
|
|---|
| 155 | exit(0);
|
|---|
| 156 |
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | fprintf(stdout, "Completed lseek on file: %s\n", file);
|
|---|
| 160 |
|
|---|
| 161 | /* Now, read the file contents back ... */
|
|---|
| 162 |
|
|---|
| 163 | err = smbc_read(fd, buff, sizeof(buff));
|
|---|
| 164 |
|
|---|
| 165 | if (err < 0) {
|
|---|
| 166 |
|
|---|
| 167 | fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
|
|---|
| 168 | exit(0);
|
|---|
| 169 |
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | fprintf(stdout, "Read file: %s\n", buff); /* Should check the contents */
|
|---|
| 173 |
|
|---|
| 174 | fprintf(stdout, "Now fstat'ing file: %s\n", file);
|
|---|
| 175 |
|
|---|
| 176 | err = smbc_fstat(fd, &st1);
|
|---|
| 177 |
|
|---|
| 178 | if (err < 0) {
|
|---|
| 179 |
|
|---|
| 180 | fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
|
|---|
| 181 | exit(0);
|
|---|
| 182 |
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 | /* Now, close the file ... */
|
|---|
| 187 |
|
|---|
| 188 | err = smbc_close(fd);
|
|---|
| 189 |
|
|---|
| 190 | if (err < 0) {
|
|---|
| 191 |
|
|---|
| 192 | fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
|
|---|
| 193 |
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /* Now, rename the file ... */
|
|---|
| 197 |
|
|---|
| 198 | err = smbc_rename(file, file2);
|
|---|
| 199 |
|
|---|
| 200 | if (err < 0) {
|
|---|
| 201 |
|
|---|
| 202 | fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
|
|---|
| 203 |
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | fprintf(stdout, "Renamed file %s to %s\n", file, file2);
|
|---|
| 207 |
|
|---|
| 208 | /* Now, create a file and delete it ... */
|
|---|
| 209 |
|
|---|
| 210 | fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
|
|---|
| 211 |
|
|---|
| 212 | fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
|
|---|
| 213 |
|
|---|
| 214 | if (fd < 0) {
|
|---|
| 215 |
|
|---|
| 216 | fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
|
|---|
| 217 | exit(0);
|
|---|
| 218 |
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | fprintf(stdout, "Opened or created file: %s\n", file);
|
|---|
| 222 |
|
|---|
| 223 | err = smbc_close(fd);
|
|---|
| 224 |
|
|---|
| 225 | if (err < 0) {
|
|---|
| 226 |
|
|---|
| 227 | fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
|
|---|
| 228 | exit(0);
|
|---|
| 229 |
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /* Now, delete the file ... */
|
|---|
| 233 |
|
|---|
| 234 | fprintf(stdout, "File %s created, now deleting ...\n", file);
|
|---|
| 235 |
|
|---|
| 236 | err = smbc_unlink(file);
|
|---|
| 237 |
|
|---|
| 238 | if (err < 0) {
|
|---|
| 239 |
|
|---|
| 240 | fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
|
|---|
| 241 | exit(0);
|
|---|
| 242 |
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /* Now, stat the file, file 2 ... */
|
|---|
| 246 |
|
|---|
| 247 | fprintf(stdout, "Now stat'ing file: %s\n", file);
|
|---|
| 248 |
|
|---|
| 249 | err = smbc_stat(file2, &st2);
|
|---|
| 250 |
|
|---|
| 251 | if (err < 0) {
|
|---|
| 252 |
|
|---|
| 253 | fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
|
|---|
| 254 | exit(0);
|
|---|
| 255 |
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | fprintf(stdout, "Stat'ed file: %s. Size = %d, mode = %04X\n", file2,
|
|---|
| 259 | (int)st2.st_size, st2.st_mode);
|
|---|
| 260 | fprintf(stdout, " time: %s\n", ctime(&st2.st_atime));
|
|---|
| 261 | fprintf(stdout, "Earlier stat: %s, Size = %d, mode = %04X\n", file,
|
|---|
| 262 | (int)st1.st_size, st1.st_mode);
|
|---|
| 263 | fprintf(stdout, " time: %s\n", ctime(&st1.st_atime));
|
|---|
| 264 |
|
|---|
| 265 | /* Now, make a directory ... */
|
|---|
| 266 |
|
|---|
| 267 | fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
|
|---|
| 268 |
|
|---|
| 269 | if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
|
|---|
| 270 |
|
|---|
| 271 | fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
|
|---|
| 272 | strerror(errno));
|
|---|
| 273 |
|
|---|
| 274 | if (errno == EEXIST) { /* Try to delete the directory */
|
|---|
| 275 |
|
|---|
| 276 | fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
|
|---|
| 277 |
|
|---|
| 278 | if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
|
|---|
| 279 |
|
|---|
| 280 | fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
|
|---|
| 281 |
|
|---|
| 282 | exit(0);
|
|---|
| 283 |
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
|
|---|
| 287 |
|
|---|
| 288 | if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
|
|---|
| 289 |
|
|---|
| 290 | fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
|
|---|
| 291 | strerror(errno));
|
|---|
| 292 |
|
|---|
| 293 | fprintf(stderr, "I give up!\n");
|
|---|
| 294 |
|
|---|
| 295 | exit(1);
|
|---|
| 296 |
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | exit(0);
|
|---|
| 302 |
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | fprintf(stdout, "Made dir: make-dir\n");
|
|---|
| 306 | return 0;
|
|---|
| 307 | }
|
|---|