"Fossies" - the Fresh Open Source Software Archive

Member "fdupes-2.1.2/dir.c" (12 Aug 2020, 1450 Bytes) of package /linux/privat/fdupes-2.1.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dir.c" see the Fossies "Dox" file reference documentation.

    1 /* Copyright (c) 2018 Adrian Lopez
    2 
    3    This software is provided 'as-is', without any express or implied
    4    warranty. In no event will the authors be held liable for any damages
    5    arising from the use of this software.
    6 
    7    Permission is granted to anyone to use this software for any purpose,
    8    including commercial applications, and to alter it and redistribute it
    9    freely, subject to the following restrictions:
   10 
   11    1. The origin of this software must not be misrepresented; you must not
   12       claim that you wrote the original software. If you use this software
   13       in a product, an acknowledgment in the product documentation would be
   14       appreciated but is not required.
   15    2. Altered source versions must be plainly marked as such, and must not be
   16       misrepresented as being the original software.
   17    3. This notice may not be removed or altered from any source distribution. */
   18 
   19 #include <errno.h>
   20 #include <stdlib.h>
   21 #include <unistd.h>
   22 #include "dir.h"
   23 
   24 char *getworkingdirectory()
   25 {
   26   size_t size;
   27   char *result;
   28   char *new_result;
   29   char *cwd;
   30   
   31   size = 1024;
   32 
   33   result = 0;
   34   do
   35   {
   36     new_result = (char*) realloc(result, size);
   37     if (new_result == 0)
   38     {
   39       if (result != 0)
   40         free(result);
   41 
   42       return 0;
   43     }
   44 
   45     result = new_result;
   46 
   47     cwd = getcwd(result, size);
   48 
   49     size *= 2;
   50   } while (cwd == 0 && errno == ERANGE);
   51 
   52   if (cwd == 0)
   53   {
   54     free(result);
   55     return 0;
   56   }
   57 
   58   return result;
   59 }