examples: Make libsmbclient samples look a *bit* less ugly
[samba.git] / examples / libsmbclient / testsmbc.c
index 3c9aa5674cdaab40cd988a72abadcb6c081952b3..a61d38ecaba330640d6921c2698e9571100de401 100644 (file)
@@ -1,20 +1,20 @@
-/* 
+/*
    Unix SMB/CIFS implementation.
    SMB client library test program
    Copyright (C) Andrew Tridgell 1998
    Copyright (C) Richard Sharpe 2000
    Copyright (C) John Terpsra 2000
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 int main(int argc, char *argv[])
 {
-  int err, fd, dh1, dsize, dirc;
-  const char *file = "smb://samba/public/testfile.txt";
-  const char *file2 = "smb://samba/public/testfile2.txt";
-  char buff[256];
-  char dirbuf[512];
-  char *dirp;
-  struct stat st1, st2;
+       int err, fd, dh1, dsize, dirc;
+       const char *file = "smb://samba/public/testfile.txt";
+       const char *file2 = "smb://samba/public/testfile2.txt";
+       char buff[256];
+       char dirbuf[512];
+       char *dirp;
+       struct stat st1, st2;
+
+       err = smbc_init(get_auth_data_fn,  10); /* Initialize things */
+
+       if (err < 0) {
+
+               fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));
 
-  err = smbc_init(get_auth_data_fn,  10); /* Initialize things */
+       }
 
-  if (err < 0) {
+       if (argc > 1) {
 
-    fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));
+               if ((dh1 = smbc_opendir(argv[1]))<1) {
 
-  }
+                       fprintf(stderr, "Could not open directory: %s: %s\n",
+                               argv[1], strerror(errno));
 
-  if (argc > 1) {
+                       exit(1);
 
-    if ((dh1 = smbc_opendir(argv[1]))<1) {
+               }
 
-      fprintf(stderr, "Could not open directory: %s: %s\n",
-             argv[1], strerror(errno));
+               fprintf(stdout, "Directory handle: %u\n", dh1);
 
-      exit(1);
+               /* Now, list those directories, but in funny ways ... */
 
-    }
+               dirp = (char *)dirbuf;
 
-    fprintf(stdout, "Directory handle: %u\n", dh1);
+               if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp,
+                                         sizeof(dirbuf))) < 0) {
 
-    /* Now, list those directories, but in funny ways ... */
+                       fprintf(stderr, "Problems getting directory entries: %s\n",
+                               strerror(errno));
 
-    dirp = (char *)dirbuf;
+                       exit(1);
 
-    if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp, 
-                             sizeof(dirbuf))) < 0) {
+               }
 
-      fprintf(stderr, "Problems getting directory entries: %s\n",
-             strerror(errno));
+               /* Now, process the list of names ... */
 
-      exit(1);
+               fprintf(stdout, "Directory listing, size = %u\n", dirc);
 
-    }
+               while (dirc > 0) {
 
-    /* Now, process the list of names ... */
+                       dsize = ((struct smbc_dirent *)dirp)->dirlen;
+                       fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
+                               ((struct smbc_dirent *)dirp)->smbc_type,
+                               ((struct smbc_dirent *)dirp)->name,
+                               ((struct smbc_dirent *)dirp)->comment);
 
-    fprintf(stdout, "Directory listing, size = %u\n", dirc);
+                       dirp += dsize;
+                       dirc -= dsize;
 
-    while (dirc > 0) {
+               }
 
-      dsize = ((struct smbc_dirent *)dirp)->dirlen;
-      fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
-             ((struct smbc_dirent *)dirp)->smbc_type, 
-             ((struct smbc_dirent *)dirp)->name, 
-             ((struct smbc_dirent *)dirp)->comment);
+               dirp = (char *)dirbuf;
 
-      dirp += dsize;
-      dirc -= dsize;
+               exit(1);
 
-    }
+       }
 
-    dirp = (char *)dirbuf;
+       /* For now, open a file on a server that is hard coded ... later will
+        * read from the command line ...
+        */
 
-    exit(1);
+       fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
 
-  }
+       if (fd < 0) {
 
-  /* For now, open a file on a server that is hard coded ... later will
-   * read from the command line ...
-   */
+               fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
+               exit(0);
 
-  fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
+       }
 
-  if (fd < 0) {
+       fprintf(stdout, "Opened or created file: %s\n", file);
 
-    fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
-    exit(0);
+       /* Now, write some date to the file ... */
 
-  }
+       memset(buff, '\0', sizeof(buff));
+       snprintf(buff, sizeof(buff), "%s", "Some test data for the moment ...");
 
-  fprintf(stdout, "Opened or created file: %s\n", file);
+       err = smbc_write(fd, buff, sizeof(buff));
 
-  /* Now, write some date to the file ... */
+       if (err < 0) {
 
-  memset(buff, '\0', sizeof(buff));
-  snprintf(buff, sizeof(buff), "%s", "Some test data for the moment ...");
+               fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
+               exit(0);
 
-  err = smbc_write(fd, buff, sizeof(buff));
+       }
 
-  if (err < 0) {
-    
-    fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
-    exit(0);
+       fprintf(stdout, "Wrote %lu bytes to file: %s\n",
+               (unsigned long) sizeof(buff), buff);
 
-  }
+       /* Now, seek the file back to offset 0 */
 
-  fprintf(stdout, "Wrote %lu bytes to file: %s\n",
-          (unsigned long) sizeof(buff), buff);
+       err = smbc_lseek(fd, SEEK_SET, 0);
 
-  /* Now, seek the file back to offset 0 */
+       if (err < 0) {
 
-  err = smbc_lseek(fd, SEEK_SET, 0);
+               fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
+               exit(0);
 
-  if (err < 0) {
+       }
 
-    fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
-    exit(0);
+       fprintf(stdout, "Completed lseek on file: %s\n", file);
 
-  }
+       /* Now, read the file contents back ... */
 
-  fprintf(stdout, "Completed lseek on file: %s\n", file);
+       err = smbc_read(fd, buff, sizeof(buff));
 
-  /* Now, read the file contents back ... */
+       if (err < 0) {
 
-  err = smbc_read(fd, buff, sizeof(buff));
+               fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
+               exit(0);
 
-  if (err < 0) {
+       }
 
-    fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
-    exit(0);
+       fprintf(stdout, "Read file: %s\n", buff);  /* Should check the contents */
 
-  }
+       fprintf(stdout, "Now fstat'ing file: %s\n", file);
 
-  fprintf(stdout, "Read file: %s\n", buff);  /* Should check the contents */
+       err = smbc_fstat(fd, &st1);
 
-  fprintf(stdout, "Now fstat'ing file: %s\n", file);
+       if (err < 0) {
 
-  err = smbc_fstat(fd, &st1);
+               fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
+               exit(0);
 
-  if (err < 0) {
+       }
 
-    fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
-    exit(0);
 
-  }
+       /* Now, close the file ... */
 
+       err = smbc_close(fd);
 
-  /* Now, close the file ... */
+       if (err < 0) {
 
-  err = smbc_close(fd);
+               fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
 
-  if (err < 0) {
+       }
 
-    fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
+       /* Now, rename the file ... */
 
-  }
+       err = smbc_rename(file, file2);
 
-  /* Now, rename the file ... */
+       if (err < 0) {
 
-  err = smbc_rename(file, file2);
+               fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
 
-  if (err < 0) {
+       }
 
-    fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
+       fprintf(stdout, "Renamed file %s to %s\n", file, file2);
 
-  }
+       /* Now, create a file and delete it ... */
 
-  fprintf(stdout, "Renamed file %s to %s\n", file, file2);
+       fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
 
-  /* Now, create a file and delete it ... */
+       fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
 
-  fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
+       if (fd < 0) {
 
-  fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
+               fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
+               exit(0);
 
-  if (fd < 0) {
+       }
 
-    fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
-    exit(0);
+       fprintf(stdout, "Opened or created file: %s\n", file);
 
-  }
+       err = smbc_close(fd);
 
-  fprintf(stdout, "Opened or created file: %s\n", file);
+       if (err < 0) {
 
-  err = smbc_close(fd);
+               fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
+               exit(0);
 
-  if (err < 0) {
+       }
 
-    fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
-    exit(0);
+       /* Now, delete the file ... */
 
-  }
-  
-  /* Now, delete the file ... */
+       fprintf(stdout, "File %s created, now deleting ...\n", file);
 
-  fprintf(stdout, "File %s created, now deleting ...\n", file);
+       err = smbc_unlink(file);
 
-  err = smbc_unlink(file);
+       if (err < 0) {
 
-  if (err < 0) {
+               fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
+               exit(0);
 
-    fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
-    exit(0);
+       }
 
-  }
+       /* Now, stat the file, file 2 ... */
 
-  /* Now, stat the file, file 2 ... */
+       fprintf(stdout, "Now stat'ing file: %s\n", file);
 
-  fprintf(stdout, "Now stat'ing file: %s\n", file);
+       err = smbc_stat(file2, &st2);
 
-  err = smbc_stat(file2, &st2);
+       if (err < 0) {
 
-  if (err < 0) {
+               fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
+               exit(0);
 
-    fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
-    exit(0);
+       }
 
-  }
+       fprintf(stdout, "Stat'ed file:   %s. Size = %d, mode = %04X\n", file2,
+               (int)st2.st_size, (unsigned int)st2.st_mode);
+       fprintf(stdout, "    time: %s\n", ctime(&st2.st_atime));
+       fprintf(stdout, "Earlier stat:   %s, Size = %d, mode = %04X\n", file,
+               (int)st1.st_size, (unsigned int)st1.st_mode);
+       fprintf(stdout, "    time: %s\n", ctime(&st1.st_atime));
 
-  fprintf(stdout, "Stat'ed file:   %s. Size = %d, mode = %04X\n", file2, 
-         (int)st2.st_size, (unsigned int)st2.st_mode);
-  fprintf(stdout, "    time: %s\n", ctime(&st2.st_atime));
-  fprintf(stdout, "Earlier stat:   %s, Size = %d, mode = %04X\n", file, 
-         (int)st1.st_size, (unsigned int)st1.st_mode);
-  fprintf(stdout, "    time: %s\n", ctime(&st1.st_atime));
+       /* Now, make a directory ... */
 
-  /* Now, make a directory ... */
+       fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
 
-  fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
+       if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
 
-  if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
+               fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
+                       strerror(errno));
 
-    fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n", 
-           strerror(errno));
+               if (errno == EEXIST) { /* Try to delete the directory */
 
-    if (errno == EEXIST) { /* Try to delete the directory */
+                       fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
 
-      fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
+                       if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
 
-      if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
+                               fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
 
-       fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
+                               exit(0);
 
-       exit(0);
+                       }
 
-      }
+                       fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
 
-      fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
+                       if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
 
-      if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
+                               fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
+                                       strerror(errno));
 
-       fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
-               strerror(errno));
+                               fprintf(stderr, "I give up!\n");
 
-       fprintf(stderr, "I give up!\n");
+                               exit(1);
 
-       exit(1);
+                       }
 
-      }
+               }
 
-    }
+               exit(0);
 
-    exit(0);
-    
-  }
+       }
 
-  fprintf(stdout, "Made dir: make-dir\n");
-  return 0;
+       fprintf(stdout, "Made dir: make-dir\n");
+       return 0;
 }