~ubuntu-branches/ubuntu/utopic/coreutils/utopic-proposed

« back to all changes in this revision

Viewing changes to src/dirname.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-11-28 03:03:42 UTC
  • mfrom: (8.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20121128030342-21zanj8354gas5gr
Tags: 8.20-3ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Make 'uname -i -p' return the real processor/hardware, instead of
    unknown.
  - Build-depend on gettext:any instead of on gettext, so that apt-get can
    properly resolve build-dependencies on the tool when cross-building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* dirname -- strip suffix from file name
2
2
 
3
 
   Copyright (C) 1990-1997, 1999-2002, 2004-2011 Free Software Foundation, Inc.
 
3
   Copyright (C) 1990-2012 Free Software Foundation, Inc.
4
4
 
5
5
   This program is free software: you can redistribute it and/or modify
6
6
   it under the terms of the GNU General Public License as published by
23
23
#include <sys/types.h>
24
24
 
25
25
#include "system.h"
26
 
#include "long-options.h"
27
26
#include "error.h"
28
 
#include "quote.h"
29
27
 
30
 
/* The official name of this program (e.g., no `g' prefix).  */
 
28
/* The official name of this program (e.g., no 'g' prefix).  */
31
29
#define PROGRAM_NAME "dirname"
32
30
 
33
31
#define AUTHORS \
34
32
  proper_name ("David MacKenzie"), \
35
33
  proper_name ("Jim Meyering")
36
34
 
 
35
static struct option const longopts[] =
 
36
{
 
37
  {"zero", no_argument, NULL, 'z'},
 
38
  {GETOPT_HELP_OPTION_DECL},
 
39
  {GETOPT_VERSION_OPTION_DECL},
 
40
  {NULL, 0, NULL, 0}
 
41
};
 
42
 
37
43
void
38
44
usage (int status)
39
45
{
40
46
  if (status != EXIT_SUCCESS)
41
 
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
42
 
             program_name);
 
47
    emit_try_help ();
43
48
  else
44
49
    {
45
50
      printf (_("\
46
 
Usage: %s NAME\n\
47
 
  or:  %s OPTION\n\
 
51
Usage: %s [OPTION] NAME...\n\
48
52
"),
49
 
              program_name, program_name);
 
53
              program_name);
50
54
      fputs (_("\
51
 
Output NAME with its last non-slash component and trailing slashes removed;\n\
52
 
if NAME contains no /'s, output `.' (meaning the current directory).\n\
 
55
Output each NAME with its last non-slash component and trailing slashes\n\
 
56
removed; if NAME contains no /'s, output '.' (meaning the current directory).\n\
53
57
\n\
54
58
"), stdout);
 
59
      fputs (_("\
 
60
  -z, --zero     separate output with NUL rather than newline\n\
 
61
"), stdout);
55
62
      fputs (HELP_OPTION_DESCRIPTION, stdout);
56
63
      fputs (VERSION_OPTION_DESCRIPTION, stdout);
57
64
      printf (_("\
58
65
\n\
59
66
Examples:\n\
60
 
  %s /usr/bin/      Output \"/usr\".\n\
61
 
  %s stdio.h        Output \".\".\n\
 
67
  %s /usr/bin/          -> \"/usr\"\n\
 
68
  %s dir1/str dir2/str  -> \"dir1\" followed by \"dir2\"\n\
 
69
  %s stdio.h            -> \".\"\n\
62
70
"),
63
 
              program_name, program_name);
 
71
              program_name, program_name, program_name);
64
72
      emit_ancillary_info ();
65
73
    }
66
74
  exit (status);
70
78
main (int argc, char **argv)
71
79
{
72
80
  static char const dot = '.';
 
81
  bool use_nuls = false;
73
82
  char const *result;
74
83
  size_t len;
75
84
 
81
90
 
82
91
  atexit (close_stdout);
83
92
 
84
 
  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
85
 
                      usage, AUTHORS, (char const *) NULL);
86
 
  if (getopt_long (argc, argv, "+", NULL, NULL) != -1)
87
 
    usage (EXIT_FAILURE);
 
93
  while (true)
 
94
    {
 
95
      int c = getopt_long (argc, argv, "z", longopts, NULL);
 
96
 
 
97
      if (c == -1)
 
98
        break;
 
99
 
 
100
      switch (c)
 
101
        {
 
102
        case 'z':
 
103
          use_nuls = true;
 
104
          break;
 
105
 
 
106
        case_GETOPT_HELP_CHAR;
 
107
        case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
 
108
 
 
109
        default:
 
110
          usage (EXIT_FAILURE);
 
111
        }
 
112
    }
88
113
 
89
114
  if (argc < optind + 1)
90
115
    {
92
117
      usage (EXIT_FAILURE);
93
118
    }
94
119
 
95
 
  if (optind + 1 < argc)
96
 
    {
97
 
      error (0, 0, _("extra operand %s"), quote (argv[optind + 1]));
98
 
      usage (EXIT_FAILURE);
99
 
    }
100
 
 
101
 
  result = argv[optind];
102
 
  len = dir_len (result);
103
 
 
104
 
  if (! len)
105
 
    {
106
 
      result = &dot;
107
 
      len = 1;
108
 
    }
109
 
 
110
 
  fwrite (result, 1, len, stdout);
111
 
  putchar ('\n');
 
120
  for (; optind < argc; optind++)
 
121
    {
 
122
      result = argv[optind];
 
123
      len = dir_len (result);
 
124
 
 
125
      if (! len)
 
126
        {
 
127
          result = &dot;
 
128
          len = 1;
 
129
        }
 
130
 
 
131
      fwrite (result, 1, len, stdout);
 
132
      putchar (use_nuls ? '\0' :'\n');
 
133
    }
112
134
 
113
135
  exit (EXIT_SUCCESS);
114
136
}